Did you know that modern browsers already implement builtin support to visualize SVG drawings? And that you can make changes on them from the HTML document and make changes into the document from the SVG? In this post I’m going to explain all that through a simple example.
To create the SVG document you can use Inkscape. This program has a nice feature that allows you to edit attributes of particular nodes. For instance, you can select such a node, open the XML editor (Edit –> XML Editor…) and add a new attribute
onclick to the node with the value:
When you’ve finished, just save the document as “plain SVG” to prevent compatibility problems.
Let’s go to the HTML part now. The easiest way to show the SVG in the document is by using an “embed” tag:
<html> <body> <h1>Anatomy of a smiley</h1> <form name="form1"> Body part: <input type="text" name="bodypart" /> <br/> </form> <embed id="smiley" src="smiley.svg" width="745" height="1053" /> </body> </html>
And that’s all. Now you have bidirectional knowledge between your HTML document and your SVG document. Take a look here to see the whole example.
There are still pending issues, like embedding the SVG code as part of the HTML source. That can be done including the proper DTDs and making sure that the document is interpreted as XHTML (that’s the most important think). Here is a modified version of the previous example to illustrate that, but I hadn’t been able to resolve nodes using this kind of embedded drawings.