Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML canvas setTransform() method

HTML canvas setTransform() method

Canvas Object Reference Canvas object

Example

Draw a rectangle, reset and create a new transformation matrix via setTransform(), draw the rectangle again, reset and create a new transformation matrix, then draw the rectangle again. Note that every time you call setTransform(), it resets the previous transformation matrix and then builds a new one, so in the example below the red rectangle is not shown because it is beneath the blue rectangle:

YourbrowserdoesnotsupporttheHTML5canvastag.

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 »


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 9, Firefox, Opera, Chrome and Safari support the setTransform() method.

**Note:** Internet Explorer 8 and earlier versions do not support .


Definition and Usage

Every object on the canvas has a current transformation matrix.

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

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

**Note:** This transformation only affects the drawing after the setTransform() method is called.

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

Parameter value

Parameters Description
a Scales the drawing horizontally.
b Skews the drawing horizontally.
c Skews the drawing vertically.
d Scales the drawing vertically.
e Moves the drawing horizontally.
f Moves the drawing vertically.

Canvas Object Reference Canvas object