Last active
October 25, 2021 22:02
-
-
Save rafaelsq/a9426f765d09e09b62f5dca67c3cb68d to your computer and use it in GitHub Desktop.
SVG to Base64 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
Jul 28, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment