Skip to content

Instantly share code, notes, and snippets.

@LeaVerou
Created April 5, 2013 20:58
Show Gist options
  • Save LeaVerou/5322586 to your computer and use it in GitHub Desktop.
Save LeaVerou/5322586 to your computer and use it in GitHub Desktop.
Draw SVG in canvas
/**
* Draw SVG in canvas
*/
<canvas width="400" height="400"></canvas>
var canvas = document.querySelector('canvas'),
ctx = canvas.getContext('2d');
var img = new Image;
img.src = 'http://www.webplatform.org/errors/images/logo-2.svg';
img.onload = function() {
var canvasW = canvas.width, canvasH = canvas.height;
var imgW = img.naturalWidth || canvasW, imgH = img.naturalHeight || canvasH;
var ratio = canvasW/imgW;
console.log(imgW, imgH, canvasW, canvasH);
ctx.drawImage(img, 0, 0, imgW * ratio, imgH*ratio);
}
{"view":"split","fontsize":"100","seethrough":"","prefixfree":"1","page":"javascript"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment