The Wayback Machine - https://web.archive.org/web/20210916211223/https://www.geeksforgeeks.org/html-canvas-rotate-method/
Skip to content
Related Articles

Related Articles

Improve Article

HTML canvas rotate() Method

  • Last Updated : 28 Jun, 2019

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>                         

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>                                      

Output:

Supported Browsers:

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :