Creating an SVG in Javascript, trivial example
I’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’re looking at creating SVGs in Javascript, I recommend checking out FabricJS, I found it much easier to get what I wanted working.
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 28 29 30 31 32 33 34 | < html > < body > < script language = "javascript" > var svgNS = "http://www.w3.org/2000/svg"; function do_draw() { // The following commands can be used to create an SVG programatically // var mySVG = document.createElementNS("http://www.w3.org/2000/svg", "svg"); // mySVG.setAttribute("height",300); // mySVG.setAttribute("width" ,300); // document.body.appendChild(mySVG); var mySVG = document.getElementById('mySVG') var circles = document.createElementNS("http://www.w3.org/2000/svg", "circle"); circles.setAttribute("cx",20); circles.setAttribute("cy",20); circles.setAttribute("r",20); mySVG.appendChild(circles); } </ script > < svg id = "mySVG" style = "width:300px; height:300px" xmlns = "http://www.w3.org/2000/svg" xmlns:xlink = "http://www.w3.org/1999/xlink" /> < input type = "button" id = "drawBtn" value = "draw" onclick = "do_draw()" ></ input >< br >< br > < div id = 'main' ></ div > </ body > </ html > |