Reference applyMatrix()

applyMatrix()

Applies a transformation matrix to the coordinate system.

Transformations such as translate(), rotate(), and scale() use matrix-vector multiplication behind the scenes. A table of numbers, called a matrix, encodes each transformation. The values in the matrix then multiply each point on the canvas, which is represented by a vector.

applyMatrix() allows for many transformations to be applied at once. See Wikipedia and MDN for more details about transformations.

There are two ways to call applyMatrix() in two and three dimensions.

In 2D mode, the parameters a, b, c, d, e, and f, correspond to elements in the following transformation matrix:

The transformation matrix used when applyMatrix is called in 2D mode.

The numbers can be passed individually, as in applyMatrix(2, 0, 0, 0, 2, 0). They can also be passed in an array, as in applyMatrix([2, 0, 0, 0, 2, 0]).

In 3D mode, the parameters a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, and p correspond to elements in the following transformation matrix:

The transformation matrix used when applyMatrix is called in 3D mode.

The numbers can be passed individually, as in applyMatrix(2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1). They can also be passed in an array, as in applyMatrix([2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1]).

By default, transformations accumulate. The push() and pop() functions can be used to isolate transformations within distinct drawing groups.

Note: Transformations are reset at the beginning of the draw loop. Calling applyMatrix() inside the draw() function won't cause shapes to transform continuously.

Examples

Syntax

applyMatrix(arr)
applyMatrix(a, b, c, d, e, f)
applyMatrix(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)

Parameters

arr
Array:

an array containing the elements of the transformation matrix. Its length should be either 6 (2D) or 16 (3D).

a
Number:

an element of the transformation matrix.

b
Number:

an element of the transformation matrix.

c
Number:

an element of the transformation matrix.

d
Number:

an element of the transformation matrix.

e
Number:

an element of the transformation matrix.

f
Number:

an element of the transformation matrix.

g
Number:

an element of the transformation matrix.

h
Number:

an element of the transformation matrix.

i
Number:

an element of the transformation matrix.

j
Number:

an element of the transformation matrix.

k
Number:

an element of the transformation matrix.

l
Number:

an element of the transformation matrix.

m
Number:

an element of the transformation matrix.

n
Number:

an element of the transformation matrix.

o
Number:

an element of the transformation matrix.

p
Number:

an element of the transformation matrix.

Notice any errors or typos? Please let us know. Please feel free to edit src/core/transform.js and open a pull request!

Related References