Created
September 7, 2017 07:15
-
-
Save bhaltair/37693c9e583e8827a274f43c6f12710a to your computer and use it in GitHub Desktop.
image2canvas
This file contains hidden or 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
function toDataURL(src, callback, outputFormat) { | |
var img = new Image(); | |
img.crossOrigin = 'Anonymous'; | |
img.onload = function() { | |
var canvas = document.createElement('CANVAS'); | |
var ctx = canvas.getContext('2d'); | |
var dataURL; | |
canvas.height = this.naturalHeight; | |
canvas.width = this.naturalWidth; | |
ctx.drawImage(this, 0, 0); | |
dataURL = canvas.toDataURL(outputFormat); | |
callback(dataURL); | |
}; | |
img.src = src; | |
if (img.complete || img.complete === undefined) { | |
img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="; | |
img.src = src; | |
} | |
} | |
toDataURL( | |
'https://www.gravatar.com/avatar/d50c83cc0c6523b4d3f6085295c953e0', | |
function(dataUrl) { | |
console.log('RESULT:', dataUrl) | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment