Draw with SVG


How to draw with SVG? There are two common ways getting something dynamic on the stage, direct draw or import a SVG file. Direct draw allows you to easily draw simple shapes like squares, circles, lines etc.. To draw more complex shapes you need to create them in a Vector drawing application like Illustrator or Inkscape.

 

direct draw

We starting off with direct draw, since it is the most basic way to draw with SVG. First, we prepare a simple html file called index.html.

The result is a circle. It is also possible to draw other shapes like a rectangle, ellipse, line, polyline and a polygon.

Some have different attributes then others. See http://www.w3.org/TR/SVG/shapes.html
rect
The ‘rect’ element defines a rectangle which is axis-aligned with the current user coordinate system. Rounded rectangles can be achieved by setting appropriate values for attributes ‘rx’ and ‘ry’.
circle
The ‘circle’ element defines a circle based on a center point and a radius.
ellipse
The ‘ellipse’ element defines an ellipse which is axis-aligned with the current user coordinate system based on a center point and two radii.
line
The ‘line’ element defines a line segment that starts at one point and ends at another.
polyline
The ‘polyline’ element defines a set of connected straight line segments. Typically, ‘polyline’ elements define open shapes.
polygon
The ‘polygon’ element defines a closed shape consisting of a set of connected straight line segments.
To keep thing simple we didn’t animate any of the values. But you can see how easy you can change all the attributes on the fly. It is possible to animate any SVG object via Css, Js or an animation attribute.

SVG import

Direct draw into SVG can be handy under certain circumstances, but most often we need to import graphic assets. How to load and append SVG files to the scene? First we need to create a SVG file, it has not to be a piece of art, just something to import and see the process happening. We will use Inkscape for the job, but any other vector drawing application should do the job as well. Inkscape is a free and open source vector drawing application(link at the bottom).

You can set unique identifiers for later access via Javascript or Css. You can also group objects directly in Inkscape. Working with a Vector drawing application provides a really fast workflow.

DrawWithInkscape

Save the file as Optimized SVG. We call it optimized.svg.

Optimized_SVG

Now we have an SVG file which we will import into a html5 SVG scene.

As you can see it is just an XML file with the informations about the drawing. In this simple case there are two object with the identifiers, “circle_1” and “circle_2”. They are in a group with the id “layer1”.

To import this into our html5 environment we start with a plane html5 index.html file. We add a simple XML request to the Javascript to keep track of the loading process. After the file is loaded we append it to the document, but it is also a good idea to store each element in an Array for later access.
The Objects can be animated like any other object.

conclusion

It is really easy to get something onto the stage with SVG. Working with additional software like Inkscape highly increases the workflow. Once we have all the assets ready and loaded we can move on and start animate them.

tools

icn_inkscape