# Crafty.math.Matrix2D Class

Matrix2D uses the following form, as per the whatwg.org specifications for canvas.transform(): [a, c, e] [b, d, f] [0, 0, 1]

`public {Matrix2D} new Matrix2D();`
[Returns]

A new identity matrix

`public {Matrix2D} new Matrix2D(Matrix2D matrix);`
matrix

a matrix to copy

[Returns]

A new instance whose entries are copied from the passed matrix

`public {Matrix2D} new Matrix2D(Number a, Number b, Number c, Number d, Number e, Number f);`
a

(m11) Horizontal scale

b

(m12) Horizontal skew

c

(m21) Vertical skew

d

(m22) Vertical scale

e

(dx) Horizontal translation

f

(dy) Vertical translation

[Returns]

A new instance whose entries are set from the passed arguments

#### Example

``````// Create the following translation matrix:
// [1, 0, 5]
// [0, 1, 7]
// [0, 0, 1]
var m = new Crafty.math.Matrix2D(1, 0, 0, 1, 5, 7);``````

## .apply()

Applies the matrix transformations to the passed object

`public {Vector2D} apply(Vector2D vecRH);`
vecRH

vector to be transformed

[Returns]

the passed vector object after transforming

## .clone()

Creates an exact, numeric copy of the current matrix

`public {Matrix2D} clone();`
[Returns]

The cloned matrix

## .combine()

Multiplies this matrix with another, overriding the values of this matrix. The passed matrix is assumed to be on the right-hand side.

`public {Matrix2D} combine(Matrix2D mtrxRH);`
mtrxRH

The passed matrix

[Returns]

this matrix after combination

## .determinant()

Calculates the determinant of this matrix

`public {Number} determinant();`
[Returns]

det(this matrix)

## .equals()

Checks for the numeric element-wise equality of this matrix versus another.

`public {Boolean} equals(Matrix2D mtrxRH);`
mtrxRH

The matrix to check equality with

[Returns]

true if the two matrices are numerically equal

## .invert()

Inverts this matrix if possible

`public {Matrix2D} invert();`
[Returns]

this inverted matrix or the original matrix on failure

## .isIdentity()

Returns true if this matrix is the identity matrix

`public {Boolean} isIdentity();`
[Returns]

true if this matrix is an identity matrix

## .isInvertible()

Determines is this matrix is invertible.

`public {Boolean} isInvertible();`
[Returns]

true if this matrix is invertible

## .preRotate()

Applies a counter-clockwise pre-rotation to this matrix

`public {Matrix2D} preRotate(Number rads);`

[Returns]

this matrix after pre-rotation

## .preScale()

Applies a pre-scaling to this matrix, applied to the a, b, c, and d elements.

If two arguments are supplied, a and c are multiplied by scalarX, b, and d by scalarY.

`public {Matrix2D} preScale(Number scalarX[, Number scalarY]);`
scalarX

The amount to scale

[scalarY]

scalarX is used if scalarY is undefined

[Returns]

this after pre-scaling

## .preTranslate()

Applies a pre-translation to this matrix

`public {Matrix2D} preTranslate(Number dx, Number dy);`
dx

The amount to shift the e component

dy

The amount to shift the f component

[Returns]

this matrix after pre-translation

`public {Matrix2D} preTranslate(Vector2D vector);`
vector

The vector to shift (e, f) by.

[Returns]

this matrix after pre-translation

## .rotate()

Applies a counter-clockwise post-rotation to this matrix

`public {Matrix2D} rotate(Number rads);`

[Returns]

this matrix after rotation

## .scale()

Applies a post-scaling to this matrix, modifying components a-f.

If two arguments are passed, scalarX is used for components a, c, and e; scalarY for b, d, and f.

`public {Matrix2D} scale(Number scalarX[, Number scalarY]);`
scalarX The amount to scale by along the x axis
[scalarY] scalarX is used if scalarY is undefined
[Returns]

this after post-scaling

## .setValues()

Sets the values of this matrix.

`public {Matrix2D} setValues(Matrix2D matrix);`
matrix

A matrix to copy the values from

[Returns]

This matrix after copying the values

`public {Matrix2D} setValues(Number a, Number b, Number c, Number d, Number e, Number f);`

When used as a translation matrix, the 6 elements have particular meanings.

a

(m11) Horizontal scale

b

(m12) Horizontal skew

c

(m21) Vertical skew

d

(m22) Vertical scale

e

(dx) Horizontal translation

f

(dy) Vertical translation

[Returns]

this matrix containing the new values

## .toString()

Returns the string representation of this matrix.

`public {String} toString();`
[Returns]

A string representation like "Matrix2D([a, c, e], [b, d, f], [0, 0, 1])"

## .translate()

Applies a post-translation to this matrix

`public {Matrix2D} translate(Vector2D vector);`
vector

the vector to translate by

[Returns]

this matrix after post-translation

`public {Matrix2D} translate(Number dx, Number dy);`
dx

The shift along the x-axis

dy

The shift along the y-axis

[Returns]

this matrix after post-translation