javascript - Create an SVG DOM element from a String -
how go creating svg dom element string
?
example:
var svgstr = '<svg width="500" height="400" xmlns="http://www.w3.org/2000/svg"><!-- created method draw - http://github.com/duopixel/method-draw/ --><g><title>background</title><rect fill="#fff" id="canvas_background" height="402" width="502" y="-1" x="-1"/><g display="none" overflow="visible" y="0" x="0" height="100%" width="100%" id="canvasgrid"><rect fill="url(#gridpattern)" stroke-width="0" y="0" x="0" height="100%" width="100%"/></g></g><g><title>layer 1</title><path id="svg_1" d="m118,242l64,-153l63,157c0,0 45,-71 49,-68c4,3 11,146 12,146c1,0 -173,-7 -173,-7c0,0 -61,-72 -61,-72c0,0 110,-156 46,-3z" fill-opacity="0.7" stroke-width="2" stroke="#995757" fill="#995757"/></g></svg>';
you can use domparser parse xml string.
var parser = new domparser(); var doc = parser.parsefromstring(stringcontainingxmlsource, "image/svg+xml");
the root element parsed string doc.documentelement
for work cross-browser you'll need set html namespace i.e. string need this...
var svg2='<svg xmlns="http://www.w3.org/2000/svg" width="500" height="500" ...
Comments
Post a Comment