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

HTML canvas transform() method

HTML canvas transform() method

HTML canvas Reference HTML canvas Reference Manual

Example

Draw a rectangle, add a new transformation matrix via transform(), draw the rectangle again, add a new transformation matrix, then draw the rectangle again. Note that every time you call transform(), it builds on top of the previous transformation matrix:

YourbrowserdoesnotsupporttheHTML5canvastag.

JavaScript:

var c=document.getElementById("myCanvas");

var ctx=c.getContext("2d");

ctx.fillStyle="yellow";

ctx.fillRect(0,0,250,100)

ctx.transform(1,0.5,-0.5,1,30,10);

ctx.fillStyle="red";

ctx.fillRect(0,0,250,100);

ctx.transform(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 transform() 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 transform() method replaces the current transformation matrix. It operates on the current transformation matrix using the matrix described below:

a c e
b d f
0 0 1

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

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

**Note:** The behavior of the transform() method is relative to other transformations performed by rotate(), scale(), translate(), or transform(). For example: if you have already set the drawing to be doubled, the transform() method will double the drawing again, and your drawing will end up being enlarged four times.

**Tip:** See the setTransform() method, which does not behave relative to other transformations.

JavaScript syntax: context.transform(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.

HTML canvas Reference HTML canvas Reference Manual