Languages
[Edit]
EN

JavaScript - rotate image on canvas

6 points
Created by:
lena
714

In this article, we would like to show you how to rotate image on HTML canvas element using JavaScript.

Quick solution:

 

Practical example

Edit

In this example, we rotate image around image center.

Image rotation around image center using JavaScript.
Image rotation around image center using JavaScript.

Source code:

 

Advanced solution

Edit

Usually, when we rotate images we think about rotation around some point. Using math theorem, image rotation around some point requires 3 transformations: translation, rotation, and translation.

We can write it in a more clear way: T = T1 * R1 * T2

We should read transformations starting from right side, so:

  • T2 - means rotation point translation to (0, 0),
  • R1 - means rotation around point (0, 0),
  • T1 - means translation back to beginning position,
  • T - means final transformations composition required to do rotation around some point.

In this example, we create three functions.

 

See also

Edit
  1. JavaScript - animate image rotation (rotation around: image center, image let-top corner, indicated point)

References

Edit
  1. clearRect() - Web APIs | MDN
  2. save() - Web APIs | MDN
  3. translate() - Web APIs | MDN
  4. rotate() - Web APIs | MDN
  5. drawImage() - Web APIs | MDN
  6. restore() - Web APIs | MDN

Alternative titles

  1. JavaScript - rotate image on HTML canvas element
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - HTML5 canvas tutorial

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join