If I programatically create an SVG element, can I programatically save that element as an image?
Solutions I've researched on Stackoverflow and Google so far:
Have the user
right-click-save-as
3rd party libs can convert
svg
commands tocanvas
commands and save the canvas with canvas.toDataURL
Is there a direct way of programatically converting SVG to an image?
Example Javascript/SVG:
var svgns="http://www.w3.org/2000/svg";
var xlink='http://www.w3.org/1999/xlink'
var svg=document.createElementNS(svgns,"svg");
svg.setAttribute('width', 100);
svg.setAttribute('height', 100);
var e=document.createElementNS(svgns,"rect");
e.setAttribute("fill","skyblue");
e.setAttribute("stroke","lightgray");
e.setAttribute("stroke-width",4);
e.setAttribute("x",20);
e.setAttribute("y",20);
e.setAttribute("width",50);
e.setAttribute("height",50);
svg.appendChild(e);
document.body.appendChild(svg);