Skip to content

Instantly share code, notes, and snippets.

@loai-kanou
Forked from LeaVerou/dabblet.css
Created August 5, 2013 03:49
Show Gist options
  • Save loai-kanou/6153377 to your computer and use it in GitHub Desktop.
Save loai-kanou/6153377 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