HTML canvas rotate() Method
The HTML canvas rotate() Method is used to rotate the drawing by given angle. Note that the rotation will only work on those canvas that made after the rotation was done.
Syntax:
context.rotate(angle)
Parameter Values:
- angle: It store the rotation angle in radian. If the angle is give in form of degree then it convert into radian by using formula degree*Math.PI/180.
Example 1:
<!DOCTYPE html> <html> <head> <title> HTML canvas rotate() Method </title> </head> <body> <center> <h1 style="color:green">GeeksforGeeks</h1> <canvas id="GFG" width="500" height="300" style="border:2px solid gray;"> </canvas> <script> var geeks = document.getElementById("GFG"); var contex = geeks.getContext("2d"); contex.strokeRect(100, 50, 150, 100); contex.rotate(30 * Math.PI / 180); contex.strokeRect(100, 50, 150, 100); </script> </center> </body> </html> |
chevron_right
filter_none
Output:

Example 2:
<!DOCTYPE html> <html> <head> <title> HTML canvas rotate() Method </title> </head> <body> <center> <h1 style="color:green">GeeksforGeeks</h1> <canvas id="GFG" width="500" height="300" style="border:2px solid gray;"> </canvas> <script> var geeks = document.getElementById("GFG"); var contex = geeks.getContext("2d"); contex.rect(100, 100, 150, 100);//acctual rectangle contex.stroke(); contex.rotate((-20) * Math.PI / 180); contex.rect(100, 100, 150, 100);//rotate rectangle contex.stroke(); </script> </center> </body> </html> |
chevron_right
filter_none
Output:

Supported Browsers:
- Google Chrome
- Internet Explorer 9.0
- Firefox
- Safari
- Opera
Recommended Posts:
- HTML | canvas arc() Method
- HTML | canvas beginPath() Method
- HTML | canvas clearRect() Method
- HTML | canvas isPointInPath() Method
- HTML | canvas createImageData() Method
- HTML | canvas setTransform() Method
- HTML | canvas closePath() Method
- HTML | canvas strokeText() Method
- HTML | canvas stroke() Method
- HTML | canvas fill() Method
- HTML | canvas putImageData() Method
- HTML | canvas getImageData() Method
- HTML | canvas strokeRect() Method
- HTML | canvas translate() Method
- HTML | canvas fillRect() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



