XML technologies / SVG / Manipulating graphics with external scripts / Solutions
(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)
To implement a timer using the
setIntervalmethod of thewindowobject, define the function to call at script level outside a function in the SVG file. The required syntax is:window.funktionsname=funktionsname;To define the display
00:00:00, the following code is required in the SVG file. The formatting is, of course, irrelevant:
<text id="uhrzeit" x="10" y="50" style="font-size:36px">00:00:00</text>A function that obtains and displays the current time could look as follows:
<svg width="200px" height="100px" onload="initUhr(evt)">
<script type="text/ecmascript">
<![CDATA[
var svgdok;
var elem;
var zeit;
window.neueZeit=neueZeit;
function initUhr(evt)
{
svgdok=evt.getTarget().getOwnerDocument();
}
function neueZeit()
{
var zeit= new Date();
var stunden=zeit.getHours();
var minuten=zeit.getMinutes();
var sekunden=zeit.getSeconds();
zeitTxt=stunden +":"+minuten+":"+sekunden;
elem=svgdok.getElementById("uhrzeit");
elem.firstChild.setNodeValue(zeitTxt);
}
]]>
</script>
To call the function once every second, use the
setIntervalmethod.
window.neueZeit=neueZeit;
function initUhr(evt)
{
svgdok=evt.getTarget().getOwnerDocument();
setInterval('neueZeit()',1000);
}
To always display minutes, seconds and hours with two digits, extend the function
neueZeitwith threeifstatements.
function neueZeit()
{
var zeit= new Date();
var stunden=zeit.getHours();
var minuten=zeit.getMinutes();
var sekunden=zeit.getSeconds();
if (stunden<10){eval("stunden='0' +stunden")};
if (minuten<10){eval("minuten='0' +minuten")};
if (sekunden<10){eval("sekunden='0' +sekunden")};
zeitTxt=stunden +":"+minuten+":"+sekunden;
elem=svgdok.getElementById("uhrzeit");
elem.firstChild.setNodeValue(zeitTxt);
}
To insert the graphic into an HTML page, use the
<EMBED>tag:
<embed src="KAP11.svg" width="200" height="100" type="image/svg+xml" id="Uhr"/>
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