Skip to content
Related Articles
Open in App
Not now

Related Articles

HTML canvas rotate() Method

Improve Article
Save Article
  • Last Updated : 05 Jan, 2023
Improve Article
Save Article

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: 

html




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

html




<!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);//actual 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
Related Articles

Start Your Coding Journey Now!