Skip to content

Instantly share code, notes, and snippets.

@tborychowski
Created November 29, 2021 11:14
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 tborychowski/5a67056eac6970248c46a19ab4dd9f1b to your computer and use it in GitHub Desktop.
Save tborychowski/5a67056eac6970248c46a19ab4dd9f1b to your computer and use it in GitHub Desktop.
JS :: convert image to data url with javascript
console.clear();
var img = new Image();
img.src = 'img/img.png';
img.onload = function () {
var canvas = document.createElement('canvas'), context = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0, img.width, img.height);
console.log(canvas.toDataURL('image/png'));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment