XML technologies / SVG / Interactive graphics / Creating elements dynamically
(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)
The first step in dynamically changing graphics is modifying existing contents, such as text rather than formatting or positions. This simple example changes a <text> tag’s text to display the current date.
The file contains one <text> tag with the id text01. The <text> tag initially contains the text Textelement, to be replaced with today’s date. The function init creates it with the instruction var heute= new Date(); and assigns it to the variable heute. First retrieve the <text> tag using the method getElementById( ). Then the method getFirstChild( ) retrieves the first element of the <text> tag: the specified text. Reset it with the method setNodeValue( ), passing the new value as a parameter.
<?xml version="1.0" encoding="iso-8859-1"?>
...
<svg width="320" height="300" onload="init(evt)">
<script type="text/ecmascript">
<![CDATA[
var elem;
var svgdok;
var eingabe="";
function init(evt)
{
var i=0;
var svgTag;
var styleAttr;
var heute= new Date();
svgdok=evt.getTarget().getOwnerDocument();
svgTag=svgdok.getFirstChild();
elem=svgdok.getElementById("text01");
elem.getFirstChild().setNodeValue(heute);
}
]]>
</script>
<text id="text01" x="10" y="40">Textelement</text>
</svg>
Figure: Date created and displayed.
When using SVG graphics for navigation, such as a small sitemap, create elements only when needed. This reduces loading time, improves design, and simplifies extension and modification. The following example shows how.
It uses a simple navigation bar of rectangles and text. Clicking creates and displays child elements; clicking the second item also removes those created for the previous item.
Figure: Completed dynamic sitemap in Internet Explorer.
The following SVG code defines the necessary elements and enables stylesheet formatting.
<?xml version="1.0" encoding="iso-8859-1"?> ...
<svg width="440" height="300" onload="init(evt)">
<style type="text/css">
<![CDATA[
text {fill:black;font-family:Arial,Helvetica;font-size:14px;font-stretch:ultra-expanded;font-weight:bold}
text.unterelement {fill:black;font-family:Arial,Helvetica;font-size:12px;font-stretch:condensed;font-weight:bold}
rect {fill:none;stroke:none;stroke-width:1px}
rect.unterelement {fill:url(#linearerVerlauf);stroke:black;stroke-width:1px;display:true}
a {fill:red;}
]]>
</style>
<defs>
<linearGradient id="linearerVerlauf" gradientTransform="rotate(45)">
<stop offset="0%" style="stop-color:white"/>
<stop offset="100%" style="stop-color:gray"/>
</linearGradient>
</defs>
<rect x="10" y="10" width="420" height="30" style="fill:url(#linearerVerlauf);stroke:black;stroke-width:1px"/>
<g transform="translate(10,10)" id="kontakt" onclick="ausklappen(evt)">
<rect x="0" y="0" width="120" height="30"/>
<text x="2px" y="20px">Kontakt</text>
</g>
<g transform="translate(130,10)" id="inhalte" onclick="ausklappen(evt)">
<rect x="0" y="0" width="120" height="30"/>
<text x="2px" y="20px">Inhalte</text>
</g>
<g transform="translate(250,10)" id="projekte" onclick="ausklappen(evt)">
<rect x="0" y="0" width="120" height="30"/>
<text x="2px" y="20px">Projekte</text>
</g>
</svg> Group each text with its containing rectangle and name the groups sensibly. The exact names do not matter, but changing those shown requires adjusting the source code.
Note:
Because this example is complex, only its main code elements are explained below.
Copyright © verlag moderne industrie GmbH 2001
You may print the online version for your private use.
Otherwise, this chapter from “Das Einsteigerseminar SVG — Webgrafiken mit XML” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
verlag moderne industrie GmbH, Justus-von-Liebig-Straße 1, D-86899 Landsberg/Lech, fon ++49 (0) 81 91 / 125 – 0, leserservice(at)mi-verlag.de
back to top