Convert svg to canvas, and download in PNG or PDF. Use canvg, jspdf, html2canvas.
Last active
September 7, 2017 08:32
-
-
Save SevenChan07/bfc0cbffad7847845a2dfb123a39aa92 to your computer and use it in GitHub Desktop.
download svg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 | |
height: 700 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
margin: 20px 0px 0px 50px | |
} | |
#pie { | |
display: inline | |
} | |
</style> | |
<script type="text/javascript" src="//d3js.org/d3.v4.min.js"></script> | |
<script type="text/javascript" src="http://canvg.github.io/canvg/canvg.js"></script> | |
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.min.js"></script> | |
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script> | |
</head> | |
<body> | |
<div> | |
<button onclick="drawCanvas()">drawCanvas</button> | |
<button onclick="downloadPNG()">downloadPNG</button> | |
<button onclick="downloadPDF()">downloadPDF</button> | |
</div> | |
<div id="pie"> | |
<svg></svg> | |
</div> | |
<canvas id="canvasId"></canvas> | |
</body> | |
<script type="text/javascript"> | |
let width = 400, | |
height = 400, | |
radius = 150 | |
let data = [1, 1, 2, 3, 5, 8, 13, 21] | |
let color = d3.scaleOrdinal(d3.schemeCategory20) | |
let arc = d3.arc() | |
.outerRadius(radius) | |
.innerRadius(0) | |
let pie = d3.pie() | |
let svg = d3.select('svg') | |
.attr('width', width) | |
.attr('height', height) | |
.append('g') | |
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")") | |
svg.append('text') | |
.attr('text-anchor', 'middle') | |
svg.selectAll('path') | |
.data(pie(data)) | |
.enter() | |
.append('path') | |
.attr("fill", function(d, i) { return color(i) }) | |
.attr("d", arc) | |
.transition() | |
.ease(d3.easeExpInOut) | |
.duration(1000) | |
.attrTween("d", tweenPie) | |
function tweenPie(b) { | |
b.innerRadius = 0 | |
let i = d3.interpolate({startAngle: 0, endAngle: 0}, b) | |
return function(t) { return arc(i(t)) } | |
} | |
let filename = 'pie' | |
let canvas = document.getElementById('canvasId') | |
function drawCanvas() { | |
let svgHtml = document.getElementById('pie').innerHTML.trim() | |
canvg(canvas,svgHtml) | |
} | |
function downloadPNG() { | |
let url = canvas.toDataURL('image/png') | |
let link = document.createElement('a') | |
link.href = url | |
link.download = filename | |
document.body.appendChild(link) | |
link.click() | |
} | |
function downloadPDF() { | |
// use html2canvas | |
html2canvas(canvas, { | |
onrendered: function(canvas) { | |
var imgData = canvas.toDataURL('image/png') | |
var doc = new jsPDF() | |
doc.addImage(imgData, 'PNG', 10, 10) | |
doc.save('pie.pdf') | |
} | |
}) | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment