Is there a way to rotate the existing content of HTML5 canvas by Javascript? I know it's possible to rotate an image that will be drawn on to canvas, but I want to rotate the content that has been drawn on to canvas, for example, a 200x200 corner of a 400x400 canvas, or any specific region of an existing canvas.
Same question to scale the existing canvas content...
I know getImageData/putImageData provide a potential to transform the pixel array, but it's just too slow and inefficient.
If you first want to draw on a
canvas
and then rotate it for use on e.g. corners, you can to that when you "clone" the canvas or by using CSS.Examples
Get the first canvas element:
draw on it:
clone it to another canvas (that is rotated by CSS):
or rotate the canvas while you "clone" it:
here is the CSS for rotating it:
Here is the full example:
It's pretty easy to do with a temp canvas.
Live Demo
Live Demo Animated (just for the heck of it)
The above example draws 2 boxes, then rotates and scales from 0,0 to 200,200