-
-
Save loai-kanou/6153377 to your computer and use it in GitHub Desktop.
Draw SVG in canvas
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
/** | |
* Draw SVG in canvas | |
*/ |
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
<canvas width="400" height="400"></canvas> |
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
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); | |
} |
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
{"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