INFO-I400 Topics in Informatics

Graphics, Animation, and Multimedia for the Web

Lesson 17: Introduction to SVG

Prerequisites:

Reading Assignment

David Dailey, An SVG Primer for Today’s Browsers:

Read enough of this, and carefully enough, that you are able to meet the learning objectives immediately below.

Learning Objectives

Be able to:

  1. Describe the nature and advantages of vector graphics over bitmapped graphics.
  2. Create stand-alone SVG documents, and SVG elements within HTML documents.
  3. Write SVG elements describing lines, rectangles, circles, ellipses, paths, images, and text.
  4. Write attributes for these elements to control stroke and fill styles (including colors, stroke-width, stroke-dasharray, stroke-linecap), and text styles (font size, etc.).
  5. Define, create, and use groups of elements with the defs, g, and use tags.
  6. Transform SVG elements (translate, rotate, scale).

Status of SVG Today

The Primer was written in 2010, and things move rapidly. The W3C has two current standards for SVG:

They are working on modularizing the specification (i.e., breaking it down into parts like color management, CSS animations, CSS transforms, filter effects, vector effects) and plan to put together an SVG 2.0 specification in August 2014.1

What about browser support?

Gallery

SVG Study shows off a number of shapes, text, Bezier curves, declarative animation and effects, and dynamic control through JavaScript.

Reference

SVG Element

<svg xmlns="http://www.w3.org/2000/svg" width="800" height="600">
  ...
</svg>

The point (0, 0) is the left top corner of the SVG element. The x axis points right; the y axis points down.

Comments

SVG is an XML application (that is, a language in the XML framework). Therefore, SVG comments are XML comments; they look like this:

<!-- This is a comment.  -->

Figures

A line from (x1, y1) to (x2, y2):

<line x1="0" y1="0" x2="1" y2="1"/>

A rectangle with left top corner (x, y):

<rect x="0" y="0" width="1" height="1"/>

A circle with radius (cx, cy) and radius r:

<circle cx="100" cy="100" r="100"/>

An ellipse with radius (cx, cy) and radii rx and ry:

<ellipse cx="100" cy="100" rx="50" ry="150"/>

Paths:

<path d="..."/>

where the value of d includes directions like these:

Example:

<path d="M 0 0
         L 100 80 
         C 250 400   600 90   300 300
         z"/>

draws a line from (0, 0) to (100, 80), a cubic Bezier curve from (100, 80) to (300, 300) with intermediate control points (250, 400) and (600, 90), and closes the path with a line from (300, 300) back to (0, 0).

Example 17-01

Stroke and Fill

For stroke and fill colors, SVG 1.1 recognizes all the color syntax of CSS2 colors: hexadecimal, rgb(255, 255, 255), rgb(100%, 100%, 100%); and the SVG recognized color keywords. Use “none” for no color (i.e., no stroke or no fill). The SVG 1.1 Full specification does not allow HSL, HSLA, or RGBA color specifications. The SVG2 draft color specification seems to allow HSL, HSLA, and RGBA color syntax, from CSS3 Color, and these all work in current Firefox and Chromium (other browsers?). Opacity (alpha) can (also) be specified separately.

Example:

<rect x="100" y="80" width="200" height="25"
      fill="red" stroke="rgb(255, 255, 255)" opacity="0.6"
      stroke-width="6" stroke-dasharray="8,4,12,4"/>

Example 17-02

Text and Images

<text x="100" y="80" font-size="16">Partly cloudy.</text>

Text elements can also have a style attribute with CSS style properties such as font-family, font-style, font-weight, font-size, text-decoration, etc.

Text can be made to run along a path, as explained in the tutorial.

Image files can be PNG, JPEG, or SVG.

<image xlink:href="IMAGE-URL" x="100" y="100" width="308" height="350"/>

The notation xlink:href means that the href attribute belongs to the xlink namespace. Namespaces in XML are similar to packages in Java or modules in Python. You may need to identify this namespace with xmlns:xlink="http://www.w3.org/1999/xlink" either in the image tag or in the svg tag.

Example 17-03

Transforms, Groups, and Defs

SVG supports affine transforms including translate, rotate, and scale. These are specified by the transform attribute in any element, either singly or in combination:

From the tutorial it sounds like scaling is pretty crude, and actually repositions the element because it just multiplies each coordinate by the scale factor, so you have to either position the object at (0, 0) before scaling and/or compensate by a translation.

Transforms can be combined:

Transforms become really powerful when applied to groups. To put elements into a group, enclose them in g tags, and apply the transform to the whole group:

<g transform="...">
  <path .../>
  ...
</g>

Even more, you can define elements (groups or otherwise) by creating them in a defs element and giving them an id attribute (the elements are not drawn when defined); then use them (multiple times) with a use element. When referencing the defined element, be sure to prefix its id with # (e.g., #g0 instead of just g0):

<defs>
  <g id="g0">
    <path .../>
    ...
  </g>
</defs>

<use xlink:href="#g0" transform="translate(50, 50)"/>
<use xlink:href="#g0" transform="translate(90, 110)"/>
<use xlink:href="#g0" transform="translate(200, 180), rotate(45, 0, 0)"/>

Attributes of an element within a group can be set to inherit the value from the group:

<defs>
  <g id="g1" fill="green">
    <rect ... fill="inherit"/>
    ...
  </g>
</defs>

<use xlink:href="#g1" transform="translate(60, 90)"/> <!-- rect is green -->
<use xlink:href="#g1" transform="translate(160, 190)"
     fill="red"/> <!-- rect is red -->

Example 17-04

Reference


  1. W3C Roadmap - SVG