Skip to content

Instantly share code, notes, and snippets.

@Zyndoras
Last active March 24, 2025 07:53
Show Gist options
  • Select an option

  • Save Zyndoras/6897abdf53adbedf02564808aaab94db to your computer and use it in GitHub Desktop.

Select an option

Save Zyndoras/6897abdf53adbedf02564808aaab94db to your computer and use it in GitHub Desktop.
Rotate base64 image (Javascript)
function rotate(srcBase64, degrees, callback) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const image = new Image();
image.onload = function () {
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);
callback(canvas.toDataURL());
};
image.src = srcBase64;
}
// Usage:
const img = document.getElementById('image');
rotate(img.attributes.src.value, 90, function(resultBase64) {
img.setAttribute('src', resultBase64);
});
@spencergritton

Copy link
Copy Markdown

I love you for this

@braegy

braegy commented Jun 15, 2021

Copy link
Copy Markdown

Love you tooo for this 😍❤

@Bart1909

Bart1909 commented Sep 1, 2021

Copy link
Copy Markdown

Hi,

this is working fine, except, that it is cropping the edges which protrude, when rotation for example 22.5 or 45 degrees.

Do you have any idea, what I have to change, to prevent this?

@Zyndoras

Zyndoras commented Sep 1, 2021

Copy link
Copy Markdown
Author

Hi,

this is working fine, except, that it is cropping the edges which protrude, when rotation for example 22.5 or 45 degrees.

Do you have any idea, what I have to change, to prevent this?

Hi @Bart1909

Uuh, i only intended this to be used with [90, 180, 270] degrees. That's why the canvas size is calculated very naively. You would need the canvas.width and canvas.height to match your image's diagonal lengths.

@Davi9130

Davi9130 commented Jun 3, 2022

Copy link
Copy Markdown

Thanks

@acetilholin

Copy link
Copy Markdown

works like a charm!

@tanihuang

Copy link
Copy Markdown

thanks

@cnhjp

cnhjp commented Sep 23, 2022

Copy link
Copy Markdown

nice! It solved my problem.

@sh-ravan

sh-ravan commented Dec 21, 2022

Copy link
Copy Markdown

Here's an async version of this function that uses HTMLImageElement.decode()

From the docs:

This can be used to initiate loading of the image prior to attaching it to an element in the DOM (or adding it to the DOM as a new element), so that the image can be rendered immediately upon being added to the DOM. This, in turn, prevents the rendering of the next frame after adding the image to the DOM from causing a delay while the image loads.

@ArcherArash

Copy link
Copy Markdown

Thank you!
I just needed these 2 lines:
canvas.width = degrees % 180 === 0 ? image.width : image.height;
canvas.height = degrees % 180 === 0 ? image.height : image.width;

@apipkurniawan

Copy link
Copy Markdown

thank you.

@omedinapr

Copy link
Copy Markdown

Thank you for this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment