AffineMatrix

A column-major matrix representing a two-dimensional affine transform.

An affine transformation is a linear transformation that preseves parallel lines.

See https://en.wikipedia.org/wiki/Affine_transformation

Constructors

AffineMatrix(a,b,c,d,tx,ty)AffineMatrix

Creates a matrix of the form:

|a  c  tx|
|b  d  ty|
|0  0  1 |
a
number
1

x component of the first basis vector

b
number
0

y component of the first basis vector

c
number
0

x component of the second basis vector

d
number
1

y component of the second basis vector

tx
number
0

translation on the x axis

ty
number
0

translation on the y axis

AffineMatrix.fromTransform(transform)AffineMatrix

Constructs an affine matrix from a transform object.

transform
AffineMatrix.fromTranslation(translation)AffineMatrix

Constructs a translation matrix.

translation
AffineMatrix.fromTranslationPoints(p1,p2)AffineMatrix

Constructs a translation matrix from p1 to p2.

p1

A position to translate from

p2

A position to translate to

AffineMatrix.fromRotation(angle)AffineMatrix

Constructs a rotation matrix.

angle
number

The angle to rotate by (in degrees)

AffineMatrix.fromScale(scale)AffineMatrix

Constructs a scale matrix.

scale
(number | Vec)

The scaling factor. Can be either a 2d vector or a scalar number.

AffineMatrix.fromCenterScale(center,scale)AffineMatrix

Constructs a matrix that scales relative to a center point.

center

The center of the scale transformation. This point will not move

scale
(number | Vec)

The scaling factor. Can be either a 2d vector or a scalar

Methods

.copy(m)AffineMatrixchainable

Copies the value of another matrix m.

.invert()AffineMatrixchainable

Inverts the matrix.

.mul(m)AffineMatrixchainable

Multiplies the matrix by another matrix m.

m1.mul(m2); // m1 = m1 * m2
.mulWithoutTranslation(m)AffineMatrixchainable

Multiplies only the 2x2 portion portion of this matrix by another matrix m. Translation is discarded.

.preMul(m)AffineMatrixchainable

Multiplies another matrix m by this matrix, and then stores the result.

m1.preMul(m2); // m1 = m2 * m1
.preMulWithoutTranslation(m)AffineMatrixchainable

Multiplies only the 2x2 portion portion of another matrix by this matrix, and then stores the result. Translation is discarded.

.translate(v)AffineMatrixchainable

Translates the matrix by a vector v.

.scale(v)AffineMatrixchainable

Scales the matrix by a vector v.

.scaleScalar(s)AffineMatrixchainable

Scales the matrix by a uniformally by s.

s
number
.normalize()AffineMatrixchainable

Scales the basis vectors of the matrix so that they have unit length.

Basis vectors of length 0 will not be changed.

.rotate(angle)AffineMatrixchainable

Rotates the matrix by an angle.

angle
number

An angle in degrees

.skew(angle)AffineMatrixchainable

Skews the "Y" basis vector of the matrix by an angle.

angle
number

An angle in degrees

.origin(origin)AffineMatrixchainable

Translates the matrix such that the center of future scale, rotate and skew transformations will be origin.

origin
.ensureMinimumBasisLength(length)AffineMatrix

Ensure that the basis vectors of this matrix are at least as long as the specified length. If a vector is shorter than the specified length it will be set perpendicular to the opposing vector. If both are shorter they will be set to the identity matrix scaled by length.

length
number

The minimum length the basis vectors will have after this method is called

.determinant()number

Returns The determinant of the matrix.

.equals(m)boolean

Returns true if this matrix exactly equals the matrix m, false otherwise.

.isOrthogonal(tolerance)boolean

Returns true if the basis vectors of the matrix are orthogonal.

tolerance
number
DEFAULT_TOLERANCE
.isInvertible()boolean

Returns true if the matrix can be inverted.

.isUniformScale(tolerance)boolean
tolerance
number
DEFAULT_TOLERANCE

Returns true if the basis vectors of the matrix are the same length.

.isMirror()boolean

Returns true if the matrix mirrors space in either axis.

.isIdentity()boolean

Returns true if the matrix is the identity.

Applying an identity matrix has no effect.

.isNaN()boolean

Returns true if any component of the matrix is NaN.

.isFinite()boolean

Returns true if all components of the matrix are finite numbers (not Infinity or NaN).

.isValid()boolean

Returns true if the matrix is finite, and all of its components are numbers.

.toTransform()Transform

Returns a transform {position, rotation, scale, skew} such that AffineMatrix.fromTransform(transform) will return the original matrix. It guarantees:

  • 0 <= rotation < 360
  • -90 < skew < 90 (assuming the matrix basis vectors are not collinear)

Notes:

  • This will return only one of two possible solutions. You can get the other one by negating scale and rotating by 180 degrees.
  • If either of the basis vectors are degenerate (close to zero length), then this will set skew to 0.
  • If both of the basis vectors are degenerate, this will set rotation to 0.
  • scale will always be returned as a Vec.
.toTransformWithOrigin(origin)TransformWithOrigin

Similar to .toTransform(), except returns a transform with an additional origin property.

origin

The origin of the resulting transform