Stories on maker education and innovation 

Home creativity Creating Interactive Images with SVG
formats

Creating Interactive Images with SVG

Inkscape & SVG

 

During a Google Developers Group session on HTML5, Dr. Allen introduced the group to a web technology called Scalable Vector Graphics (SVG). In instructional technology, SVG can be used to create interactive diagrams that respond to mouse clicks and mouse movements. Many information visualization tools utilize SVG to visualize complex data sets. (Check out http://d3js.org for examples) As a game programmer for the web, I’m excited to see people write browser based games with SVG. For example, check out this nice implementation of Tetris. .

SVG was created by the web community to enable browsers to draw images that keep their fidelity whether they are displayed on a large device or small. SVG accomplishes this purpose by providing commands for drawing shapes, lines, and paths.

In this post, I’d like to introduce this web technology to our readers using a festive example: A silly Christmas Card. In the image displayed below, an “outch” image is displayed when you click on the “angry bird.” If you move your mouse over the snow man image, a “Merry Christmas” image is displayed. Most modern web browsers support drawing SVG files. If you are comfortable with changing HTML, you will have no problem with SVG files.

Most of the SVG Christmas card was designed using a free tool called Inkscape. You can review a very complete users guide here. InkScape runs on Windows, Mac and Linux.

Google Docs users should also note that you can export Google Drawing files to SVG. Under the “file” menu, select “Download as…” followed by “Scalable Vector Graphics(.SVG).”

To make SVG’s interactive, you need to have a basic understanding of editing an SVG file. You can edit SVG files using your favorite text editor. W3Schools.com provides a great introduction to learning SVG and JavaScript from a code perspective.

Code break down for Christmas Card

In the following JavaScript code that appears after SVG tag, we defined a few functions to handle the interactive elements of the file. In Inkscape, I defined a named groups for the “snowMan”, “merryChristmas” message, the “outch” message, and “angryBird.” As we move our mouse over the snowman, we grab a reference to the “merryChristmas” message and display it. As the mouse leaves the snowman, we hide it.

To learn more about JavaScript programming, visit CodeAcademy.com .

If we click on the angry bird, we grab a reference to the “outch” element and display it. We schedule a function call that will hide the “outch” element after 2 seconds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<script type="text/javascript">
    function handleMouseOverForSnowMan()
    {
        var merryChristmas = document.getElementById("merryChristmas");
        merryChristmas.style.display="block";
    }
    function handleMouseOutForSnowMan()
    {
        var merryChristmas = document.getElementById("merryChristmas");
        merryChristmas.style.display="none";
    }

    function handleClickForBird()
    {
        var outch = document.getElementById("outch");
        outch.style.display="block";       
        setTimeout("hideOutch()", 2000);
    }

    function hideOutch()
    {
        var outch = document.getElementById("outch");
        outch.style.display="none";    

    }

</script>

The following code shows how we connected mouse over events to the snowman group. Please note how we named the group using the ‘id’ attribute.

1
2
3
4
5
    <g
       id="snowMan"
      onmouseover="handleMouseOverForSnowMan()"
      onmouseout="handleMouseOutForSnowMan()"
      >

The following code shows how we connected a mouse click event to the angryBird group. Please note how we named the group using the ‘id’ attribute. You can edit the ‘handleClickForBird’ function to load other web pages using ‘window.open’ or anything else you can image.

1
2
3
4
    <g
       id="angryBird"
       onclick="handleClickForBird()"
      >

 

Related Posts

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
© Inspired To Educate
credit