issue135: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 | ||
issue135:inkscape [2018/08/13 07:41] – d52fr | issue135:inkscape [2018/08/15 17:47] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 3: | Ligne 3: | ||
Speaking of simple raster graphics, that’s the first of our four ways of including an image, and it’s one that I’m going to dismiss immediately. Once you convert your SVG to a raster graphic, be it a png, jpeg, or gif, it becomes no different in capability to a photo from a digital camera. You can display it in your web page, but that’s about it. Sure, with modern CSS and JavaScript you can make it respond to mouse events, and can use some SVG-style techniques such as masking, clipping and (basic) filtering – but none of those abilities come from the image itself. Your raster image is essentially a rectangle that can only be modified as a single entity, losing all notion of the individual objects in your original SVG file.** | Speaking of simple raster graphics, that’s the first of our four ways of including an image, and it’s one that I’m going to dismiss immediately. Once you convert your SVG to a raster graphic, be it a png, jpeg, or gif, it becomes no different in capability to a photo from a digital camera. You can display it in your web page, but that’s about it. Sure, with modern CSS and JavaScript you can make it respond to mouse events, and can use some SVG-style techniques such as masking, clipping and (basic) filtering – but none of those abilities come from the image itself. Your raster image is essentially a rectangle that can only be modified as a single entity, losing all notion of the individual objects in your original SVG file.** | ||
- | Ayant regardé les quatre façons principales d' | + | Ayant regardé les quatre façons principales d' |
- | À propos des images graphiques simples raster, c'est la première des quatre manières d' | + | À propos des images graphiques simples raster, c'est la première des quatre manières d' |
**So let’s move on to the second way of including an image: by linking directly to the SVG file. For this example, we’re going to use the following simple HTML page, which just contains a link to our SVG file in an <img> tag: | **So let’s move on to the second way of including an image: by linking directly to the SVG file. For this example, we’re going to use the following simple HTML page, which just contains a link to our SVG file in an <img> tag: | ||
Ligne 22: | Ligne 22: | ||
As for the SVG file, it’s just a simple square, drawn in Inkscape, and centered in a square page.** | As for the SVG file, it’s just a simple square, drawn in Inkscape, and centered in a square page.** | ||
+ | |||
+ | Aussi, passons à la deuxième façon d' | ||
+ | |||
+ | < | ||
+ | |||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | <img src=" | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Quant au fichier SVG, ce n'est qu'un simple carré, dessiné dans Inkscape et centré dans une page carrée. | ||
**I’m going to wade in and add some handwritten CSS to this file, so, for the sake of clarity (not to mention space), I’ve saved the image as an Optimized SVG. Everything I’m going to do would also work on an Inkscape SVG, but if you’re not familiar with editing XML files, it’s probably worth using optimized files, at least at first, so that you don’t have a load of extra elements and namespaces getting in the way. My optimized file looks like the codew shown below (with a few line breaks added for clarity – they won’t affect the image). | **I’m going to wade in and add some handwritten CSS to this file, so, for the sake of clarity (not to mention space), I’ve saved the image as an Optimized SVG. Everything I’m going to do would also work on an Inkscape SVG, but if you’re not familiar with editing XML files, it’s probably worth using optimized files, at least at first, so that you don’t have a load of extra elements and namespaces getting in the way. My optimized file looks like the codew shown below (with a few line breaks added for clarity – they won’t affect the image). | ||
A hand-coded version of this image could be even smaller – there would be no need for a <g> with a transform when the x and y coordinates of the < | A hand-coded version of this image could be even smaller – there would be no need for a <g> with a transform when the x and y coordinates of the < | ||
+ | |||
+ | Je vais intervenir pour ajouter un peu de CSS à la main dans ce fichier ; aussi, pour plus de clarté (pour ne pas citer l' | ||
+ | |||
+ | Une version codée à la main de cette image pourrait même être plus petite, il n'y aurait aucun besoin d'un <g> avec une transformation si les coordonnées x et y du < | ||
**With my SVG file created and saved into the same directory as the HTML document, loading the latter into a web browser gives exactly the result you would expect: a web page with a square in it. So far, we haven’t really gained a lot over using a bitmap. Yes, technically it retains a better quality when scaled, and SVG files are often (though not always) smaller than their raster equivalents – but in many cases those are modest benefits at best. | **With my SVG file created and saved into the same directory as the HTML document, loading the latter into a web browser gives exactly the result you would expect: a web page with a square in it. So far, we haven’t really gained a lot over using a bitmap. Yes, technically it retains a better quality when scaled, and SVG files are often (though not always) smaller than their raster equivalents – but in many cases those are modest benefits at best. | ||
But, even when used in an < | But, even when used in an < | ||
+ | |||
+ | Mon fichier SVG étant créé et sauvegardé dans le même répertoire que le document HTML, charger ce dernier dans un navigateur Web donne exactement le résultat auquel vous vous attendez : une page Web avec un carré dedans. Pour l' | ||
+ | |||
+ | Mais, même lors d'une utilisation dans un < | ||
+ | |||
**If you’re not familiar with CSS, then here’s what we’ve done: first there’s a pair of opening and closing < | **If you’re not familiar with CSS, then here’s what we’ve done: first there’s a pair of opening and closing < | ||
After the selector come the rules, in a block between a pair of braces (the “{” and “}” characters). We’ve got only one rule at the moment: to make the fill blue (using hexadecimal RGB syntax for the color). So, let’s reload the page and take a look at our…red square.** | After the selector come the rules, in a block between a pair of braces (the “{” and “}” characters). We’ve got only one rule at the moment: to make the fill blue (using hexadecimal RGB syntax for the color). So, let’s reload the page and take a look at our…red square.** | ||
+ | |||
+ | Si vous n' | ||
+ | |||
+ | Après le sélecteur viennent les règles, dans un bloc entre deux accolades (les caractères « { » et « } »). Nous n'y avons mis qu'une seule règle pour l' | ||
**As you may already know, CSS stands for “Cascading Style Sheets”, and it’s the “cascading” part that has interfered with our plans. Without going into all the gory details, it’s possible to have more than one source of CSS rules affecting an element, and the details of the cascade dictate which rule will take precedence over the others. In almost all cases, if there’s a rule directly placed on an element using a “style” attribute, that will win. So, in our case, if we just remove the “fill:# | **As you may already know, CSS stands for “Cascading Style Sheets”, and it’s the “cascading” part that has interfered with our plans. Without going into all the gory details, it’s possible to have more than one source of CSS rules affecting an element, and the details of the cascade dictate which rule will take precedence over the others. In almost all cases, if there’s a rule directly placed on an element using a “style” attribute, that will win. So, in our case, if we just remove the “fill:# | ||
- | **That’s all well and good, but so far we haven’t really achieved anything that couldn’t have been done just by drawing a blue square in the first place. But what we have done is to move the rule that governs the color of the square from the < | + | Comme vous le savez peut-être déjà, CSS veut dire « Cascading Style Sheets » (Feuilles de style en cascade) et c'est la partie « en cascade » qui a interféré avec nos projets. Sans rentrer dans les détails, il est possible d' |
+ | |||
+ | **That’s all well and good, but so far we haven’t really achieved anything that couldn’t have been done just by drawing a blue square in the first place. But what we have done is to move the rule that governs the color of the square from the < | ||
+ | |||
+ | Tout cela est bel et bien, mais, jusque-là, nous n' | ||
**To begin with, we have to expand our existing set of rules to also include some information about the animation we want to use. At a minimum we have to create a name for the animation (so that you can have more than one in a file), and tell it how long the animation should last. That means our rect selector now looks like this: | **To begin with, we have to expand our existing set of rules to also include some information about the animation we want to use. At a minimum we have to create a name for the animation (so that you can have more than one in a file), and tell it how long the animation should last. That means our rect selector now looks like this: | ||
Ligne 53: | Ligne 86: | ||
100% { fill: #00f; } | 100% { fill: #00f; } | ||
}** | }** | ||
+ | |||
+ | Pour commencer, nous devons étendre notre jeu de règles existant pour inclure aussi des informations sur l' | ||
+ | |||
+ | rect { | ||
+ | fill: #00f; | ||
+ | animation-name: | ||
+ | animation-duration: | ||
+ | } | ||
+ | |||
+ | Vous avez le choix du nom de l' | ||
+ | |||
+ | @keyframes myAnimation { | ||
+ | 0% { fill: #f00; } | ||
+ | 100% { fill: #00f; } | ||
+ | } | ||
**The syntax is pretty straightforward. First we start with the string “@keyframes” to indicate that this is a set of keyframes, followed immediately by the name we defined earlier. Then, inside a block of code between braces, we have individual entries for each keyframe. | **The syntax is pretty straightforward. First we start with the string “@keyframes” to indicate that this is a set of keyframes, followed immediately by the name we defined earlier. Then, inside a block of code between braces, we have individual entries for each keyframe. | ||
A keyframe entry is just the same as any normal block of CSS, except that the selector is replaced with a percentage, representing how far through the animation this block should apply. You pretty much always need at least a block for the start of the animation (0%) and one for the end (100%), but you can add more in-between if you want. Because animations with just two keyframes are so common, you can also use the words “from” and “to” instead of 0% and 100%, which arguably makes the code a little more readable.** | A keyframe entry is just the same as any normal block of CSS, except that the selector is replaced with a percentage, representing how far through the animation this block should apply. You pretty much always need at least a block for the start of the animation (0%) and one for the end (100%), but you can add more in-between if you want. Because animations with just two keyframes are so common, you can also use the words “from” and “to” instead of 0% and 100%, which arguably makes the code a little more readable.** | ||
+ | |||
+ | La syntaxe est assez simple. D' | ||
+ | |||
+ | La saisie d'une trame-clé est la même que pour n' | ||
**So, taking both of the blocks above together, we’ve essentially said “create a set of keyframes called ‘myAnimation’ and run them on the < | **So, taking both of the blocks above together, we’ve essentially said “create a set of keyframes called ‘myAnimation’ and run them on the < | ||
There’s one little caveat to catch: notice that there’s still a fill value on the rect CSS block. At the end of the animation, the “normal” CSS for the element will kick in, so if we don’t also make it blue in there, the fill will change to black. Sometimes that behaviour is desirable; you want to change the style back to the defaults after the animation has run. But, more commonly, you want the style at the end of the animation to persist.** | There’s one little caveat to catch: notice that there’s still a fill value on the rect CSS block. At the end of the animation, the “normal” CSS for the element will kick in, so if we don’t also make it blue in there, the fill will change to black. Sometimes that behaviour is desirable; you want to change the style back to the defaults after the animation has run. But, more commonly, you want the style at the end of the animation to persist.** | ||
+ | |||
+ | Aussi, en prenant ensemble les deux blocs ci-dessus, nous avons dit en gros « créez un ensemble de trames-clés appelées « myAnimation » et faites-les tourner sur le < | ||
+ | |||
+ | Il y a un petit avertissement à prendre en compte : notez qu'une valeur de remplissage sur le bloc de CSS rect existe toujours. À la fin de l' | ||
**As an alternative to duplicating the final rules for the element, we can make the last style stick by adding the “animation-fill-mode” rule to our rect block instead. The behaviour we’re seeing is equivalent to setting this to “none”, but you can also use “forwards” to make the element continue using the style from the last keyframe. There’s also “backwards” which does the same with the first keyframe for animations played in reverse, or “both” which works in either direction, and is usually the simplest option to choose. | **As an alternative to duplicating the final rules for the element, we can make the last style stick by adding the “animation-fill-mode” rule to our rect block instead. The behaviour we’re seeing is equivalent to setting this to “none”, but you can also use “forwards” to make the element continue using the style from the last keyframe. There’s also “backwards” which does the same with the first keyframe for animations played in reverse, or “both” which works in either direction, and is usually the simplest option to choose. | ||
Animations played in reverse? Yes, there’s another property that will let you do that: “animation-direction”. The default value is “normal” (i.e. your animation plays forwards from 0% to 100%), but you can also set it to “reverse” (100% to 0%), “alternate” (0% to 100% then back to 0%) or “alternate-reverse” (100% to 0% then back to 100%).** | Animations played in reverse? Yes, there’s another property that will let you do that: “animation-direction”. The default value is “normal” (i.e. your animation plays forwards from 0% to 100%), but you can also set it to “reverse” (100% to 0%), “alternate” (0% to 100% then back to 0%) or “alternate-reverse” (100% to 0% then back to 100%).** | ||
+ | |||
+ | Comme alternative à la duplication des règles de fin pour l' | ||
**Once you’ve decided what direction(s) your animation should run in, you might want to also determine how many times it will be performed. For that we have “animation-iteration-count” which can take the value “infinite” to make the animation run forever, or a number – to specify a particular number of cycles. Note that a single cycle goes from 0% to 100% (or vice versa), so if you set “animation-direction” to “alternate” or “alternate-reverse”, | **Once you’ve decided what direction(s) your animation should run in, you might want to also determine how many times it will be performed. For that we have “animation-iteration-count” which can take the value “infinite” to make the animation run forever, or a number – to specify a particular number of cycles. Note that a single cycle goes from 0% to 100% (or vice versa), so if you set “animation-direction” to “alternate” or “alternate-reverse”, | ||
+ | |||
+ | Quand vous avez décidé dans quel sens votre animation fonctionnera, | ||
**Let’s put all this together, and add another keyframe for good measure. I’ve also thrown in an “animation-delay” so that there’s a short pause between the file loading and the animation starting. Our < | **Let’s put all this together, and add another keyframe for good measure. I’ve also thrown in an “animation-delay” so that there’s a short pause between the file loading and the animation starting. Our < | ||
So far, we’ve animated only one property at a time. But because each keyframe carries a block of CSS, it’s possible to animate more than one property on an object. Here I’ve also animated the stroke color and width to substantially change my square over the course of the animation. The code is shown on the next page, bottom right.** | So far, we’ve animated only one property at a time. But because each keyframe carries a block of CSS, it’s possible to animate more than one property on an object. Here I’ve also animated the stroke color and width to substantially change my square over the course of the animation. The code is shown on the next page, bottom right.** | ||
+ | |||
+ | Rassemblons le tout et ajoutons une autre trame-clé pour faire bonne mesure. J'ai aussi mis un « animation-delay », de sorte qu'il y a une courte pause entre le chargement du fichier et le début de l' | ||
+ | |||
+ | Jusqu' | ||
**The result, captured at 2s (the length of the delay, just as the animation starts), 3.5s (halfway through the animation), and 5s (the end of the animation), looks something like this – though, obviously, the real thing isn’t a series of three images, but a single image that transitions through these three states: | **The result, captured at 2s (the length of the delay, just as the animation starts), 3.5s (halfway through the animation), and 5s (the end of the animation), looks something like this – though, obviously, the real thing isn’t a series of three images, but a single image that transitions through these three states: | ||
In practice, the iteration count of 3 and direction of “alternate” means that, after a 2s delay, the square progresses through each of these states (iteration 1), then back again in the opposite direction (iteration 2), then finally through them in order again (iteration 3). At the end of the animation the square remains blue with a thick stroke, due to the presence of animation-fill-mode: | In practice, the iteration count of 3 and direction of “alternate” means that, after a 2s delay, the square progresses through each of these states (iteration 1), then back again in the opposite direction (iteration 2), then finally through them in order again (iteration 3). At the end of the animation the square remains blue with a thick stroke, due to the presence of animation-fill-mode: | ||
+ | |||
+ | Les résultats, capturés à 2 s (la durée du retard, juste quand l' | ||
+ | |||
+ | En pratique, le compte d' | ||
**As you can see, animating an SVG image allows for possibilities that aren’t really available with raster images – at least not in a high quality way that works on most browsers. Next time, we’ll expand on these capabilities to push our SVG animations even further.** | **As you can see, animating an SVG image allows for possibilities that aren’t really available with raster images – at least not in a high quality way that works on most browsers. Next time, we’ll expand on these capabilities to push our SVG animations even further.** | ||
+ | |||
+ | Comme vous pouvez le voir, l' | ||
issue135/inkscape.1534138899.txt.gz · Dernière modification : 2018/08/13 07:41 de d52fr