The Wayback Machine - https://web.archive.org/web/20240119133551/https://www.geeksforgeeks.org/html-canvas-rotate-method/
Open In App
Related Articles

HTML canvas rotate() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The HTML canvas rotate() Method is used to rotate the drawing by a given angle. Note that the rotation will only work on the canvas that was made after the rotation was done. 

Syntax:

context.rotate(angle)

Parameter Values:

  • angle: It stores the rotation angle in radians. If the angle is given in the form of a degree then it converts into a radian by using the 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 context = geeks.getContext("2d");
        context.rect(100, 100, 150, 100);//actual rectangle
        context.stroke();
         
        context.rotate((-20) * Math.PI / 180);
        context.rect(100, 100, 150, 100);//rotate rectangle
        context.stroke();
    </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 context = geeks.getContext("2d");
        context.rect(100, 100, 150, 100);//actual rectangle
        context.stroke();
         
        context.rotate((-20) * Math.PI / 180);
        context.rect(100, 100, 150, 100);//rotate rectangle
        context.stroke();
    </script>
</center>
</body>
 
</html>                                     


Output: 

Supported Browsers:

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

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 27 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials