HTML Canvas rotate() Method

Introduction

Rotate the rectangle 20 degrees:

View in separate window

<!DOCTYPE html>
<html>
<body>

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

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

</body>
</html>

The rotate() method rotates the current drawing.

The rotation will only affect drawings made after the rotation is done.

context.rotate(angle);

Parameter Values

Parameter Description
angle The rotation angle, in radians.

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

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




PreviousNext

Related