Created
February 18, 2020 11:49
-
-
Save rafaelsq/665c48fb0fc58434b4e13b70717386cc to your computer and use it in GitHub Desktop.
SVG to PNG
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
function svg_to_png(svg, callback) { | |
var svgData = (new XMLSerializer()).serializeToString(svg); | |
var canvas = document.createElement("canvas"); | |
var svgSize = svg.getBoundingClientRect(); | |
canvas.width = svgSize.width; | |
canvas.height = svgSize.height; | |
var ctx = canvas.getContext("2d"); | |
var img = document.createElement("img"); | |
img.setAttribute("src", "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(svgData))) ); | |
img.onload = function() { | |
ctx.drawImage(img, 0, 0); | |
callback(canvas.toDataURL("image/png")) | |
}; | |
} |
Author
rafaelsq
commented
Feb 18, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment