Skip to content

Instantly share code, notes, and snippets.

@taivare
Forked from trtg/_.md
Created March 30, 2016 03:15
Show Gist options
  • Save taivare/cf30117ff46df4c5baacc6c0e4216ea1 to your computer and use it in GitHub Desktop.
Save taivare/cf30117ff46df4c5baacc6c0e4216ea1 to your computer and use it in GitHub Desktop.
how to serialize d3 svgs
{"endpoint":"","display":"svg","public":true,"require":[],"tab":"edit","display_percent":0.5245512449334112,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01}
var svg=d3.select("svg");
svg.append("rect")
.attr({
width:200,
height:200,
x:100,
y:100,
fill: "#1A9224",
})
svg.append("image")
.attr({
width:200,
height:200,
"xlink:href":"https://twimg0-a.akamaihd.net/profile_images/2560186719/7f57h85fjrqelks62v75.jpeg",
x:325,
y:100
})
function cerealize(node){
var svgxml=(new XMLSerializer()).serializeToString(node);
if($.browser.webkit){
svgxml=svgxml.replace(/ xlink:xlink/g,' xmlns:xlink');
svgxml=svgxml.replace(/ href/g, 'xlink:href');
}
return svgxml;
}
//usage: this will output a string representation of the svg element
//generated by the javascript code which you can save to an .svg
//and view in your browser
var exp=cerealize(svg.node());
console.log(exp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment