{"id":84,"date":"2008-11-03T17:30:40","date_gmt":"2008-11-03T16:30:40","guid":{"rendered":"http:\/\/blogs.igalia.com\/eocanha\/?p=84"},"modified":"2015-11-08T00:21:04","modified_gmt":"2015-11-07T23:21:04","slug":"how-to-interact-with-an-svg-image-from-an-html-document","status":"publish","type":"post","link":"https:\/\/eocanha.org\/blog\/2008\/11\/03\/how-to-interact-with-an-svg-image-from-an-html-document\/","title":{"rendered":"How to interact with an SVG image from an HTML document"},"content":{"rendered":"<p>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&#8217;m going to explain all that through a simple example.<\/p>\n<p>To create the SVG document you can use <a href=\"http:\/\/www.inkscape.org\">Inkscape<\/a>. 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 &#8211;&gt; XML Editor&#8230;) and add a new attribute <code>onclick<\/code> to the node with the value:<\/p>\n<pre>\njavascript:window.parent.document.form1.bodypart.value='head';\n<\/pre>\n<p>When you&#8217;ve finished, just save the document as &#8220;plain SVG&#8221; to prevent compatibility problems.<\/p>\n<p>Let&#8217;s go to the HTML part now. The easiest way to show the SVG in the document is by using an &#8220;embed&#8221; tag:<\/p>\n<blockquote>\n<pre>\n&lt;html&gt;\n&lt;body&gt;\n\n&lt;h1&gt;Anatomy of a smiley&lt;\/h1&gt;\n\n&lt;form name=\"form1\"&gt;\nBody part: &lt;input type=\"text\" name=\"bodypart\" \/&gt; &lt;br\/&gt;\n&lt;\/form&gt;\n\n&lt;embed id=\"smiley\" src=\"smiley.svg\" width=\"745\" height=\"1053\" \/&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<\/blockquote>\n<p>But that&#8217;s not enough. If you want to  change SVG attributes using javascript, just add this code:<\/p>\n<blockquote>\n<pre>\n&lt;script type=\"text\/javascript\"&gt;\n   function changeColor(color) {\n   var path=document.getElementById(\"smiley\")\n      .getSVGDocument()\n      .getElementById(\"path2383\");\n   path.style.setProperty(\"fill\",color, \"\");\n   }\n&lt;\/script&gt;\n\n[...]\n\nColor: &lt;input type=\"text\" name=\"color\" value=\"#FFFF00\" \/&gt; &lt;br\/&gt;\n\n&lt;input type=\"button\" value=\"Change color\"\n onclick=\"javascript:changeColor(this.form.color.value);\"\/&gt; &lt;br\/&gt;\n<\/pre>\n<\/blockquote>\n<p>And that&#8217;s all. Now you have bidirectional knowledge between your HTML document and your SVG document. <a href=\"\/media\/wordpress\/test.html\">Take a look here to see the whole example<\/a>.<\/p>\n<p>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&#8217;s the most important think). <a href=\"\/media\/wordpress\/test_inline.xhtml\">Here is a modified version of the previous example to illustrate that<\/a>, but I hadn&#8217;t been able to resolve nodes using this kind of embedded drawings.<\/p>\n<p><a href=\"\/media\/wordpress\/smiley_svg.png\"><img src=\"\/media\/wordpress\/smiley_svg_thumb.png\" border=\"0\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;m going to explain all that through a simple example. To create the SVG document you can &hellip; <a href=\"https:\/\/eocanha.org\/blog\/2008\/11\/03\/how-to-interact-with-an-svg-image-from-an-html-document\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to interact with an SVG image from an HTML document<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/posts\/84"}],"collection":[{"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/comments?post=84"}],"version-history":[{"count":1,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/posts\/84\/revisions"}],"predecessor-version":[{"id":420,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/posts\/84\/revisions\/420"}],"wp:attachment":[{"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/media?parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/categories?post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/tags?post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}