The Document Object Model (DOM) represents a document as a tree like data structure with the document as a root node. The elements of the document are represented as children of either this root node or of a parent element. In addition, elements may have attributes which contain information about that particular element.

The World Wide Web Consortium (W3C) is an organisation devoted to the development of standards for structuring and rendering web pages based on industry needs. The DOM is used in and described by several W3C recommendations including XML\cite{xml2008-1.0}, HTML\cite{html2014-draft} and SVG\cite{svg2011-1.1}. XML is a general language which is intended for representing any tree-like structure using the DOM, whilst HTML and SVG are specifically intended for representing visual information to humans. These languages make use of Cascading Style Sheets (CSS)\cite{css2011-level2} for specifying the appearance of elements.

Version 5 of the Hypertext Markup Language (HTML5) is currently a candidate recommendation which aims to standardise the state of the art in technologies relating to web based documents. In HTML5 it is possible to achieve almost any level of control over both the structure and rendering of a document desirable. In particular, the interpreted language Javascript included in the HTML5 standard can be used to dynamically alter the document as it is viewed in response to user input or other events such as communication with a remote server.

The Scalable Vector Graphics (SVG) recommendation defines a language for representing vector images using the DOM. This is intended not only for stand alone images, but also for inclusion within HTML documents. In the SVG standard, each graphics primitive is an element in the DOM, whilst attributes of the element give information about how the primitive is to be drawn, such as path coordinates, line thickness, mitre styles and fill colours. Figure \ref{SVG} shows an example of an SVG image as rendered (left) and represented as text. The textual representation is syntactically a subset of XML and is similar to HTML.\footnote{The exact details of classification of these languages (such as why HTML cannot be defined as a subset of XML) are beyond the scope of this report.} Here we have used \verb/<rect>/ elements to position rectangles and \verb/<path>/ elements to define a straight line and a filled region bounded by a cubic bezier spline; note that the points and type of curves are defined as a data attribute.



\subsubsection{Javascript and the DOM}

The W3C has produced a primer describing the use of HTML5 Javascript to produce interactive SVG's\cite{w3c2010svghtmlprimer}, and the HTML5 and SVG standards themselves include several examples discussing the use of Javascript to manipulate the DOM.

In Javascript, an element in the DOM can be selected by its type, class, name, or unique identifier, each of which may be specified as an attribute in the original DOM. Once an element is selected Javascript can be used to modify its attributes, add children below it in the DOM, or remove it from the DOM entirely.

For example, the following Javascript acting on the DOM described in Figure \ref{SVG} would change the fill colour of the curved shape from red \verb/#ff0000/ to black \verb/#000000/:
\begin{minted}{javascript}
var node = document.getElementById("curvedshape"); // Find the node by its unique id
node.style.fill = "#000000"; // Change the ``style'' attribute and set the CSS fill colour
\end{minted}

To illustrate the power of this technique we have produced our own example to generate an SVG interactively using HTML. The example generates successive iterations of a particular type of fractal curve first described by Koch\cite{koch1904surune} in 1904 and a popular example in modern literature \cite{goldman_fractal}. Unfortunately as it is currently possible to directly include W3C HTML in a PDF, we are only able to provide some examples of the output as static images in Figure \ref{koch}.

In HTML5, Javascript is not restricted to merely manipulating the DOM to alter the appearance of a document. The \verb/<canvas>/ tag and associated API provide a means to directly set the values of pixels on a display. This sort of low level API is inteded for performance intensive graphical applications such as web based games\footnote{For an example by the author including both the canvas2d and experimental WebGL APIs see \url{http://rabbitgame.net}}. As Hayes points out, there is some similarity between the \verb/<canvas>/ API and the PostScript interpreted approach to drawing\cite{hayes2012pixelsor}.

\begin{figure}[H]
\begin{minipage}[t]{0.65\textwidth}
\begin{minted}{xml}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
	"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<svg id="svg_example" xmlns="http://www.w3.org/2000/svg"
	version="1.1" width="104" height="186">
<path id="straightline" d = "m 0, 0 104, 186" 
	style="stroke:#000000;"/>
<rect id="rect1"
	x = "30" y = "20" width = "30" height = "150"
	style = "fill:#0000ff; fill-opacity:0.5; 
		stroke:#000000;"/>
<path id="curvedshape"
	d = "m 57,185 c 0,0 57,-13 32,-43 -25,-30 -53,2 -25,
		-30 28,-32 52,17 28,-32 -24,-50 -16,44 -35,12 
		-19,-32 13,-64 13,-64 0,0 40,-50 -0,-14 -40,36 
		-94,68 -59,109 35,41 45,62 45,62 z"
	style = "fill:#ff0000; fill-opacity:0.75; 
		stroke:#000000;"/>
<rect id="rect2"
	x = "12" y = "130" width = "60" height = "20"
	style = "fill:#00ff00; fill-opacity:0.5; 
		stroke:#000000;"/>
</svg>
\end{minted}
\end{minipage}
\begin{minipage}[t]{0.3\textwidth}
	\begin{figure}[H]
	\centering
	\includegraphics[width=1\textwidth]{figures/shape.pdf}
	\end{figure}
\end{minipage}
	\caption{Vector image and a possible SVG representation}\label{SVG}
\end{figure}

\begin{figure}[H]
\begin{minipage}[t]{0.33\textwidth}
	\begin{figure}[H]
	\centering
	\includegraphics[width=0.8\textwidth]{figures/koch1.pdf}
	\end{figure}
\end{minipage}
\begin{minipage}[t]{0.33\textwidth}
	\begin{figure}[H]
	\centering
	\includegraphics[width=1\textwidth]{figures/koch2.pdf}
	\end{figure}
\end{minipage}
\begin{minipage}[t]{0.33\textwidth}
	\begin{figure}[H]
	\centering
	\includegraphics[width=0.9\textwidth]{figures/koch3.pdf}
	\end{figure}
\end{minipage}
	\caption{Koch ``snowflakes'' generated using Javascript to modify an SVG DOM. The interactive HTML5 document can be found at \url{http://szmoore.net/ipdf/sam/figures/koch.html}}\label{koch}
\end{figure}


