issue153:inkscape
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue153:inkscape [2020/02/01 17:25] – créée d52fr | issue153:inkscape [2020/02/10 10:43] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Last time, we began work on an SVG file which uses JavaScript to animate the “transform” attribute of an object when loaded in a web browser. We’d got as far as animating the rotate() and skewX() functions, and you’d been left with the challenge of adding skewY() to the mix. Hopefully you worked out that this was mostly a case of copying the existing code, and replacing “X” with “Y”. Specifically: | + | **Last time, we began work on an SVG file which uses JavaScript to animate the “transform” attribute of an object when loaded in a web browser. We’d got as far as animating the rotate() and skewX() functions, and you’d been left with the challenge of adding skewY() to the mix. Hopefully you worked out that this was mostly a case of copying the existing code, and replacing “X” with “Y”. Specifically: |
• Create three new properties in the animProperties object: skewYDuration, | • Create three new properties in the animProperties object: skewYDuration, | ||
• Insert another block of code to calculate the current value of the skewY() function, given the supplied timestamp. This is literally a copy of the skewX code with the letter replaced throughout, resulting in a skewYAmount variable at the end. | • Insert another block of code to calculate the current value of the skewY() function, given the supplied timestamp. This is literally a copy of the skewX code with the letter replaced throughout, resulting in a skewYAmount variable at the end. | ||
- | • Add another line to the setAttribute() call to include the skewY() function in the transform attribute, passing the value of the skewYAmount variable via the template string. | + | • Add another line to the setAttribute() call to include the skewY() function in the transform attribute, passing the value of the skewYAmount variable via the template string.** |
- | I also left you with something of a puzzle: with the addition of the skewX() | + | La dernière fois, nous avons commencé à travailler sur un fichier SVG qui utilise JavaScript pour animer l' |
+ | ••Créer trois nouvelles propriétés dans l' | ||
+ | ••Insérer un autre bloc de code pour calculer la valeur courante de la fonction | ||
+ | ••Ajouter une autre ligne à l' | ||
- | The reason is quite simple: whereas | + | **I also left you with something of a puzzle: with the addition of the skewX() function, the square doesn’t just rotate and skew in the middle |
- | The red squares along the x-axis all skew “in-place”, | + | The reason is quite simple: whereas the rotate() function has an optional pair of parameters for setting the center of rotation, there’s no equivalent for the skew functions. Skewing takes place relative to a baseline, rather than a single point, but there’s no generic skew() function that lets you specify this baseline via two sets of coordinates. Instead, there are only two possible baselines available: the x-axis (via the skewX() function) and the y-axis (via the skewY() function). The graph below shows the effect of skewing along the x-axis for the same size of object positioned in a variety of places (bottom left)** |
+ | |||
+ | Je vous avais aussi laissé avec une sorte de puzzle : avec l' | ||
+ | |||
+ | La raison est assez simple : alors que la fonction rotate() a une paire de paramètres optionnels pour définir le centre de rotation, les fonctions skew n'ont pas d' | ||
+ | |||
+ | **The red squares along the x-axis all skew “in-place”, | ||
You may recall that we did some deliberate manipulation of our object in order to position it in the middle of the screen (50, 50). Unfortunately, | You may recall that we did some deliberate manipulation of our object in order to position it in the middle of the screen (50, 50). Unfortunately, | ||
• We want the object centered at (50, 50) for display. | • We want the object centered at (50, 50) for display. | ||
- | • We need the object to be centered at (0¸ 0) in order to skew it. | + | • We need the object to be centered at (0¸ 0) in order to skew it.** |
- | There are a few ways to solve this conundrum: | + | Les carrés rouges le long de l'axe des x se déforment « sur place », entraînant les formes de contour qui sont affichées. Les carrés vert et bleu - colorés pour éviter toute confusion quand ils se superposent - se déplacent à droite et à gauche par suite du processus de déformation. Il n'y a pas besoin de réfléchir longtemps pour réaliser que la quantité du déplacement vers la droite et la gauche est vraiment proportionnelle à la distance à l'axe des x ; ainsi, même un petit angle de déformation peut rapidement déplacer une forme sur une grande distance s'il est situé loin de l'axe des x. Et, bien sûr, ces règles s' |
+ | |||
+ | Vous devez vous rappeler que nous avons fait quelques modifications délibérées à notre objet de façon à le positionner au milieu de l' | ||
+ | ••Nous voulons que l' | ||
+ | ••Nous avons besoin que l' | ||
+ | |||
+ | **There are a few ways to solve this conundrum: | ||
• Use a translate() function to move the object to (0, 0); then skew it; then use another translate() function to move it back again. | • Use a translate() function to move the object to (0, 0); then skew it; then use another translate() function to move it back again. | ||
• Change the x and y attributes of the object so that it starts at position (0, 0). After skewing, add a translate() function to move it to (50, 50). | • Change the x and y attributes of the object so that it starts at position (0, 0). After skewing, add a translate() function to move it to (50, 50). | ||
• Use a matrix() function rather than the skewX()/ | • Use a matrix() function rather than the skewX()/ | ||
- | I’m going to immediately dismiss the last option, as it requires far too much mathematics. But it does, perhaps, explain why Inkscape always uses matrix() rather than the named skew functions. The first option is probably the clearest in terms of what’s happening, but it results in the position of the object being calculated three times: once with the initial x and y values set to 50; once when it’s translated back to (0, 0) prior to the skew; then a final time when an inverse translation is applied to put the skewed version back into place. | + | I’m going to immediately dismiss the last option, as it requires far too much mathematics. But it does, perhaps, explain why Inkscape always uses matrix() rather than the named skew functions. The first option is probably the clearest in terms of what’s happening, but it results in the position of the object being calculated three times: once with the initial x and y values set to 50; once when it’s translated back to (0, 0) prior to the skew; then a final time when an inverse translation is applied to put the skewed version back into place.** |
- | The best compromise for this project is the second approach. Simply changing the x and y values in the SVG, however, means that the source file no longer holds the “true” values for the coordinates, so if the JavaScript fails to load for some reason the square will be positioned at the top left, rather than in the middle of the screen. A slight modification to this approach – and the one we’ll take here – is to leave the SVG file untouched, but change the x and y values to zero at the start of the JS file. That way, if the script fails to load you still get a stationary fallback image with the square in the right place, but if everything loads correctly, the JS immediately changes the object’s coordinates to make for less work in the rest of the code. The first step is therefore to modify the start of the initialise() function to get a handle to the < | + | Il y a quelques manières de résoudre ce casse-tête : |
+ | ••Utiliser la fonction translate() pour déplacer l' | ||
+ | ••Changer les attributs | ||
+ | ••Utiliser une fonction matrix() plutôt que les fonctions skewX() et skewY(), car elle combine la déformation et la translation en un seul appel. | ||
- | We use group.querySelector() to get the rectangle as it limits the search to descendants of the group object (compared with document.querySelector() which would search the whole document), and it makes it easy to replace the argument with an ID or class selector when working with a more complex drawing, or with a different element selector if we change the content of the group to be a different type of object | + | Je vais abandonner immédiatement la dernière option, car elle demande trop de maths. Mais elle explique bien, peut-être, pourquoi Inkscape utilise toujours matrix() plutôt que les fonctions skew. La première option est probablement la plus claire sur ce qui se passe, mais, avec elle, la position de l' |
- | Reloading the page at this point shows that, if anything, we’ve made the problem worse! Now the square | + | **The best compromise for this project is the second approach. Simply changing the x and y values in the SVG, however, means that the source file no longer holds the “true” values for the coordinates, so if the JavaScript fails to load for some reason |
- | Reload | + | We use group.querySelector() to get the rectangle as it limits |
- | Note that the transformations are actually applied in reverse order: first the skewY(), then skewX(), then rotate() then finally translate(). When all we had was rotate and skew functions the order made little difference, but adding the translate() makes a huge difference. Put it at the end of the list, and we’re back to the same problem with the square zooming around as it skews. At the start of the list, however, we’ve got a nicely controlled square, rotating and skewing whilst never leaving the middle of the screen. | + | Le meilleur compromis pour ce projet est la seconde approche. Cependant, le simple changement des valeurs de x et y dans SVG signifie que le fichier source ne contient plus les « vraies » valeurs des coordonnées ; aussi, si JavaScript n' |
- | To complete our set of transform functions, let’s add a scale(), so that our square also grows and shrinks. This function can take one or two parameters to indicate the scale factor: if only one is provided then the object is scaled equally in both the x and y directions; if you wish to scale the two directions differently, | + | |
- | Note, however, that there’s no parameter for the center point of the scaling operation. As with the skew functions, your object has to be positioned with its center at (0, 0) if you don’t want it to move as well as change size. Since we’ve already handled this problem for skewing, we just need to ensure that our scale() function is put into the transform attribute after the translate(), to ensure that the scale operation is performed before the translation. | + | Nous utilisons group.querySelector() pour trouver le rectangle car il limite ses recherches aux descendants de l' |
- | To make our animation more interesting | + | **Reloading the page at this point shows that, if anything, |
- | Like the skew functions we also want to animate from the minimum to the maximum, then back again – as opposed to continuously going in one direction as we did with rotate(). We therefore need a couple of blocks of code to calculate the relevant value at any given time point, changing direction after each period. Below is the code for the x-axis scaling – compare it to the equivalent block for skewX() from last month and you should be able to reproduce code for the y-axis scaling yourself. | + | Reload the page and we’ve got what we expected: a square that rotates and skews in-place at the top-left of the screen. Our last step is to move it back to (50, 50) with an extra translate() step inserted to the start of the list, whose values are hard-coded (next page, top right).** |
+ | |||
+ | En rechargeant maintenant la page, nous voyons, en fait, que les choses ont empirées ! Maintenant, le carré tourne sur une étendue encore plus grande qu' | ||
+ | |||
+ | Rechargez la page et nous obtenons ce que nous attendions : un carré qui tourne et se déforme sur place en haut à gauche de l' | ||
+ | |||
+ | **Note that the transformations are actually applied in reverse order: first the skewY(), then skewX(), then rotate() then finally translate(). When all we had was rotate and skew functions the order made little difference, but adding the translate() makes a huge difference. Put it at the end of the list, and we’re back to the same problem with the square zooming around as it skews. At the start of the list, however, we’ve got a nicely controlled square, rotating and skewing whilst never leaving the middle of the screen. | ||
+ | |||
+ | To complete our set of transform functions, let’s add a scale(), so that our square also grows and shrinks. This function can take one or two parameters to indicate the scale factor: if only one is provided then the object is scaled equally in both the x and y directions; if you wish to scale the two directions differently, | ||
+ | |||
+ | Notez que les transformations sont réellement appliquées dans l' | ||
+ | |||
+ | Pour compléter notre ensemble de fonctions de transformation, | ||
+ | |||
+ | **Note, however, that there’s no parameter for the center point of the scaling operation. As with the skew functions, your object has to be positioned with its center at (0, 0) if you don’t want it to move as well as change size. Since we’ve already handled this problem for skewing, we just need to ensure that our scale() function is put into the transform attribute after the translate(), | ||
+ | |||
+ | To make our animation more interesting we’re going to animate the change in x and y scale separately, over different time periods but within the same range of 0.1 (one tenth of the width or height) to 3.0 (treble the width or height). We’ll encompass these parameters as two more sets of properties in the group.animProperties object (middle right).** | ||
+ | |||
+ | Notez, cependant, qu'il n'y a pas de paramètre pour un point central de l' | ||
+ | |||
+ | Pour rendre notre animation plus intéressante, | ||
+ | |||
+ | **Like the skew functions we also want to animate from the minimum to the maximum, then back again – as opposed to continuously going in one direction as we did with rotate(). We therefore need a couple of blocks of code to calculate the relevant value at any given time point, changing direction after each period. Below is the code for the x-axis scaling – compare it to the equivalent block for skewX() from last month and you should be able to reproduce code for the y-axis scaling yourself. | ||
And, of course, we need to add our scale() function and two new parameters to the transform attribute (top right). | And, of course, we need to add our scale() function and two new parameters to the transform attribute (top right). | ||
At last we have our object rotating, skewing and scaling, all while centered in the browser window – though a static screenshot doesn’t really do it justice. | At last we have our object rotating, skewing and scaling, all while centered in the browser window – though a static screenshot doesn’t really do it justice. | ||
- | I’m going to finish this month with a couple of exercises for you to try, which build on the animation we’ve created over these previous few articles: | ||
- | • Our final transform attribute has a fixed translate() function to position the square in the middle of the screen. Why not add another two sets of parameters to also animate the x and y position, causing the square to move around the window a little as well. Setting min and max values either side of 50 means you can replace the hard-coded coordinates in the existing translate(). Or you could have a negative min and positive max, then use the values in a second translate() function – but be careful of the ordering! | + | I’m going to finish this month with a couple of exercises for you to try, which build on the animation we’ve created over these previous few articles: |
- | • Try replacing the contents of the group with something else. It could be a more interesting single shape, such as a star or more faceted polygon, but it could also be any other Inkscape drawing – with multiple shapes and colors. Simply replacing the < | + | |
+ | Comme pour les fonctions skew, nous voulons les animer entre un minimum et un maximum, puis revenir - contrairement au mouvement dans un seul sens que nous avions avec rotate(). Par conséquent, | ||
+ | |||
+ | Et, bien sûr, nous avons besoin d' | ||
+ | |||
+ | Enfin, nous avons un objet qui tourne, se déforme et change d' | ||
+ | |||
+ | Je terminerai ce mois avec quelques exercices que vous essaierez, qui s' | ||
+ | |||
+ | **• Our final transform attribute has a fixed translate() function to position the square in the middle of the screen. Why not add another two sets of parameters to also animate the x and y position, causing the square to move around the window a little as well. Setting min and max values either side of 50 means you can replace the hard-coded coordinates in the existing translate(). Or you could have a negative min and positive max, then use the values in a second translate() function – but be careful of the ordering! | ||
+ | • Try replacing the contents of the group with something else. It could be a more interesting single shape, such as a star or more faceted polygon, but it could also be any other Inkscape drawing – with multiple shapes and colors. Simply replacing the < | ||
+ | |||
+ | ••Notre attribut transform final a une fonction translate() fixe pour positionner le carré au milieu de l' | ||
+ | ••Essayez de remplacer le contenu du groupe par quelque chose d' | ||
+ | |||
+ | **The most important thing to remember is that this animation code – and the JS that we used previously in this series – are just examples to get you going. There’s no reason why you can’t create an animation that messes with the transform attribute whilst at the same time altering the fill and stroke, or manipulating the “d” attribute of a < | ||
- | The most important thing to remember is that this animation | + | La chose la plus importante à se rappeler est que le code de cette animation - et le JS que nous avons utilisé précédemment dans cette série - ne sont que des exemples pour vous aider à démarrer rapidement. Il n'y a aucune raison pour que vous ne puissiez pas créer une animation |
issue153/inkscape.1580574331.txt.gz · Dernière modification : 2020/02/01 17:25 de d52fr