Skip to content

Instantly share code, notes, and snippets.

@rlemon
Last active April 3, 2017 17:37
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 rlemon/ee2e168db72f85dc3cbe0e5531281be1 to your computer and use it in GitHub Desktop.
Save rlemon/ee2e168db72f85dc3cbe0e5531281be1 to your computer and use it in GitHub Desktop.
async load image for js
async function loadImage(url) {
const res = await fetch(url, { mode: 'cors' });
const buff = await res.arrayBuffer();
const img = new Image();
const data = btoa([...new Uint8Array(buff)].map(c=>String.fromCharCode(c)).join(''));
img.src = `data:image/jpeg;base64,${data}`;
return img;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment