issue138:inkscape
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue138:inkscape [2018/10/27 14:21] – créée auntiee | issue138:inkscape [2018/11/11 16:08] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | A quick summary: over the past few instalments, | + | **A quick summary: over the past few instalments, |
- | That’s where SMIL comes in. It’s an older specification for animation, created around the same time as the original SVG specs, and does provide the ability to animate arbitrary attributes in SVG. It can animate the “d” attribute that makes up the shape of a path, for example – a task that can only otherwise be accomplished in the browser via JavaScript. But although SMIL is more powerful than CSS in this regard, it has never been adopted in any Microsoft browser, leading to its inexorable decline and a likely deprecation in the browsers that do support it. | + | That’s where SMIL comes in. It’s an older specification for animation, created around the same time as the original SVG specs, and does provide the ability to animate arbitrary attributes in SVG. It can animate the “d” attribute that makes up the shape of a path, for example – a task that can only otherwise be accomplished in the browser via JavaScript. But although SMIL is more powerful than CSS in this regard, it has never been adopted in any Microsoft browser, leading to its inexorable decline and a likely deprecation in the browsers that do support it.** |
- | Given this situation, we won’t look any further at SMIL, but there are still a few tricks that can be performed with CSS that are worthy of further investigation. This time, we’re going to look at an increasingly popular animation technique which is really the result of taking a simple feature of SVG and pushing it in ways it wasn’t really intended for. It’s commonly referred to as “line animation”, although the line itself doesn’t change shape (remember, that can’t be done with CSS yet). It’s also sometimes referred to as a “self-drawing” image, which is a bit more descriptive: | + | Résumé rapide : durant les quelques articles passés, nous avons regardé certaines techniques d' |
- | The first thing we’ll need is a path to animate. It should not have a fill, but will require a stroke. For this demonstration I’ve used Inkscape’s star tool to create an eight pointed star, then converted it to a path and roughly moved every second point inwards to give more of a cartoon gunshot appearance. I’ve given it a stroke-width of 2px and saved it using the “Optimised SVG” option in order to strip out most of the excess baggage from the file, just so that this tutorial isn’t filled with several pages of code. | + | C'est là que SMIL entre en scène. C'est une spécification ancienne pour l' |
- | Opening the file in a text editor reveals a structure like that shown below. | + | **Given this situation, we won’t look any further at SMIL, but there are still a few tricks that can be performed with CSS that are worthy of further investigation. This time, we’re going to look at an increasingly popular animation technique which is really the result of taking a simple feature of SVG and pushing it in ways it wasn’t really intended for. It’s commonly referred to as “line animation”, |
+ | |||
+ | The first thing we’ll need is a path to animate. It should not have a fill, but will require a stroke. For this demonstration I’ve used Inkscape’s star tool to create an eight pointed star, then converted it to a path and roughly moved every second point inwards to give more of a cartoon gunshot appearance. I’ve given it a stroke-width of 2px and saved it using the “Optimised SVG” option in order to strip out most of the excess baggage from the file, just so that this tutorial isn’t filled with several pages of code.** | ||
+ | |||
+ | Étant donné la situation, nous n' | ||
+ | |||
+ | La première chose dont nous aurons besoin est un chemin à animer. Il ne devra pas avoir de remplissage, | ||
+ | |||
+ | **Opening the file in a text editor reveals a structure like that shown below. | ||
As you can see, it’s a pretty simple file. Even the <g> isn’t really needed, but it’s easier to leave it in place with its own transform, rather than try to manipulate the coordinates of the path to compensate for its removal. | As you can see, it’s a pretty simple file. Even the <g> isn’t really needed, but it’s easier to leave it in place with its own transform, rather than try to manipulate the coordinates of the path to compensate for its removal. | ||
- | The secret to this animation technique is in the fact that SVG has a nice, simple, CSS-animatable way of drawing dashed and dotted strokes, rather than solid ones. The next step, therefore, is to introduce some dashes, defined in CSS. Note that you’ll probably have to also remove the corresponding properties from the path’s “style” attribute if you saved as a normal Inkscape file. | + | The secret to this animation technique is in the fact that SVG has a nice, simple, CSS-animatable way of drawing dashed and dotted strokes, rather than solid ones. The next step, therefore, is to introduce some dashes, defined in CSS. Note that you’ll probably have to also remove the corresponding properties from the path’s “style” attribute if you saved as a normal Inkscape file.** |
- | Save the file (top right) and load it directly into a web browser, and you should see something like this: | + | L' |
+ | |||
+ | Comme vous pouvez le voir, c'est un fichier assez simple. Même le <g> n'est pas vraiment nécessaire, | ||
+ | |||
+ | Le secret de cette technique d' | ||
+ | |||
+ | **Save the file (top right) and load it directly into a web browser, and you should see something like this: | ||
The values we set in the stroke-dasharray property are used to determine the lengths of the drawn segments and gaps along the path. With our value of “10 2 3 5” the stroke will be drawn as a line segment of 10 units, followed by a gap of 2 units, then another line of 3 units and a gap of 5 units. The sequence then automatically repeats, so the next line segment is 10 units long and the next gap is 2. It runs around the path uninterrupted by corners, so a line segment (or gap) of 10 might appear as 3 units on one side of a corner and 7 on the other – look at the leftmost corner of the shape for a good example of this. If you follow the line round, taking that into account, you can see that the stroke dashes do follow the pattern we’ve set. | The values we set in the stroke-dasharray property are used to determine the lengths of the drawn segments and gaps along the path. With our value of “10 2 3 5” the stroke will be drawn as a line segment of 10 units, followed by a gap of 2 units, then another line of 3 units and a gap of 5 units. The sequence then automatically repeats, so the next line segment is 10 units long and the next gap is 2. It runs around the path uninterrupted by corners, so a line segment (or gap) of 10 might appear as 3 units on one side of a corner and 7 on the other – look at the leftmost corner of the shape for a good example of this. If you follow the line round, taking that into account, you can see that the stroke dashes do follow the pattern we’ve set. | ||
- | This automatic repeating of values in the stroke-dasharray property produces a couple of neat features. The first is that any odd number of entries is effectively the same as writing a value that is twice as long, with the line and gap values swapped in the second half. So a value of “10 5 10” is the same as writing “10 5 10 10 5 10” – in other words a long line, short gap, long line then a long gap, short line, long gap. Notice that the length of the lines in the first half make up the length of the gaps in the second, and vice versa. | + | This automatic repeating of values in the stroke-dasharray property produces a couple of neat features. The first is that any odd number of entries is effectively the same as writing a value that is twice as long, with the line and gap values swapped in the second half. So a value of “10 5 10” is the same as writing “10 5 10 10 5 10” – in other words a long line, short gap, long line then a long gap, short line, long gap. Notice that the length of the lines in the first half make up the length of the gaps in the second, and vice versa.** |
- | The other feature – and the one we’re interested in here – is that you can collapse the value down to a single number to get equal line and gap lengths. A lone value of “10” expands to “10 10”, giving you a repeating series of lines that are 10 units long, followed by gaps that are 10 units long. In my example file, that gives you something like this: | + | Sauvegardez le fichier (en haut à droite), chargez-le directement dans le navigateur Web et vous devriez voir quelque chose comme ceci : |
+ | |||
+ | Les valeurs que nous avons réglées dans la propriété stroke-dasharray (contour en pointillé) sont utilisées pour déterminer les longueurs des segments dessinés et des espaces le long du chemin. Avec notre valeur de « 10 2 3 5 », le trait sera dessiné comme un segment de ligne de 10 unités, suivi d'un espace de 2 unités, puis une autre ligne de 3 unités et un espace de 5 unités. La séquence est automatiquement répétée ensuite, de sorte que le segment suivant a 10 unités de long et l' | ||
+ | |||
+ | Cette répétition automatique des valeurs de la propriété stroke-dasharray produit une paire de fonctionnalités bien sympas. La première est que tout nombre impair de valeurs saisies est en fait identique à la saisie d'une valeur deux fois plus grande, avec la permutation dans la deuxième partie des valeurs de lignes et d' | ||
+ | |||
+ | **The other feature – and the one we’re interested in here – is that you can collapse the value down to a single number to get equal line and gap lengths. A lone value of “10” expands to “10 10”, giving you a repeating series of lines that are 10 units long, followed by gaps that are 10 units long. In my example file, that gives you something like this: | ||
Now consider what happens if we increase the value. As the number goes up, so the visible line segments get longer – but so do the gaps. Below is a sequence of screenshots of the same image with the stroke-dasharray value set to 10, 20, 50, 100 and 500. | Now consider what happens if we increase the value. As the number goes up, so the visible line segments get longer – but so do the gaps. Below is a sequence of screenshots of the same image with the stroke-dasharray value set to 10, 20, 50, 100 and 500. | ||
- | By the time we get to 500 our first visible line segment almost covers the entire path. If we were to continue increasing the value until it is the same as the path length, it would effectively render it the same as if we didn’t have the stroke-dasharray at all. And that’s exactly what we want to do! | + | By the time we get to 500 our first visible line segment almost covers the entire path. If we were to continue increasing the value until it is the same as the path length, it would effectively render it the same as if we didn’t have the stroke-dasharray at all. And that’s exactly what we want to do!** |
- | You might be thinking it’s a little pointless to add a CSS property, then set it to a value that appears to have no effect, but remember that as well as the drawn line, there’s an equivalent length of gap that’s not visible because it extends beyond the end of the path. By adding another CSS property, however, we can adjust the start point of the drawn dashes, bringing our gap into play. | + | L' |
+ | |||
+ | Regardez maintenant ce qui se passe si vous augmentez la valeur. Plus le nombre croît, plus les segments de ligne grandissent, | ||
+ | |||
+ | Quand nous arrivons à 500, notre premier segment de ligne visible couvre à peu près tout le chemin. Si vous continuez à augmenter la valeur jusqu' | ||
+ | |||
+ | **You might be thinking it’s a little pointless to add a CSS property, then set it to a value that appears to have no effect, but remember that as well as the drawn line, there’s an equivalent length of gap that’s not visible because it extends beyond the end of the path. By adding another CSS property, however, we can adjust the start point of the drawn dashes, bringing our gap into play. | ||
The stroke-dashoffset property tells the browser to adjust its calculations to begin further into your stroke-dasharray sequence (note that the “px” here isn’t the same as screen pixels; rather it’s in the units of the SVG file, which are likely to be different to screen pixels due to scaling). If your dasharray is “10 5” and you set the offset to 3px then the first segment drawn will be only 7 units long, then a gap of 5, then a line of 10 and the rest continues as normal. | The stroke-dashoffset property tells the browser to adjust its calculations to begin further into your stroke-dasharray sequence (note that the “px” here isn’t the same as screen pixels; rather it’s in the units of the SVG file, which are likely to be different to screen pixels due to scaling). If your dasharray is “10 5” and you set the offset to 3px then the first segment drawn will be only 7 units long, then a gap of 5, then a line of 10 and the rest continues as normal. | ||
- | In practice this means that setting a positive value here will “swallow” that much of the start of the stroke array, whereas a negative value will “push” the stroke array further along the path. | + | In practice this means that setting a positive value here will “swallow” that much of the start of the stroke array, whereas a negative value will “push” the stroke array further along the path.** |
- | By adjusting the value of stroke-dashoffset we can “slide” the drawn portion of the stroke along the path. Here’s what it looks like with values of 0px, 100px, 200px, 300px. Notice that progressively larger numbers swallow more of the path, showing more of the gap at the end. | + | Vous pourriez penser que c'est sans intérêt d' |
+ | |||
+ | La propriété stroke-dashoffset (décalage du contour en pointillé) indique au navigateur qu'il doit ajuster ses calculs pour commencer plus avant dans la séquence de stroke-dasharray (notez que le « px » est ici différent des pixels de l' | ||
+ | |||
+ | En pratique, cela signifie qu'ici une valeur positive « avalera » d' | ||
+ | |||
+ | **By adjusting the value of stroke-dashoffset we can “slide” the drawn portion of the stroke along the path. Here’s what it looks like with values of 0px, 100px, 200px, 300px. Notice that progressively larger numbers swallow more of the path, showing more of the gap at the end. | ||
As you know, setting the dasharray to the same value as the length of the path will draw the entire path. But also setting the dashoffset to that value will then swallow the drawn segment, leaving only the gap (which is also the length of the path). In other words, our path will disappear completely. | As you know, setting the dasharray to the same value as the length of the path will draw the entire path. But also setting the dashoffset to that value will then swallow the drawn segment, leaving only the gap (which is also the length of the path). In other words, our path will disappear completely. | ||
- | With the drawn segment completely swallowed, we’re at the starting point for our animation: our path is not visible on the screen. By decreasing the dashoffset value we can make the drawn segment creep onto the screen, following the shape of the path. All we need to do, therefore, is to animate the stroke-dashoffset value (see part 75 if you need a reminder of the animation syntax): | + | With the drawn segment completely swallowed, we’re at the starting point for our animation: our path is not visible on the screen. By decreasing the dashoffset value we can make the drawn segment creep onto the screen, following the shape of the path. All we need to do, therefore, is to animate the stroke-dashoffset value (see part 75 if you need a reminder of the animation syntax):** |
- | Reload the file in your browser and you should see the effect we’re looking for – and because it’s all just CSS animation embedded directly in the SVG file, it will even work when the file is pulled into a web page via an <img> tag. | + | En ajustant la valeur de stroke-dashoffset, |
+ | |||
+ | Comme vous le savez, le réglage de dasharray à la même valeur que la longueur du chemin dessinera tout ce chemin. Mais si dashoffset est aussi réglé à la même valeur, le segment complet sera avalé, ne laissant apparaître que l' | ||
+ | |||
+ | Quand le segment dessiné est complètement avalé, nous sommes au début de notre animation : notre chemin n'est pas visible sur l' | ||
+ | |||
+ | **Reload the file in your browser and you should see the effect we’re looking for – and because it’s all just CSS animation embedded directly in the SVG file, it will even work when the file is pulled into a web page via an <img> tag. | ||
Just a minute! Where did that value of 575 for the dasharray and dashoffset come from? As you’ve probably realised, that’s the total length of my path. You could theoretically get it from Inkscape via the Extensions > Visualise Path > Measure Path… extension, but that throws a Python error on my system. Alternatively you could just use trial and error: increasing the value of the dasharray until it just fills the path (which is quite easily done using the browser developer tools, if you’re familiar with them). | Just a minute! Where did that value of 575 for the dasharray and dashoffset come from? As you’ve probably realised, that’s the total length of my path. You could theoretically get it from Inkscape via the Extensions > Visualise Path > Measure Path… extension, but that throws a Python error on my system. Alternatively you could just use trial and error: increasing the value of the dasharray until it just fills the path (which is quite easily done using the browser developer tools, if you’re familiar with them). | ||
- | The simplest option, though, is to ask the browser to do the hard work for you by invoking a little JavaScript. There is a method on the < | + | The simplest option, though, is to ask the browser to do the hard work for you by invoking a little JavaScript. There is a method on the < |
+ | |||
+ | Rechargez votre fichier dans le navigateur et vous devriez voir l' | ||
+ | |||
+ | Attendez une minute ! D'où vient la valeur de 575 mise dans dasharray et dashoffset ? Comme vous l'avez probablement réalisé, c'est la longueur totale du chemin. Vous pouvez théoriquement la trouver dans Inkscape via l' | ||
+ | |||
+ | Toutefois, l' | ||
+ | |||
+ | **Remember that JavaScript won’t run when the document is loaded via the <img> tag, so to use the getTotalLength() method you will have to load the SVG file directly into the browser. You only need to do it once, then you can simply hard-code the value into the < | ||
+ | |||
+ | There is also an alternative to specifying the actual length of the path. You could add a “pathLength” attribute to the path, set it to a value of your choosing, then treat that value as the total length in the CSS. This basically tells the browser “I want to pretend this path is 100 units long, even though we both know it’s not, so can you just do the maths for me as needed”. A good compromise is to use JavaScript to find the actual length, then round it to the next whole number and set that value in the pathLength attribute and the CSS. That way any browser that understands pathLength will use it to give you a precise animation, and those that don’t are still using a value that’s close enough to work in most cases.** | ||
+ | |||
+ | Souvenez-vous que Javascript ne se lance pas quand le document est chargé via la balise < | ||
+ | |||
+ | Il y a aussi une autre option pour spécifier la longueur réelle du chemin. Vous pouvez ajouter un attribut « pathLength » (longueur du chemin) au chemin, le régler à une valeur de votre choix, puis, dans le CSS, traiter cette valeur comme étant la longueur totale. Ceci dit en gros au navigateur « Je veux affirmer que ce chemin a 100 unités de long, même si nous savons tous les deux que ce n'est pas vrai ; aussi, pouvez-vous faire les calculs pour moi si nécessaire. » Un bon compromis consiste à utiliser Javascript pour trouver la longueur réelle, puis l' | ||
+ | |||
+ | **There’s one final thing to note with this approach. So far I’ve used the version of the technique you’ll most commonly find documented online, but, according to SVG expert Amelia Bellamy-Royds, | ||
- | Remember that JavaScript won’t run when the document is loaded via the <img> tag, so to use the getTotalLength() method you will have to load the SVG file directly into the browser. You only need to do it once, then you can simply hard-code | + | If you want to play around with this effect, but have the computer do a little more of the hard work for you, a discussion of this technique over on inkscapeforum.com led to one user creating an Inkscape extension that will add the CSS animation code for you. You can find the extension at https:// |
- | There is also an alternative to specifying the actual length of the path. You could add a “pathLength” attribute to the path, set it to a value of your choosing, then treat that value as the total length in the CSS. This basically tells the browser “I want to pretend this path is 100 units long, even though we both know it’s not, so can you just do the maths for me as needed”. A good compromise is to use JavaScript to find the actual length, then round it to the next whole number and set that value in the pathLength attribute and the CSS. That way any browser that understands pathLength will use it to give you a precise | + | Une dernière chose est à noter dans cette approche. Jusqu' |
- | There’s one final thing to note with this approach. So far I’ve used the version of the technique you’ll most commonly find documented online, but, according to SVG expert Amelia Bellamy-Royds, | + | Si vous voulez tester cet effet, mais que vous voulez que l' |
- | If you want to play around | + | **Just animating a star being drawn is a little dull, but does show you the basic approach. Although the star contains no curves, this method works equally well with any shape of path. An easy step on from this, for example, would be to replace |
- | Just animating a star being drawn is a little dull, but does show you the basic approach. Although the star contains | + | If you’re prepared to spend more time hand-crafting your animations there’s |
- | If you’re prepared to spend more time hand-crafting your animations there’s no reason why you shouldn’t produce a self-drawing SVG image consisting of multiple paths, each animated separately using delays to ensure that they appear in the correct order. Once your outline is drawn, some more delayed animations could fade in the fill colours, erase some of the lines, or cross-fade to a raster image. With time and effort, this technique can produce some spectacular results, and all just by moving a dashed line around. | + | La simple animation du dessin d'une étoile est un peu terne, mais vous montre l' |
+ | Si vous êtes prêt à passer plus de temps à ajuster vos animations à la main, il n'y a aucune raison pour que vous n' |
issue138/inkscape.1540642881.txt.gz · Dernière modification : 2018/10/27 14:21 de auntiee