Canvas transform() Method

Definition and Usage

Each object on the canvas has a current transformation matrix.

transform() Method replaces the current transformation matrix. It operates the current transformation matrix with the following matrix described:

a  c  e
b  d  f
0  0  1

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

Σημείωση:This transformation only affects the drawing after the transform() method call.

Σημείωση:The behavior of the transform() method is relative to rotate(),scale(),translate() or other transformations completed by transform(). For example: if you have already set the drawing to double size, the transform() method will double the drawing, and your drawing will end up being quadrupled.

Tips:Please see setTransform() Method, which does not behave relative to other transformations.

Example

Draw a rectangle; add a new transformation matrix using transform() and draw the rectangle again; add another transformation matrix and draw the rectangle again. Note that each time you call transform(), it builds on the previous transformation 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.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 yourself

Syntax

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

Parameter value

Parameter Description
a Horizontal scaling drawing.
b Horizontal skew drawing.
c Vertical skew 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 attribute.

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

Σημείωση:Οι εκδόσεις Internet Explorer 8 και παλιότερες δεν υποστηρίζουν το στοιχείο <canvas>.