Skip to content

Instantly share code, notes, and snippets.

@gongzhitaao
Forked from gustavohenke/svg2png.js
Created May 7, 2014 04:30
Show Gist options
  • Save gongzhitaao/0b6113b09d165fc4c667 to your computer and use it in GitHub Desktop.
Save gongzhitaao/0b6113b09d165fc4c667 to your computer and use it in GitHub Desktop.
var svg = document.querySelector( "svg" );
var svgData = new XMLSerializer().serializeToString( svg );
var canvas = document.createElement( "canvas" );
var ctx = canvas.getContext( "2d" );
var img = document.createElement( "img" );
img.setAttribute( "src", "data:image/svg+xml;base64," + btoa( svgData ) );
img.onload = function() {
ctx.drawImage( img, 0, 0 );
// Now is done
console.log( canvas.toDataURL( "image/png" ) );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment