issue148: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 | ||
issue148:inkscape [2019/09/13 12:03] – auntiee | issue148:inkscape [2019/09/14 15:09] (Version actuelle) – d52fr | ||
---|---|---|---|
Ligne 45: | Ligne 45: | ||
**Breaking this down, we have an <svg> tag containing a < | **Breaking this down, we have an <svg> tag containing a < | ||
- | En décomposant cet exemple, nous avons une balise <svg> contenant une balise < | + | En décomposant cet exemple, nous avons une balise <svg> contenant une balise < |
**The < | **The < | ||
Ligne 61: | Ligne 61: | ||
**Press F12 or use the menu to open your browser’s developer tools. Somewhere along the top should be a row of tabs (though they’re not always clearly styled as such). Make sure you have the “Console” tab selected. If the panel is already filled with text, find the button in the console’s toolbar to clear it, for clarity. Click inside the console area to give it the focus, and type the following (followed by the Enter key):** | **Press F12 or use the menu to open your browser’s developer tools. Somewhere along the top should be a row of tabs (though they’re not always clearly styled as such). Make sure you have the “Console” tab selected. If the panel is already filled with text, find the button in the console’s toolbar to clear it, for clarity. Click inside the console area to give it the focus, and type the following (followed by the Enter key):** | ||
- | Appuyez sur F12 ou utilisez le menu pour ouvrir les outils de développement de votre navigateur. Quelque part en haut de l’écran, il devrait y avoir une rangée d' | + | Appuyez sur F12 ou utilisez le menu pour ouvrir les outils de développement de votre navigateur. Quelque part en haut de l’écran, il devrait y avoir une rangée d' |
var t = document.querySelector("# | var t = document.querySelector("# | ||
**The console will display the string “undefined” at this point. That’s nothing to worry about, it just indicates that the line you entered didn’t return a value. But what it has done is find the element with an ID of “text” and assign it to the variable “t”. You can confirm that by typing the letter “t” on its own, then pressing Enter. The console should show a representation of the < | **The console will display the string “undefined” at this point. That’s nothing to worry about, it just indicates that the line you entered didn’t return a value. But what it has done is find the element with an ID of “text” and assign it to the variable “t”. You can confirm that by typing the letter “t” on its own, then pressing Enter. The console should show a representation of the < | ||
- | + | ||
- | La console affichera la chaîne de caractères | + | La console affichera la chaîne de caractères |
**Let’s use some JavaScript we already know to reduce the size of the font a little. Type this into the console:** | **Let’s use some JavaScript we already know to reduce the size of the font a little. Type this into the console:** | ||
Ligne 77: | Ligne 77: | ||
**The SVG content should react as soon as you press the Enter key. Type the letter “t” again and you’ll see that the element now has a “style” attribute with the font-size property set. Notice that we set “fontSize” in JS, but the CSS in the attribute shows “font-size”. If you tried to use the latter in JavaScript, it would be interpreted as trying to subtract the “size” variable from the “font” variable, and would throw an error. As a general rule, any CSS property containing embedded hyphens is available as a JavaScript property by removing the hyphens and capitalising the first letter of all but the first word.** | **The SVG content should react as soon as you press the Enter key. Type the letter “t” again and you’ll see that the element now has a “style” attribute with the font-size property set. Notice that we set “fontSize” in JS, but the CSS in the attribute shows “font-size”. If you tried to use the latter in JavaScript, it would be interpreted as trying to subtract the “size” variable from the “font” variable, and would throw an error. As a general rule, any CSS property containing embedded hyphens is available as a JavaScript property by removing the hyphens and capitalising the first letter of all but the first word.** | ||
- | Le contenu SVG doit réagir dès que vous appuyez sur la touche Entrée. Tapez à nouveau la lettre | + | Le contenu SVG devrait |
**Breaking down the line above, you know that “t” is a JavaScript representation of our XML node. The browser exposes various properties and methods (functions tied to a specific object) on that node, including the “style” property. This property, in turn, has a “fontSize” property, which we’ve set to a value of “10px”. But the browser treats the “style” property a little differently to most JavaScript properties, and instead also applies any changes to the “style” attribute in the XML. In this instance, it doesn’t matter whether you change the attribute or the property – but that’s not usually the case.** | **Breaking down the line above, you know that “t” is a JavaScript representation of our XML node. The browser exposes various properties and methods (functions tied to a specific object) on that node, including the “style” property. This property, in turn, has a “fontSize” property, which we’ve set to a value of “10px”. But the browser treats the “style” property a little differently to most JavaScript properties, and instead also applies any changes to the “style” attribute in the XML. In this instance, it doesn’t matter whether you change the attribute or the property – but that’s not usually the case.** | ||
- | En décomposant la ligne ci-dessus, vous savez que "t" | + | En décomposant la ligne ci-dessus, vous savez que « t » est une représentation JavaScript de notre nœud XML. Le navigateur expose diverses propriétés et méthodes (fonctions liées à un objet spécifique) sur ce nœud, dont la propriété |
**To change most attributes, therefore, you can’t just set a correspondingly named JavaScript property. Instead, you have to use the setAttribute() method that we’ve looked at previously. Here’s how we might move the text up a little:** | **To change most attributes, therefore, you can’t just set a correspondingly named JavaScript property. Instead, you have to use the setAttribute() method that we’ve looked at previously. Here’s how we might move the text up a little:** | ||
- | Pour changer la plupart des attributs, vous ne pouvez donc pas simplement définir une propriété JavaScript nommée en conséquence. Au lieu de ça, vous devez utiliser la méthode setAttribute() que nous avons examinée précédemment. | + | Pour changer la plupart des attributs, vous ne pouvez donc pas simplement définir une propriété JavaScript nommée en conséquence. Au lieu de ça, vous devez utiliser la méthode setAttribute() que nous avons examinée précédemment. |
t.setAttribute(" | t.setAttribute(" | ||
Ligne 91: | Ligne 91: | ||
**Type “t” again to see the XML, and you’ll notice the “y” attribute now has a value of “20”. We can also retrieve that value using the getAttribute() method:** | **Type “t” again to see the XML, and you’ll notice the “y” attribute now has a value of “20”. We can also retrieve that value using the getAttribute() method:** | ||
- | Tapez à nouveau | + | Tapez à nouveau |
t.getAttribute(" | t.getAttribute(" | ||
Ligne 105: | Ligne 105: | ||
**Gah! Where did the text go!? Actually it’s still there, but it’s been positioned so far down in the image that it’s dropped out of the 100x100 viewBox, so isn’t visible. But why is that, when we just wanted to adjust the value from 20 to 30?** | **Gah! Where did the text go!? Actually it’s still there, but it’s been positioned so far down in the image that it’s dropped out of the 100x100 viewBox, so isn’t visible. But why is that, when we just wanted to adjust the value from 20 to 30?** | ||
- | Bah ! Où est passé le texte ? En fait, il est toujours là, mais il a été positionné si bas dans l' | + | Bah ! Où est passé le texte ? En fait, il est toujours là, mais il a été positionné si bas dans l' |
**The problem is that XML is a text-based system, and doesn’t really have a concept of different data types. All attributes are therefore text strings, regardless of the value you put in, so our call to getAttribute() returns the string “20”, not the number 20. JavaScript then tries to be ‘helpful’ by determining that we’re trying to ‘add’ the number 10 to the string “20”. Since you can’t add a number to a string, it automatically converts the number into a string (“10”), then concatenates the two, to give a result of “2010”. That’s the value we end up putting into the attribute in our setAttribute() call, so our text ends up being moved to a y-position of 2010 units!** | **The problem is that XML is a text-based system, and doesn’t really have a concept of different data types. All attributes are therefore text strings, regardless of the value you put in, so our call to getAttribute() returns the string “20”, not the number 20. JavaScript then tries to be ‘helpful’ by determining that we’re trying to ‘add’ the number 10 to the string “20”. Since you can’t add a number to a string, it automatically converts the number into a string (“10”), then concatenates the two, to give a result of “2010”. That’s the value we end up putting into the attribute in our setAttribute() call, so our text ends up being moved to a y-position of 2010 units!** | ||
- | Le problème est que XML est un système basé sur le texte, et n'a pas vraiment un concept de différents types de données. Tous les attributs sont donc des chaînes de texte, quelle que soit la valeur que vous entrez, donc notre appel à getAttribute() renvoie la chaîne | + | Le problème est que XML est un système basé sur le texte, et n'a pas vraiment un concept de différents types de données. Tous les attributs sont donc des chaînes de texte, quelle que soit la valeur que vous entrez, donc notre appel à getAttribute() renvoie la chaîne |
**We can fix this by converting the value returned from getAttribute() into a number. We only want an integer value, so the parseInt() function is the tool to use – but there is also a parseFloat() if you need to deal with decimal fractions. parseInt() has a second parameter for the number base that you should always provide (with a value of 10 for a decimal conversion) to avoid some rare-but-odd corner case bugs when converting certain strings. Entering the following lines into the console should get us the result we were looking for:** | **We can fix this by converting the value returned from getAttribute() into a number. We only want an integer value, so the parseInt() function is the tool to use – but there is also a parseFloat() if you need to deal with decimal fractions. parseInt() has a second parameter for the number base that you should always provide (with a value of 10 for a decimal conversion) to avoid some rare-but-odd corner case bugs when converting certain strings. Entering the following lines into the console should get us the result we were looking for:** | ||
- | Nous pouvons corriger cela en convertissant la valeur retournée par getAttribute() en nombre. Nous voulons seulement une valeur entière, donc la fonction parseInt() est celle à utiliser | + | Nous pouvons corriger cela en convertissant la valeur retournée par getAttribute() en nombre. Nous voulons seulement une valeur entière, donc la fonction parseInt() est celle à utiliser, mais il y a aussi un parseFloat() si vous avez besoin de traiter des fractions décimales. parseInt() a un second paramètre pour la base de nombres que vous devriez toujours fournir (avec une valeur de 10 pour une conversion décimale) pour éviter certains bugs (cas rares, mais étranges), quand vous convertissez certaines chaînes. Le fait de saisir les lignes suivantes dans la console devrait nous donner le résultat que nous recherchons : |
t.setAttribute(" | t.setAttribute(" | ||
Ligne 152: | Ligne 152: | ||
**Being able to change the text content via JavaScript opens up a world of possibilities, | **Being able to change the text content via JavaScript opens up a world of possibilities, | ||
- | La possibilité | + | Être capable |
ts1.textContent = prompt(" | ts1.textContent = prompt(" | ||
Ligne 168: | Ligne 168: | ||
**Adding a new element to the page can be trivially easy, or it can be rather convoluted. Let’s start with the easy method, by adding another < | **Adding a new element to the page can be trivially easy, or it can be rather convoluted. Let’s start with the easy method, by adding another < | ||
- | Ajouter un nouvel élément à la page peut être facile ou plutôt compliqué. Commençons par la méthode facile, en ajoutant un autre < | + | Ajouter un nouvel élément à la page peut être vraiment |
t.innerHTML += '< | t.innerHTML += '< | ||
Ligne 174: | Ligne 174: | ||
**Even though we’re working on an SVG file, which is a form of XML document, we still have to use the “innerHTML” property. This returns all the descendants of the specified node as a string – basically a string of HTML (or XML in this case) much like the ones you type into a text editor. The “+=” operator essentially retrieves a value, adds or concatenates something to it, and puts the result back into the same place. In our case it has the effect of appending a new < | **Even though we’re working on an SVG file, which is a form of XML document, we still have to use the “innerHTML” property. This returns all the descendants of the specified node as a string – basically a string of HTML (or XML in this case) much like the ones you type into a text editor. The “+=” operator essentially retrieves a value, adds or concatenates something to it, and puts the result back into the same place. In our case it has the effect of appending a new < | ||
- | Même si nous travaillons sur un fichier SVG, qui est une forme de document XML, nous devons quand même utiliser la propriété | + | Même si nous travaillons sur un fichier SVG, qui est une forme de document XML, nous devons quand même utiliser la propriété |
**Let’s do something similar, but with a more complex approach…** | **Let’s do something similar, but with a more complex approach…** | ||
Ligne 193: | Ligne 193: | ||
**• We set up a variable, “ns”, that will hold our SVG namespace. Usually this is done once at the top of the JavaScript so you can use it in multiple places.** | **• We set up a variable, “ns”, that will hold our SVG namespace. Usually this is done once at the top of the JavaScript so you can use it in multiple places.** | ||
- | • Nous avons mis en place une variable, | + | • Nous avons mis en place une variable, |
**• We create a new < | **• We create a new < | ||
Ligne 201: | Ligne 201: | ||
**• We give the element an ID to make it easier to get hold of later. We could have used setAttribute() for this, but the browser has an implicit mapping between the property and attribute in this case, in the same manner as we saw earlier with the ‘style’ property.** | **• We give the element an ID to make it easier to get hold of later. We could have used setAttribute() for this, but the browser has an implicit mapping between the property and attribute in this case, in the same manner as we saw earlier with the ‘style’ property.** | ||
- | • Nous donnons à l' | + | • Nous donnons à l' |
**• Now we can set an attribute on the new element. We would need to repeat a line like this for each attribute we wish to set.** | **• Now we can set an attribute on the new element. We would need to repeat a line like this for each attribute we wish to set.** | ||
Ligne 221: | Ligne 221: | ||
**Consider trying to plot a graph using SVG. Each point on the graph might be represented by a < | **Consider trying to plot a graph using SVG. Each point on the graph might be represented by a < | ||
- | Et si vous tentiez de tracer un graphique à l'aide de SVG ? Chaque point du graphique peut être représenté par un < | + | Et si vous tentiez de tracer un graphique à l'aide de SVG ? Chaque point du graphique peut être représenté par un < |
**Next time, we’ll build on the techniques used in this instalment, to further investigate ways to manipulate the individual elements in an SVG document through JavaScript.** | **Next time, we’ll build on the techniques used in this instalment, to further investigate ways to manipulate the individual elements in an SVG document through JavaScript.** |
issue148/inkscape.1568368986.txt.gz · Dernière modification : 2019/09/13 12:03 de auntiee