Webby DOM stuff
[ipdf/sam.git] / chapters / Background_DOM.tex
index bb5d169..f6adfd4 100644 (file)
@@ -1,12 +1,28 @@
-
 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 DOM is described within several W3C standards including XML, HTML and SVG. XML is a general language which is intended for representing any tree-like structure using the DOM, whilst languages such as HTML\cite{citationneeded} and SVG\cite{citationneeded} are specifically intended for representing visual information. SVG is defined as a type of XML, whilst HTML is tecnically distinct from XML. Both HTML and XML are supposedly be based upon a language called SGML which is not actually defined \rephrase{except in the foulest chapters of the necromonicom}. Then there is XHTML which is HTML but also XML. \rephrase{Cut the crap about what is and is not valid XML? We only care about the DOM we're not fucking web designers here}.
+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 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.
+
 
-The HyperText Markup Language (HTML) was, as its name implies, originally intended mostly for text. When combined with Cascading Style Sheets (CSS) control over the positioning and style of the text can be acheived. Images stored in some other format can be rendered within a HTML document, but HTML does not include ways to specify graphics primitives or their coordinates.
 
-The Scalable Vector Graphics (SVG) standard was designed to represent a vector image. 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 image defined using the SVG format. The first two lines are only required for a stand alone image; the HTML standard allows for rendering an SVG within a web page using the \verb/<svg>/ tag. \rephrase{Read the standards a bit more carefully here...}
+\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{koch, goldman_fractal}. Unfortunately as it is currently not possible to include HTML within 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.3\textwidth}
@@ -29,7 +45,7 @@ The Scalable Vector Graphics (SVG) standard was designed to represent a vector i
        x = "30" y = "20" width = "30" height = "150"
        style = "fill:#0000ff; fill-opacity:0.5; 
                stroke:#000000;"/>
-<path id="path"
+<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 
@@ -43,13 +59,28 @@ The Scalable Vector Graphics (SVG) standard was designed to represent a vector i
 </svg>
 \end{minted}
 \end{minipage}
-       \caption{A vector image demonstrating some ideas from Section\ref{Rasterising Vector Images} and it's representation in SVG}\label{SVG}
+       \caption{Vector image and a possible SVG representation. This image also demonstrates concepts discussed in Section \ref{Rasterising Vector Images} including Spline Curves (\ref{}) and Compositing (\ref{})}\label{SVG}
 \end{figure}
 
-
-\subsubsection{Modifying the DOM --- Javascript}
-
-Javascript is now ubiquitous in web based documents, and is essentially used to make the DOM interactive. This can be done by altering the attributes of elements, or adding and removing elements from the DOM, in response to some event such as user input or communication with a HTTP server. \rephrase{For some really shitty examples of this, see \url{http://szmoore.net/ipdf/sam/figures/rabbit.html} and \url{http://szmoore.net/ipdf/sam/figures/shape.html}}.
-
-In the HTML5 standard, it is also possible to draw directly to a region of the document defined by the \verb/<canvas>/ tag; as Hayes points out, this is similar to the use of PostScript in specifying the appearance of a document using low level drawing operators which are read by an interpreter. \rephrase{Say some stuff about WebGL or get as far away from this topic as possible while we still can?}
+\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}
 

UCC git Repository :: git.ucc.asn.au