A HTML5 Tutorial: Creating Simple Graphics With SVG, Scalable Vector Graphics
SVG is a Quick Means to Enhance HTML Documents
Scalable Vector Graphics (SVG) is a way to create and incorporate images in a web page. The images are defined in the XML language. SVG is a W3C recommended practice.
SVG is relatively easy to learn and has a number of advantages over using other image formats such as .jpeg and .gif. Some of the advantages are:
- you have a specific need for a simple two dimensional picture and cannot find anything on the web that matches your specific need.
- you don't need to worry about copyright infringement because the drawing is your own work.
- you don't need to acquire expensive software. SVG can be created with any text editor.
- SVG can be compressed, is searchable, can be scripted
- SVG images can be zoomed without degradation.
- and can be embedded in web pages
- it has support for event handlers.
Of course, nothing really comes without some costs, chiefly is that rendering can be slow for large complex drawings and naturally there is some learning involved.
SVG Images Can Become Quite Complex
The <svg> HTML Tags
To start, one must enclose the code between the <svg> and </svg> element tags.
The next thing you will probably want to do is set width and height attributes for the image. Color for an image part is set with the fill attribute.
SVG Predefined Shapes
SGV has a number of predefined shaps which a developer can use:Circle <circle>
- Circle <circle>
- Ellipse <ellipse>
- Line <line>
- Path <path>
- Polygon <polygon>
- Polyline <polyline>
- Rectangle <rect>
Creating a Simple Circle
The snapshot shows the code and the circle created.The SVG tags needed is <circle> which is self-closing.
The attributes besides width, height, and fill color include the circle attributes:
- cx and cy - are the coordinates of the center of the circle.By default they are set to (0,0).
- r is the radius of the circle in pixels.
Creating a Simple Circle
Creating an Ellipse
To create an ellipse one needs to define values for rx and ry that differ from each other unlike the circle where the values are the same
A Simple Ellipse
A Simple Line
The values needed for the line besides stroke at the beginning and ending points defined by (x1,y1) and (x2,y2) as illustrated in the snapshot which follows.
A Simple Line
Creating a Simple Polygon
The <polygon> element is used to create a graphic that contains at least three sides such as a triangle. In order to create a polygon you need to have at least 3 x,y coordinate pairs.
Polygons are made of straight lines, and the shape is "closed".
A Simple Triangle (Polygon)
Creating a Jagged Line With <polyline>
Creating a Path
We create a path by specifying the <path> element with any of the following commands. The commands are enclosed in a path data value, "d". The commands when they are specified in uppercase means the positioning is absolute. When they are specified in lowercase positioning is relative.
- A = elliptical Arc
- C = curveto
- H = horizontal lineto
- L = lineto
- M = moveto
- Q = quadratic Bézier curve
- S = smooth curveto
- T = smooth quadratic Bézier curveto
- V = vertical lineto
- Z = closepath
The <path> element can create complex drawings, however if you are going to be creating many complex drawings you might want to consider a free application such as Inkscape.
Creating a Simple Triangle Using the <path> Element
Creating a Simple Rectangle
A basic rectangle is shown in the snapshot which follows. The element names are <rect> and </rect> The attributes are relatively simple. One would specify width and height. The style attribute lists the CSS attributes:
- fill - lists the color of the rectangle
- stroke - lists the color of the border
- stroke-width - lists the width of the border.
Two additional attributes are sometimes used fill-opacity and stroke-opacity. Their legal values are in the range 0 to 1.
A Simple Rectangle Defined in SVG
The HTML <text> Elements
Text can be created with the use of <text> and </text> tags. Beause it is an image that you are working with one can apply a number of special effects. The <test> element can be further refined by sing the <tspan> element. We will defer this for now and address it in a tutorial about special effects.
Using the <text> Element
Wrap Up and What's Next
In our next tutorial we will take SVG to a higher level by showing some advanced elements and attributes which results in some very striking web pages.