HTML canvas setTransform() method

Definition and Usage

Each object on the canvas has a current transformation matrix.

setTransform() method resets the current transformation matrix to the unit matrix and then runs with the same parameters transform().

In other words, setTransform() allows you to scale, rotate, move, and skew the current environment.

Note:This transformation only affects the drawing after the setTransform() method call.

Example

Draw a rectangle, reset and create a new transformation matrix using setTransform(), draw a rectangle again, reset and create a new transformation matrix, and then draw a rectangle again. Please note that the red rectangle will not be displayed in the example below because it is below the blue rectangle: every time you call setTransform(), it resets the previous transformation matrix and then constructs a new matrix.

Your browser does not support the HTML5 canvas tag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="yellow";
ctx.fillRect(0,0,250,100)
ctx.setTransform(1,0.5,-0.5,1,30,10);
ctx.fillStyle="red";
ctx.fillRect(0,0,250,100);
ctx.setTransform(1,0.5,-0.5,1,30,10);
ctx.fillStyle="blue";
ctx.fillRect(0,0,250,100);

Try It Yourself

Syntax

context.setTransform(a,b,c,d,e,f);

Parameter Value

Parameter Description
a Horizontal rotation drawing.
b Horizontal skewing drawing.
c Vertical skewing drawing.
d Vertical scaling drawing.
e Horizontal movement drawing.
f Vertical movement drawing.

Browser Support

The numbers in the table indicate the first browser version that fully supports this property.

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
4.0 9.0 3.6 4.0 10.1

Note:Internet Explorer 8 and earlier versions do not support the <canvas> element.