Created
October 13, 2021 22:09
-
-
Save OlivierJM/e296c846b88927b9371c422d8758cd95 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const createImage = url => { | |
return new Promise((resolve, reject) => { | |
const image = new Image(); | |
image.addEventListener('load', () => resolve(image)); | |
image.addEventListener('error', error => reject(error)); | |
image.setAttribute('crossOrigin', 'anonymous'); | |
image.src = url; | |
}); | |
}; | |
export const getCroppedImg = async (imageSrc, crop) => { | |
const image = await createImage(imageSrc); | |
const canvas = document.createElement('canvas'); | |
const ctx = canvas.getContext('2d'); | |
/* setting canvas width & height allows us to | |
resize from the original image resolution */ | |
canvas.width = 450; | |
canvas.height = 116; | |
ctx.drawImage(image, crop.x, crop.y, crop.width, crop.height, 0, 0, canvas.width, canvas.height); | |
return new Promise(resolve => { | |
canvas.toBlob(blob => { | |
resolve(blob); | |
}, 'image/png'); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment