XML technologies / SVG / Manipulating graphics with external scripts / Solutions

(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)

  1. To implement a timer using the setInterval method of the window object, define the function to call at script level outside a function in the SVG file. The required syntax is: window.funktionsname=funktionsname;

  2. 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>

  1. 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>

  1. To call the function once every second, use the setInterval method.

window.neueZeit=neueZeit;
function initUhr(evt)
{
  svgdok=evt.getTarget().getOwnerDocument();
  setInterval('neueZeit()',1000);
}

  1. To always display minutes, seconds and hours with two digits, extend the function neueZeit with three if statements.

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); 
}

  1. 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"/>

   

 

 

 

back to top