Last active
March 31, 2016 09:00
-
-
Save erdii/8fcc29d0bdb62e6c05b0503da8a45e35 to your computer and use it in GitHub Desktop.
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
// spotted @ https://davidwalsh.name/convert-image-data-uri-javascript | |
function getDataUri(url, callback) { | |
var image = new Image(); | |
image.onload = function () { | |
var canvas = document.createElement("canvas"); | |
canvas.width = this.naturalWidth; | |
canvas.height = this.naturalHeight; | |
canvas.getContext("2d").drawImage(this, 0, 0); | |
var dataUrl = canvas.toDataURL("image/png"); | |
callback(dataUrl); | |
}; | |
image.src = url; | |
} | |
// use it like | |
getDataUri("url/to/image", function(dataUrl) { | |
// do something with dataUrl | |
console.log(dataUrl); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment