{"id":1978,"date":"2015-01-20T21:13:18","date_gmt":"2015-01-20T21:13:18","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=1978"},"modified":"2015-01-21T01:09:18","modified_gmt":"2015-01-21T01:09:18","slug":"creating-svg-javascript-trivial-example","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2015\/01\/creating-svg-javascript-trivial-example\/","title":{"rendered":"Creating an SVG in Javascript, trivial example"},"content":{"rendered":"<p>I&#8217;m no JS coder, but for my reference below is a simple example showing how to create an SVG image in Javascript. The code below also shows how the SVG element itself might be created in JS.<\/p>\n<p>UPDATE: If you&#8217;re looking at creating SVGs in Javascript, I recommend checking out FabricJS, I <a href=\"http:\/\/41j.com\/blog\/2015\/01\/creating-svg-javascript-using-fabricjs-downloading-locally\/\">found it much easier to get what I wanted working<\/a>.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n\r\n&lt;body&gt;\r\n&lt;script language=&quot;javascript&quot;&gt;\r\n\r\nvar svgNS = &quot;http:\/\/www.w3.org\/2000\/svg&quot;;  \r\n\r\nfunction do_draw() {\r\n\r\n  \/\/ The following commands can be used to create an SVG programatically\r\n  \/\/ var mySVG = document.createElementNS(&quot;http:\/\/www.w3.org\/2000\/svg&quot;, &quot;svg&quot;);\r\n  \/\/ mySVG.setAttribute(&quot;height&quot;,300);\r\n  \/\/ mySVG.setAttribute(&quot;width&quot; ,300);\r\n  \/\/ document.body.appendChild(mySVG);\r\n  \r\n  var mySVG = document.getElementById('mySVG')\r\n\r\n  var circles = document.createElementNS(&quot;http:\/\/www.w3.org\/2000\/svg&quot;, &quot;circle&quot;);\r\n  circles.setAttribute(&quot;cx&quot;,20);\r\n  circles.setAttribute(&quot;cy&quot;,20);\r\n  circles.setAttribute(&quot;r&quot;,20);\r\n  mySVG.appendChild(circles);\r\n}\r\n\r\n&lt;\/script&gt;\r\n\r\n\r\n&lt;svg id=&quot;mySVG&quot; style=&quot;width:300px; height:300px&quot; xmlns=&quot;http:\/\/www.w3.org\/2000\/svg&quot; xmlns:xlink=&quot;http:\/\/www.w3.org\/1999\/xlink&quot;\/&gt;\r\n&lt;input type=&quot;button&quot; id=&quot;drawBtn&quot; value=&quot;draw&quot; onclick=&quot;do_draw()&quot;&gt;&lt;\/input&gt;&lt;br&gt;&lt;br&gt;\r\n&lt;div id='main'&gt;&lt;\/div&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m no JS coder, but for my reference below is a simple example showing how to create an SVG image in Javascript. The code below also shows how the SVG element itself might be created in JS. UPDATE: If you&#8217;re looking at creating SVGs in Javascript, I recommend checking out FabricJS, I found it much [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1978","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-vU","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=1978"}],"version-history":[{"count":3,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1978\/revisions"}],"predecessor-version":[{"id":1983,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1978\/revisions\/1983"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=1978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=1978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=1978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}