There are two modern web technologies for creating rich drawn graphics within the browser: HTML5 Canvas and Scalable Vector Graphics (SVG). This presentation focuses on SVG. SVG is fully described in the W3C SVG Specification.

SVG is an XML (Extensible Markup Language) technology. XML is a simple text-based format for representing structured information. See W3C's XML Essentials.

The graphical renderings in the problems below were produced with SVG.

SVG is incorporated into web pages by embedding the svg element in a parent HTML document.

The svg element accepts graphics elements as children to draw lines, shapes, text, etc.

The svg element has attributes for, among other things,

The example file shown below renders the image shown to the right. Note that the svg element shown has no children.

How It Renders

The following pages show the addition of graphics elements as children of the svg element.

We begin by depicting the water jug puzzle.

The polyline element defines a set of connected straight line segments.

It has attributes for specifying, among other things,

Below we have added two polyline elements as children of the svg element to draw outlines of the water jugs.

How It Renders

The text element defines a graphics element consisting of text.

It has attributes for specifying, among other things,

The text to be rendered is given in the text element's body.

Below we have added six text elements as children of the svg element to label the water jugs.

How It Renders

The rect element defines a rectangle.

It has attributes for specifying, among other things,

Below we have added two rect elements as children of the svg element to represent the water in the jugs.

Note that the rect elements precede the polyline elements so that the jugs' black outlines are rendered over the blue water.

How It Renders

A path element represents the outline of a shape that can be filled or stroked.

A path can be understood by analogy with a pen drawing on paper. The pen has a current position, which can be changed, and the pen can be dragged on the paper to create straight or curved lines.

The outline of the shape is specified through the path element's d attribute, which provides path data in the form of commands for moving the pen and drawing.

Some path data commands:

See the
W3C Path Specification
The example shown below produces a curving blue river on a green background for the farmer, wolf, goat, and cabbage problem.

How It Renders

The next example depicts waves on the river:

by specifying connected elliptical arcs in a path element's path data attribute.

The arc command (a) requires:

The path data in the example below begins with an M command to move to a start point for the waves path.

Since the a command is lower case, the end point it specifies is relative to the start point. An upper case A command would specify absolute coordinates for its end point.

(Later we show how the waves can be rotated to match with the flow of the river.)

How It Renders

SVG allows graphical elements to undergo coordinate system transformations using the transform attribute.

Common transformations include:

This section shows examples of translating and rotating.
The code below produces the same water jug representation as before, but:

How It Renders

In this example we use a transform to rotate the path representing the waves of the river in the farmer, wolf, goat, and cabbage problem so that the waves match the flow of the river.

How It Renders

Using the various SVG animation elements, you can define motion paths, fade-in or fade-out effects, and objects that grow, shrink, spin or change color.

We will describe the animate and animateMotion elements.

The animate element is used to animate a single attribute or property over time.

It is specified as a child element of a graphical element whose attribute(s) you wish to animate.

The animate element has attributes for specifying, among other things,

In the animation shown to the right, the rect elements are animated by changing their height attributes from zero to a target value over a duration of 0.5 seconds.

The animation is initiated by clicking a rect element object. This event is specified by the begin attribute. Note:

The code is shown below.

How It Renders

For the purposes of animating water jugs, the example so far is insufficient, since we want the blue rectangles to grow from the bottom of the jugs.

We can achieve this effect by rotating each rectangle 180 degrees about its center — (40,40) for jug X and (40,80) for jug Y.

Note that the rotation point for each rectangle is relative to the translated coordinate system.

The code below shows such rotations added to the transform attribute of the rectangles.

How It Renders

The animateMotion element animates its parent graphics element by moving it along a motion path like the one shown here in black in the middle of the river in the farmer, wolf, goat, and cabbage problem:

The animateMotion element has attributes for specifying, among other things,

In the code below, the path element representing the river waves is animated by sending it down the motion path. Note:

How It Renders

SVG is an XML technology, and the SVG code examples shown so far have been pure XML.

SVG elements, like ordinary HTML elements, are part of the DOM (Document Object Model) and thus manipulable by scripting languages.

This section describes how the SVG elements depicting the problems shown in the Examples menu item to the left are manipulated using Javascript. As of this writing, SVG had not been fully integrated with JQuery, so the code used will be plain Javascript.

Recall our last Javascript/JQuery implementation of the farmer, wolf, goat, cabbage and water jug problems, shown below.

The main HTML page presents the problem's current state by placing state objects' toString return values in an HTML textarea element:

To accommodate graphics, we replace the textarea element with a div container element whose child will be an appropriate svg element:

Since textarea elements are no longer used to display states, the framework code that set the text area size attributes goes away.

The textarea elements are replaced by div elements containing svg elements that draw individual states:

These changes are sketched in the framework code shown here:
The new solve.html and solve.js files are shown in the submenu.

We will show the application code's svg methods after considering how to create SVG elements using Javascript.

This section shows how to create SVG elements under Javascript control.
Recall the pure XML code below that produces the water jug image to the right.

How It Renders

To create an svg element or one of its children, use the document object's createElementNS method. To give the created element attributes, use the element's setAttribute method.

To make an element e1 a child of another element e2, use e2's appendChild method.

Here is the HTML and Javascript code that creates the water jug image:

manual-svg.js

How It Renders

Manual creation of SVG elements is tedious and error-prone, so we will create an SVG element creation utility.

It exploits two features of Javascript:

For example, in the code below,
  1. An object attrs is created whose properties correspond to the attributes of the polyline element used to draw jug X
  2. A polyline element is created
  3. The element's attributes are set by iterating through attr's properties
Since making SVG elements and setting their attributes is a common operation, we define a makeSVG function that takes a tag name and attributes object as parameters and returns an appropriately created element object:

make-svg.js

Here is the HTML and Javascript code that creates the previous water jug image using the makeSVG utility function:

utility-svg.js

How It Renders

The SVG Document Object Model (DOM) allows registration of event listeners for SVG graphics elements.

For example, in the 8-puzzle application, the rect elements representing 8-puzzle tiles have event handlers that attempt to move them into the blank space when clicked.

The SVG DOM is similar to the HTML DOM, and event handling is similar:

Here is the Javascript code that attaches a click handler to a tile:
The only application domain changes are to the constructor functions WaterJugState, FarmerState, and PuzzleState.