rotate() Method - Javascript Canvas Reference

Javascript examples for Canvas Reference:rotate


The rotate() method rotates the current drawing.

JavaScript syntax


Parameter Values

Parameter Description
angle The rotation angle, in radians.

To calculate from degrees to radians: degrees*Math.PI/180.

To rotate 5 degrees, specify the following: 5*Math.PI/180


Rotate the rectangle 20 degrees:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<canvas id="myCanvas" width="300" height="250" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>


var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.rotate(20 * Math.PI / 180);//from w  w  w  .j  ava  2 s.c  o m
ctx.fillRect(50, 20, 100, 100);



Related Tutorials