issue138:inkscape
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue138:inkscape [2018/11/08 09:01] – d52fr | issue138:inkscape [2018/11/11 16:08] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 3: | Ligne 3: | ||
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.** | ||
- | Résumé rapide : durant les quelques articles passés, nous avons regardé | + | Résumé rapide : durant les quelques articles passés, nous avons regardé |
- | C'est là que SMIL entre en scène. C'est une spécification ancienne pour l' | + | C'est là que SMIL entre en scène. C'est une spécification ancienne pour l' |
**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”, | **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”, | ||
Ligne 11: | Ligne 11: | ||
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.** | 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 | + | Étant |
- | La première chose dont nous aurons besoin est un chemin à animer. Il ne devra pas avoir de remplissage, | + | 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. | **Opening the file in a text editor reveals a structure like that shown below. | ||
Ligne 25: | Ligne 25: | ||
Comme vous pouvez le voir, c'est un fichier assez simple. Même le <g> n'est pas vraiment nécessaire, | Comme vous pouvez le voir, c'est un fichier assez simple. Même le <g> n'est pas vraiment nécessaire, | ||
- | Le secret de cett etechnique | + | Le secret de cette technique |
**Save the file (top right) and load it directly into a web browser, and you should see something like this: | **Save the file (top right) and load it directly into a web browser, and you should see something like this: | ||
Ligne 32: | Ligne 32: | ||
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.** | ||
- | Sauvegardez le fichier (en haut à droite), chargez-le directement dans le navigateur Web et vous devriez voir quelque chose comme ça : | + | 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' | + | 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 nettes. La première est que tout nombre impair de valeurs saisies est dans les faits identique à la saisie d'une valeur deux fois plus grande, avec les valeurs de lignes et d' | + | 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: | **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: | ||
Ligne 44: | Ligne 44: | ||
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!** | ||
- | L' | + | L' |
- | Regardez maintenant ce qui se passe si vous augmentez la valeur. Plus le nombre croît, plus les segments de ligne grandissent | + | Regardez maintenant ce qui se passe si vous augmentez la valeur. Plus le nombre croît, plus les segments de ligne grandissent, c' |
- | Quand nous arrivons à 500, notre premier segment de ligne visible couvre à peu près tout le chemin. Si vous continuiez | + | Quand nous arrivons à 500, notre premier segment de ligne visible couvre à peu près tout le chemin. Si vous continuez |
**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. | **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. | ||
Ligne 56: | Ligne 56: | ||
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.** | ||
- | Vous pourriez penser que c' | + | 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' | + | 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, | + | En pratique, |
**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. | **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. | ||
Ligne 68: | Ligne 68: | ||
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):** | ||
- | En ajustant la valeur de stroke-dashoffset, | + | 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' | 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' | + | 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. | **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. | ||
Ligne 80: | Ligne 80: | ||
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' | + | 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' | + | 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' | + | 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 < | **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.** | 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, | **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, | ||
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:// | 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:// | ||
+ | |||
+ | Une dernière chose est à noter dans cette approche. Jusqu' | ||
+ | |||
+ | Si vous voulez tester cet effet, mais que vous voulez que l' | ||
**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 the star with a path for some handwritten text, to produce a “self-writing” effect. Be aware, however, that sharp transitions can cause rendering artefacts depending on the miter limit. In the case of my star, for example, the corner at the start/end of the line flashes on and off as the rest of the line is drawn. It’s often better to use rounded corners and end caps to avoid this, which also helps to reinforce the illusion of the line being drawn by a pen or pencil. | **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 the star with a path for some handwritten text, to produce a “self-writing” effect. Be aware, however, that sharp transitions can cause rendering artefacts depending on the miter limit. In the case of my star, for example, the corner at the start/end of the line flashes on and off as the rest of the line is drawn. It’s often better to use rounded corners and end caps to avoid this, which also helps to reinforce the illusion of the line being drawn by a pen or pencil. | ||
Ligne 98: | Ligne 106: | ||
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.** | 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.1541664096.txt.gz · Dernière modification : 2018/11/08 09:01 de d52fr