Skip to content

Instantly share code, notes, and snippets.

@biovisualize
Created September 11, 2011 12:07
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save biovisualize/1209499 to your computer and use it in GitHub Desktop.
Save biovisualize/1209499 to your computer and use it in GitHub Desktop.
Download generated SVG with preview (from mbostock)
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
<title>Download SVG</title>
<style type="text/css">
a{
cursor: pointer;
text-decoration: underline;
color: black;
}
#download{
border: 1px solid silver;
position: absolute;
opacity: 0;
}
</style>
</head>
<body>
<div id="viz"></div>
<a href="#" id="generate">Generate download preview</a>
<script type="text/javascript">
// Modified from https://groups.google.com/d/msg/d3-js/aQSWnEDFxIc/k0m0-q-3h1wJ
d3.select("#viz")
.append("svg:svg")
.attr("width", 300)
.attr("height", 200)
.style("background-color", "WhiteSmoke")
.append("svg:rect")
.attr("fill", "aliceblue")
.attr("stroke", "cadetblue")
.attr("width", 60)
.attr("height", 40)
.attr("x", 50)
.attr("y", 50);
d3.select("#generate")
.on("click", writeDownloadLink);
function writeDownloadLink(){
var html = d3.select("svg")
.attr("title", "test2")
.attr("version", 1.1)
.attr("xmlns", "http://www.w3.org/2000/svg")
.node().parentNode.innerHTML;
d3.select("body").append("div")
.attr("id", "download")
.style("top", event.clientY+20+"px")
.style("left", event.clientX+"px")
.html("Right-click on this preview and choose Save as<br />Left-Click to dismiss<br />")
.append("img")
.attr("src", "data:image/svg+xml;base64,"+ btoa(html));
d3.select("#download")
.on("click", function(){
if(event.button == 0){
d3.select(this).transition()
.style("opacity", 0)
.remove();
}
})
.transition()
.duration(500)
.style("opacity", 1);
};
</script>
</body>
</html>
@molnarg
Copy link

molnarg commented Mar 31, 2013

@andycasey
Copy link

Hi,

This approach seems to fail when MathJaX objects exist. Do you know of any way around that?

@scorpy86
Copy link

I tried to parse an svg containing an image, and does not appear, here is an example of < image > tag:

xlink:href="https://cdn1.iconfinder.com/data/icons/IconsLandVistaPeopleIconsDemo/16/Person_CoffeeBreak_Male_Dark.png" x="-8" y="-8" width="16" height="16" class="nodeImage" display="block"

someone has an idea why i've this problem?

Chrome 30.0.1599.69 m, Windows 8 64bit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment