Convert an Image to dataUri
function toDataUri(img, scalar = 1) {
const canvas = document.createElement('canvas');
canvas.width = img.width * scalar;
canvas.height = img.height * scalar;
canvas.getContext('2d').drawImage(img, 0, 0, canvas.width, canvas.height);
return canvas.toDataURL('image/png');
}
const img = new Image();
img.crossOrigin = 'Anonymous';
img.addEventListener('load', () => {
const thumb = new Image();
// use the data URI as the source
thumb.src = toDataUri(img, .3);
document.body.appendChild(thumb);
});
img.src = 'https://zevanrosser.com/fotoda/slide-shot-9.jpg';
Load an image and convert it to a data URI.