Outils pour utilisateurs

Outils du site


issue139:inkscape

Ceci est une ancienne révision du document !


There’s a part of the Inkscape user interface that is so common, and so taken for granted, that you’ve probably never given much thought to it: the page border. Yet this seemingly mundane part of the display plays a vital role when it comes to creating SVG files for use online. By understanding what it represents, and how to manipulate it, you will open up some techniques that can make your SVG files far more versatile, even if they’re only being included via an <img> tag. By default, creating a new document in Inkscape will create an A4 page (210x297mm), displaying a thin outline to represent the page border, and a drop shadow to make it look a little more like a page of paper than a simple rectangle. I can’t say for certain if A4 is used as a default everywhere, or if there is a locale dependency that creates US Letter pages in the USA, for example, but either way, you get a default size and visible page border. Changing the page size is done via the File > Document Properties (Ctrl-Alt-D) dialog: you can either select one of the predefined page sizes or enter a custom width and height, with your choice of units. The bottom of the dialog also provides options to show or hide the page border and to display it without the drop shadow, if you prefer.

Une partie de l’interface utilisateur d’Inkscape est tellement classique, et tellement prise pour acquis, que vous n’avez probablement jamais pris le temps d’y réfléchir : c’est la bordure de la page. Cette partie de l’affichage, qui semble banale, joue un rôle vital quand on passe à la création de fichiers SVG pour un usage en ligne. En comprenant ce qu’elle représente, et comment la manipuler, vous découvrirez certaines techniques qui peuvent rendre les fichiers SVG largement plus polyvalents, même s’il ne sont inclus que via une balise <tag>.

Par défaut, la création d’un nouveau document dans Inkscape créera une page A4 1)

1)
210x297mm), affichant un fin contour qui représente le bord de la page et une ombre portée qui la fait ressembler plus à une feuille de papier qu’à un simple rectangle. Je ne peux pas dire su le A4 est utilisé par défaut partout, ou s’il y a un lien de localisation qui crée des pages au format Letter aux USA, par exemple, mais, quoiqu’il en soit, vous obtenez une taille par défaut et un bord de page visible. En Le changement de la taille de la page s’obtient via le dialogue Fichier > Propriétés du document (Ctrl-Alt-D) : vous pouvez, soit sélectionner une des tailles de pages prédéfinies, soit entrer des largeur et hauteur personnalisées, avec le choix de vos unités. En bas du dialogue, vous disposez aussi d’options pour afficher ou cacher le bord de page et pour l’afficher sans ombre portée, si vous le préférez. I’ve seen a few video tutorials where the presenter heads straight to this dialog to turn off the page border, but I think that’s usually a mistake. If you’re designing for print then having an idea of how your work fits into the page is essential. But the border is just as important for web work, as anything that is drawn outside it won’t be rendered by the browser. If you turn the border off, there’s a real danger that parts of your design might inadvertently fall outside it, preventing them from rendering as expected. The non-display, or non-printing, of content outside the page border, can be a blessing. If your design has to bleed off the edge of the page, it can be essential. You can also use the area outside the page to store rough designs, notes, spare elements, or the source objects for clones (particularly those with unset fills) – anything which you want to keep with your image, but don’t want to be visible in the final product. I’ve often used this capability to include Easter eggs in my comic strips – even to the extent of holding extra panels or even entire extra strips that can be found only by opening the original source file in Inkscape. J’ai regardé quelques tutoriels vidéos ou le présentateur fonce vers ce dialogue pour masquer le bord de page, mais je pense que que c’est usuellement une erreur. Si vous dessinez pour ensuite imprimer, avoir une idée de comment votre travail s’intègre dans la page est essentiel. Mais la bordure est tout aussi importante pour un travail pour le Web, car tout ce que vous dessinez en dehors ne sera pas rendu par le navigateur. Si vous masquez la bordure, il y a un vrai danger que des parties de votre dessin se trouvent à l’extérieur par inadvertance, les empêchant d’être rendues comme prévu. Le non-affichage, ou la non-impression, du contenu situé à l’extérieur du bord de la page peut être un bienfait. Si votre dessin doit déborder de la page, ça peut être essentiel. Vous pouvez aussi utiliser la zone extérieure pour y stocker des esquisses, des notes, des éléments épars, ou les objets sources pour le clonage (en particulier ceux avec un remplissage indéfini) - tout ce que vous voulez garder avec votre image, mais que vous ne voulez pas voir dans le résultat final. J’ai souvent utilisé cette faculté pour y inclure des Easter Eggs (Œufs de Pâques - petits cadeaux cachés) dans mes bandes dessinées _ allant même jusqu’à y mettre des planches supplémentaires ou même des bandes dessinées complètes qui ne peuvent être trouvées qu’en ouvrant le fichier original dans Inkscape. For many uses, the page size can natively be set in the Document Properties dialog, with no need to concern yourself any further. This sets the “width” and “height” attributes on the main <svg> which determines the default size that your image will be drawn in the browser. For an icon design, for example, you might set the dimensions to 32px by 32px, and that’s the size it will be rendered. But what happens when you want to use a different scale inside your drawing? Your drawing might be in metres or even miles, yet you still want it displayed at a reasonable size in the browser. For this, there is the viewBox attribute. The viewBox attribute is a list of four numbers, representing the x and y coordinates of the origin in the drawing, and the width and height of the drawing in “user units”. Let’s look at a couple of examples: <svg … width=“100” height=“200” viewBox=“0 0 100 200” … > Dans de nombreuses utilisations, la taille de la page peut être réglée nativement dans le dialogue des Propriétés du document, sans s’en préoccuper par la suite. Ceci paramètre les attributs « width » (largeur) et « height » (hauteur) dans le <svg> principal, ce qui détermine la taille par défaut dans laquelle votre image sera dessinée dans le navigateur. Pour le dessin d’une icône, par exemple, vous devez régler les dimensions à 32 px x 32 px et le rendu sera fait dans cette taille. Mais que se passe-t-il quand vous voulez utiliser une échelle différente dans votre dessin ? Votre dessin pourrait être en mètres ou même en miles, que, là encore, vous voudriez qu’il soit affiché avec une taille raisonnable dans votre navigateur. Pour cela, il y a l’attribut viewBox. L’attibut viewBox est une liste de quatre nombres, représentant les coordonnées x et y de l’origine du dessin et les largeur et hauteur de ce dessin en « unités utilisateur ». Laissez-moi prendre quelques exemples : <svg …
width="100"
height="200"
viewBox="0 0 100 200"
… >
This one’s simple. The width and height of the image will default to 100px by 200px, and the coordinates in the drawing are mapped to the image on a 1:1 basis. If you draw a rectangle that’s 100 units wide and 200 units tall, it will fill the available space in the browser window (assuming you position it at the top left of your drawing). Let’s try another: <svg … width=“100” height=“200” viewBox=“0 0 500 1000” … > Again the image will render to a size of 100px by 200px in the browser. But the viewBox defines a different coordinate system. Now 500 units in the drawing map to 100px in the browser. If you draw a rectangle that’s 100 units wide and 200 units tall this time, it will occupy only one corner of the image (actually being drawn as 20px by 40px in the browser). To fill the image, you would need to draw a rectangle that’s 500 units by 1000 units. Celui-ci est simple. Les largeur et hauteur de l’image seront de 100 px par 200 px par défaut, et les coordonnées du dessin formeront une image sur une base de 1:1. Si vous dessinez un rectangle de 100 unités de large pour 200 unités de hauteur, il remplira l’espace disponible dans la fenêtre du navigateur (en supposant que vous le positionnez en haut à gauche de votre dessin). Essayons-en un autre : <svg …
width="100"
height="200"
viewBox="0 0 500 1000"
… >
À nouveau, l’image est affichée dans une taille de 100 px par 200 px dans votre navigateur. Mais l’attribut viewBox définit un autre système de coordonnées. Les 550 px de votre dessin deviennent 100 px dans le navigateur. Si vous dessinez un rectangle qui, cette fois, est de large 100 px et haut de 200 px, il n’occupera seulement qu’un coin de l’image (étant dessiné réellement en 20 px par 40 px dans le navigateur). Pour remplir l’image, vous devrez dessiner un rectangle de 500 unités par 1000 unités. Changing the x and y values lets you move the origin of your coordinate system. It lets you say “the origin for the browser (i.e. the point that’s used as the top left of the image) should actually be 100 units down and 50 units across in my drawing”. Another way to think of it is that the viewBox lets you create a viewport into your drawing of a specific size and location: everything inside the viewport will be scaled up or down to fill the image in the browser; everything outside the viewport will be cropped and left un-drawn. For use in a web page, this capability to render only part of the image lets us perform a rather neat trick. By changing the viewBox values, we can selectively display subsections of the file, letting us store multiple images in one file. This reduces the number of network requests needed by your page, in turn speeding up your site. Consider this collection of four images – a star, a circle, a spiral path, and some text in the rather wonderful free font, Trump Grotesk Bold. I’ve drawn them in four sections of a tall, thin page that is 250px wide by 1000px tall (so each element occupies 250px by 250px). With the viewBox set to “0 0 250 1000”, we have a 1:1 mapping when the image is drawn in the browser. You can pretty much ignore the “Scale x” and “Scale y” values – they get set automatically by Inkscape as you change the viewBox fields. As you might expect, when I save this image and load it into the browser, I see all four elements, taking up a space of 250px by 1000px. But look what happens if I change both the page height and the viewBox height to 250: As you can see, the page border only surrounds the text. If I save the pge and load it into the browser, all I get is a 250px by 250px image showing the text element. If I now set the “y” value for the viewBox to -250, thus moving the viewport upwards, only the spiral appears in the page. Saving the file and loading it in a browser now only renders the spiral, hiding the other three elements. I’m sure you’ve worked out by now that setting the “y” value to -500 will put the page around the circle, whilst -750 puts it around the star. Now let’s just remind ourselves what the HTML <img> tag looks like to render this image: <img src=“views.svg”></img> Well that’s pretty terse and to the point. It just tells the browser to show the “views.svg” file using the width and height set in the SVG file, and displaying the default viewport set by the viewBox attribute. But we can append a little magic to the filename to tell the browser to override the default viewbox: <img src=“views.svg#svgView(viewBox(0, -750, 250, 250))”></img> By changing the viewBox values in the URL we can therefore select a specific region of the image to display. In this case, it allows us to choose between one of several sub-images, making this approach ideal for files that contain multiple icons or logos. An alternative is to use viewBox values that focus on a particular part of your design, or cause some section to be zoomed in. That gives you the possibility of showing, for example, an overview and a detail view, both taken from the same image. Hard-coding the viewBox dimensions into the URLs does have one significant problem: if your image changes, such that elements are swapped or moved, you also need to update the HTML or CSS file containing the URLs. SVG has an answer to this problem as well: named views. Named views are, as you might have guessed, a way of giving a particular set of viewBox values a name – that can then be referenced from elsewhere. Unfortunately, Inkscape has no specific support for them, ex pxcept to expose the underlying code in the XML editor. But the syntax is straightforward enough that they’re easy to add via a text editor. They can go pretty much anywhere in the SVG file, but as they’re not visible objects in their own right, I prefer to keep them in the <defs> section where things such as filter and gradient definitions live. Named views can be thought of as viewBox definitions, so this location makes sense to me. Here’s an example of the top section of the SVG file above, once I’ve added named views for each of the objects to my <defs> (below). In this example, the viewBox attribute in the <svg> element is set to show the text content, but I could equally have set it to show all four objects, just a couple of them, a smaller part of one of the objects, or any other rectangular space in the image. This is the viewBox that will be used by default if nothing else is specified in the document’s URL. In the <defs> section, you can see that I’ve also defined four <view> elements. Each of these has an ID that will be used to reference them later, together with a viewBox attribute. The IDs aren’t special: I’ve called them “textView”, “spiralView” and so on, simply to make it clear what they’re showing, but I could equally have gone for “fred”, “wilma”, “barney” and “betty” – had I wished to. The only requirements are that they are valid XML IDs, and are unique within the document. If in doubt, stick to plain text with no punctuation and you should be okay. Using your named view in an <img> tag is trivially straightforward – you just have to set the appropriate ID as the fragment identifier (the bit after the “#” character in a URL): <img src=“views.svg#spiralView”></img> Of course there’s nothing to stop you referencing the same image more than once in a web page, with a different fragment identifier each time. You can also mix and match named views, the “svgView()” syntax, and the default viewBox. In this way, a single SVG image can easily be used to provide a whole host of icons and other images for your page. To finish off, here’s an example of an HTML document that uses the SVG image from this tutorial: You should now be able to understand how our single SVG image is used multiple times to give the final result:
issue139/inkscape.1543823442.txt.gz · Dernière modification : 2018/12/03 08:50 de d52fr