Created
April 7, 2020 11:24
-
-
Save hn3000/16808f6f3938576033ea534de6b5f1f0 to your computer and use it in GitHub Desktop.
Convert an image URL to a Data URL with this one simple trick ...
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
function createDataUrl(icon: string): Promise<string> { | |
const result = new Promise<string>((resolve, _reject) => { | |
let image = new Image(); | |
image.addEventListener('load', () => { | |
let canvas = document.createElement('canvas'); | |
canvas.width = image.naturalWidth; | |
canvas.height = image.naturalHeight; | |
canvas.getContext('2d').drawImage(image, 0, 0); | |
resolve (canvas.toDataURL('image/png')); | |
}); | |
setTimeout(() => {resolve(icon)}, 2000); | |
image.src = icon; | |
}); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment