Skip to content

Instantly share code, notes, and snippets.

@hn3000
Created April 7, 2020 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hn3000/16808f6f3938576033ea534de6b5f1f0 to your computer and use it in GitHub Desktop.
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 ...
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