Skip to content

Instantly share code, notes, and snippets.

@round
Forked from gustavohenke/svg2png.js
Created January 15, 2019 05:41
Show Gist options
  • Select an option

  • Save round/76d2fa9839f7fca5ecdf87452600d0b2 to your computer and use it in GitHub Desktop.

Select an option

Save round/76d2fa9839f7fca5ecdf87452600d0b2 to your computer and use it in GitHub Desktop.

Revisions

  1. Gustavo Henke created this gist Feb 18, 2014.
    15 changes: 15 additions & 0 deletions svg2png.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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" ) );
    };