Skip to content

Instantly share code, notes, and snippets.

@sh-ravan
Created December 21, 2022 07:20
Show Gist options
  • Save sh-ravan/9da7fc2724fa19cb3647cc8354fca7fe to your computer and use it in GitHub Desktop.
Save sh-ravan/9da7fc2724fa19cb3647cc8354fca7fe to your computer and use it in GitHub Desktop.
Asynchronously rotate base64 image (Javascript)
const rotate = async (srcBase64, degrees = 90) => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const image = new Image();
image.src = srcBase64;
await image.decode();
canvas.width = degrees % 180 === 0 ? image.width : image.height;
canvas.height = degrees % 180 === 0 ? image.height : image.width;
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.rotate(degrees * Math.PI / 180);
ctx.drawImage(image, image.width / -2, image.height / -2);
return canvas.toDataURL();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment