How to convert svg code into svg string?

1,285 views
Skip to first unread message

melaga...@gmail.com

unread,
Dec 25, 2013, 11:26:41 AM12/25/13
to sna...@googlegroups.com
Hi,

Any method for converting standard svg code into svg string?
Thanks for any idea!

Ian

unread,
Dec 26, 2013, 11:22:34 AM12/26/13
to sna...@googlegroups.com
Can you clarify a bit what you are actually trying to do, and how you are differentiating svg code (do you mean svg markup, or Snap code?) from svg string ?

.innerSVG may help, but not quite sure what you are after, to know.

Ian

melaga...@gmail.com

unread,
Dec 28, 2013, 6:25:41 PM12/28/13
to sna...@googlegroups.com
To extract all the paths plus their attr and leave the rest of the svg ?

Ian

unread,
Dec 29, 2013, 4:52:26 AM12/29/13
to sna...@googlegroups.com
You could just do something like 

var allPaths = someElement.selectAll('path'); which you can then access each element and their attr.

Ian

melaga...@gmail.com

unread,
Dec 29, 2013, 6:14:28 PM12/29/13
to sna...@googlegroups.com
Thanks Ian,

How can I get the output result of that function to copy that and past all the paths into a note pad to save it for future use?

Ashish Desai

unread,
Dec 31, 2013, 6:11:51 PM12/31/13
to sna...@googlegroups.com
For your issue, you can get it like below:
var arr = new Array();;
         
var allPaths = someElement.selectAll('path');
         allPaths.forEach(function(item,index) {
            arr.push(item.toString());
            return true;
         }, allPaths);
         console.log(arr.join('\n'));

Output (something like below):
<circle cx="50" cy="50" r="50" fill="#64e600" style=""/> <circle cx="50" cy="50" r="50" fill="#ff0000" mask="url('#Shpvrov896')" style=""/> <circle cx="251.5" cy="125" r="45" fill="#f5e2c6" style=""/> <circle cx="224" cy="112" r="6" fill="#000000" style=""/> <circle cx="295" cy="5" r="5" fill="#ffffff" style=""/> <circle cx="251.5" cy="247" r="10" fill="#f5e2c6" style=""/>

You can get the SVG string of any element element using element.toString() method. it will output contents like:
         var txtpth = s.path("M70 70 Q 80 90 200 150 L 200 400").attr({
            fill: "none",
            stroke: "black"
         });
         console.log(txtpth.toString());
Output:
<path d="M70 70 Q 80 90 200 150 L 200 400" fill="none" stroke="#000000" style=""/>

Let me know if this helps. If you want to print the whole SVG that is generated, use the <yoursnapobject>.toString() method.
Reply all
Reply to author
Forward
0 new messages