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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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


Output:

Supported Browsers:

  • Google Chrome
  • Internet Explorer 9.0
  • Firefox
  • Safari
  • Opera


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.