--- title: SVG --- ## SVG SVG or Scalable Vector Graphics is a web standard for defining vector-based graphics in web pages. Based on XML, the SVG standard provides markup to describe paths, shapes, and text within a viewport. The markup can be embedded directly into HTML for display or saved to a `.svg` file and inserted like any other image. You can write SVG by hand, but more complicated graphics can be designed in vector graphics editors such as Illustrator or InkScape and exported to SVG files or code. ## SVG Basics Developers start an SVG graphic with the `` tag and XML namespace like so: ```svg ``` The sample also includes a `version` attribute. The `version` attribute is optional but it is recommended for compliance with XML specifications. This sample won't display anything, it merely establishes a viewport. You can add `height` and `width` attributes to set a display size for the viewport. This essentially establishes a canvas for you to work in. With a viewport in place, you can add basic graphics, text, and path elements. ```svg SVG Text is browser readable! ``` You can see the output and play with the code in this codepen. In the opening `svg` tag we add a width attribute to set the width of the viewport to 100% of the container width, so you can use percentages or pixel widths. The opening svg tag also has `viewbox` attribute which defines a window through which elements of your svg are visible. In this case, the viewbox spans from (0,0) to (600,300). In SVG space, the X-axis starts with zero on the left and increases to the right; the Y-axis starts with zero at the top and increases towards the bottom. The first new tag is the `` tag which defines a rectangle in the SVG viewport. In this case we define a square which is 10 units from the top and left and 100 units tall and wide. The `fill` attribute sets the fill color for the shape. Next we define a circle or oval with the `` tag. The sample defines a circle centered at (240,60) with a radius of 50 units. The `stroke` and `stroke-width` attributes set a stroke color and a size for the stroke. You can add text to the graphic with the `text` tag. The sample text is anchored from the middle of the text to a point at (450, 70) and has a font size of 20 units. The nice thing about text in SVG is it will scale with the rest of your graphic, but it is still readable by the browser and web bots. When you want to apply the same attributes or CSS styles to multiple SVG elements you can group them with the `` tag. Attributes assigned to the `` tag, like the `stroke` attribute in the example, will be applied to all elements within the group. In this case three `` elements. The `` element defines a vector path in the viewport. The path is defined by the `d` attribute. In the first example the definition reads 'move to the absolute coordinate (10, 170) _and_ draw a line to the relative coordinates 590 in the X direction and 0 in the Y direction. The following commands can be used to create your path: M = move to L = line to H = horizontal line to V = vertical line to Z = close path C = (cubic bezier) curve to S = smooth curve to Q = quadratic bezier curve to T = smooth quadratic bezier curve to A = arc ### The canvas element Canvas graphics can be drawn onto a element. You can give such an element width and height attributes to determine its size in pixels. A new canvas is empty, meaning it is entirely transparent and thus shows up simply as empty space in the document. The tag is intended to support different styles of drawing. To get access to an actual drawing interface, we first need to create a context, which is an object whose methods provide the drawing interface. There are currently two widely supported drawing styles: "2d" for two-dimensional graphics and "webgl" for three-dimensional graphics through the OpenGL interface. A context is created through the getContext method on the element. ```html ``` ![](http://www.crwflags.com/fotw/images/s/sly@stt.gif) After creating the context object, the example draws a red rectangle 100 pixels wide and 50 pixels high, with its top-left corner at coordinates (10,10). ### Drawing a pie chart The results variable contains an array of objects that represent the survey responses. ```javascript var results = [{ name: "Satisfied", count: 1043, color: "lightblue"}, { name: "Neutral", count: 563, color: "lightgreen"}, { name: "Unsatisfied", count: 510, color: "pink"}, { name: "No comment", count: 175, color: "silver"}]; ``` To draw a pie chart, we draw a number of pie slices, each made up of an arc and a pair of lines to the center of that arc. We can compute the angle taken up by each arc by dividing a full circle (2 π ) by the total number of responses and then multiplying that number (the angle per response) by the number of people who picked a given choice. ```html ``` This draws the following chart: ![](https://pbs.twimg.com/media/CTDvkA8UwAAdJg5.png) ### Browser Support [Browser support for SVG](https://caniuse.com/#feat=svg) is available in all modern browsers. There are some issues with scaling in IE 9 through IE 11 however they can be overcome with the use of the `width`, `height`, `viewbox`, and CSS. ## Editors * [Vectr](https://vectr.com) - web and desktop tool for creating and editing SVG graphics, free of charge. ## Tools to create SVG There are a few tools available to create SVG in the form of a drawing program. - Inkscape - It is an open source tool for state-of-the-art vector drawing with an easy to use graphical interface. - Adobe Illustrator - Adobe Illustrator is a commercial tool for Vector Imagery. - Google Drawings - Google Drawings is a simple web based software allowing you to export your vectors as SVGs. For more tools, refer to W3C list of tools that supports SVG ## Why you should use SVGs As a vector image format, it allows you to resize an image without any loss of quality and is particularly light weight. As an XML format, it allows you to benefit from the full power of JavaScript and especially CSS. ## Resources - W3C, Scalable Vector Graphics (SVG) 1.1 (Second Edition) - Mozilla Developer Network, SVG