Matrix
An affine transformation matrix performs a linear mapping from 2D coordinates to other 2D coordinates that preserves the “straightness” and “parallelness” of lines.
Such a coordinate transformation can be represented by a 3 row by 3 column matrix with an implied last row of [ 0 0 1 ]. This matrix transforms source coordinates (x, y) into destination coordinates (x',y') by considering them to be a column vector and multiplying the coordinate vector by the matrix according to the following process:
[ x ] [ a c tx ] [ x ] [ a * x + c * y + tx ]
[ y ] = [ b d ty ] [ y ] = [ b * x + d * y + ty ]
[ 1 ] [ 0 0 1 ] [ 1 ] [ 1 ]Note the locations of b and c.
This class is optimized for speed and minimizes calculations based on its knowledge of the underlying matrix (as opposed to say simply performing matrix multiplication).
Constructors
Matrix()Creates a 2D affine transformation matrix that describes the identity transformation.
Returns:
Matrix
Matrix(a, b, c, d, tx, ty)Creates a 2D affine transformation matrix.
Parameters:
a:Number— the a property of the transformb:Number— the b property of the transformc:Number— the c property of the transformd:Number— the d property of the transformtx:Number— the tx property of the transformty:Number— the ty property of the transformReturns:
Matrix
Matrix(values)Creates a 2D affine transformation matrix.
Parameters:
values:Array ofNumbers— the matrix values to initialize this matrix withReturns:
Matrix
Matrix(matrix)Creates a 2D affine transformation matrix.
Parameters:
matrix:Matrix— the matrix to copy the values fromReturns:
Matrix
Properties
aThe value that affects the transformation along the x axis when scaling or rotating, positioned at (0, 0) in the transformation matrix.
Type:
Number
bThe value that affects the transformation along the y axis when rotating or skewing, positioned at (1, 0) in the transformation matrix.
Type:
Number
cThe value that affects the transformation along the x axis when rotating or skewing, positioned at (0, 1) in the transformation matrix.
Type:
Number
dThe value that affects the transformation along the y axis when scaling or rotating, positioned at (1, 1) in the transformation matrix.
Type:
Number
txThe distance by which to translate along the x axis, positioned at (2, 0) in the transformation matrix.
Type:
Number
tyThe distance by which to translate along the y axis, positioned at (2, 1) in the transformation matrix.
Type:
Number
valuesThe matrix values as an array, in the same sequence as they are passed to
initialize(a, b, c, d, tx, ty).Read only.
Type:
Array of
Numbers
translationThe translation of the matrix as a vector.
Read only.
Type:
Point
scalingThe scaling values of the matrix, if it can be decomposed.
Read only.
Type:
PointSee also:
decompose()
rotationThe rotation angle of the matrix, if it can be decomposed.
Read only.
Type:
NumberSee also:
decompose()
Methods
set(...values)Sets the matrix to the passed values. Note that any sequence of parameters that is supported by the various
Matrix() constructors also work for calls ofset().Parameters:
values:any valueReturns:
Point
clone()Returns:
Matrix— a copy of this transform
equals(matrix)Checks whether the two matrices describe the same transformation.
Parameters:
matrix:Matrix— the matrix to compare this matrix toReturns:
Boolean—trueif the matrices are equal,falseotherwise
toString()Returns:
String— a string representation of this transform
reset()Resets the matrix by setting its values to the ones of the identity matrix that results in no transformation.
apply([recursively])Attempts to apply the matrix to the content of item that it belongs to, meaning its transformation is baked into the item’s content or children.
Parameters:
recursively:Boolean— controls whether to apply transformations recursively on children — optional, default:trueReturns:
Boolean—trueif the matrix was applied,falseotherwise
translate(point)Concatenates this matrix with a translate transformation.
Parameters:
point:Point— the vector to translate byReturns:
Matrix— this affine transform
translate(dx, dy)Concatenates this matrix with a translate transformation.
Parameters:
dx:Number— the distance to translate in the x directiondy:Number— the distance to translate in the y directionReturns:
Matrix— this affine transform
scale(scale[, center])Concatenates this matrix with a scaling transformation.
Parameters:
scale:Number— the scaling factorcenter:Point— the center for the scaling transformation — optionalReturns:
Matrix— this affine transform
scale(hor, ver[, center])Concatenates this matrix with a scaling transformation.
Parameters:
hor:Number— the horizontal scaling factorver:Number— the vertical scaling factorcenter:Point— the center for the scaling transformation — optionalReturns:
Matrix— this affine transform
rotate(angle, center)Concatenates this matrix with a rotation transformation around an anchor point.
Parameters:
angle:Number— the angle of rotation measured in degreescenter:Point— the anchor point to rotate aroundReturns:
Matrix— this affine transform
rotate(angle, x, y)Concatenates this matrix with a rotation transformation around an anchor point.
Parameters:
angle:Number— the angle of rotation measured in degreesx:Number— the x coordinate of the anchor pointy:Number— the y coordinate of the anchor pointReturns:
Matrix— this affine transform
shear(shear[, center])Concatenates this matrix with a shear transformation.
Parameters:
shear:Point— the shear factor in x and y directioncenter:Point— the center for the shear transformation — optionalReturns:
Matrix— this affine transform
shear(hor, ver[, center])Concatenates this matrix with a shear transformation.
Parameters:
hor:Number— the horizontal shear factorver:Number— the vertical shear factorcenter:Point— the center for the shear transformation — optionalReturns:
Matrix— this affine transform
skew(skew[, center])Concatenates this matrix with a skew transformation.
Parameters:
skew:Point— the skew angles in x and y direction in degreescenter:Point— the center for the skew transformation — optionalReturns:
Matrix— this affine transform
skew(hor, ver[, center])Concatenates this matrix with a skew transformation.
Parameters:
hor:Number— the horizontal skew angle in degreesver:Number— the vertical skew angle in degreescenter:Point— the center for the skew transformation — optionalReturns:
Matrix— this affine transform
append(matrix)Appends the specified matrix to this matrix. This is the equivalent of multiplying
(this matrix) * (specified matrix).Parameters:
matrix:Matrix— the matrix to appendReturns:
Matrix— this matrix, modified
prepend(matrix)Prepends the specified matrix to this matrix. This is the equivalent of multiplying
(specified matrix) * (this matrix).Parameters:
matrix:Matrix— the matrix to prependReturns:
Matrix— this matrix, modified
appended(matrix)Returns a new matrix as the result of appending the specified matrix to this matrix. This is the equivalent of multiplying
(this matrix) * (specified matrix).Parameters:
matrix:Matrix— the matrix to appendReturns:
Matrix— the newly created matrix
prepended(matrix)Returns a new matrix as the result of prepending the specified matrix to this matrix. This is the equivalent of multiplying
(specified matrix) * (this matrix).Parameters:
matrix:Matrix— the matrix to prependReturns:
Matrix— the newly created matrix
invert()Inverts the matrix, causing it to perform the opposite transformation. If the matrix is not invertible (in which case
isSingular() returns true),nullis returned.Returns:
Matrix— this matrix, ornull, if the matrix is singular.
inverted()Creates a new matrix that is the inversion of this matrix, causing it to perform the opposite transformation. If the matrix is not invertible (in which case
isSingular() returns true),nullis returned.Returns:
Matrix— this matrix, ornull, if the matrix is singular.
isIdentity()Returns:
Boolean— whether this matrix is the identity matrix
isInvertible()Checks whether the matrix is invertible. A matrix is not invertible if the determinant is 0 or any value is infinite or NaN.
Returns:
Boolean— whether the matrix is invertible
isSingular()Checks whether the matrix is singular or not. Singular matrices cannot be inverted.
Returns:
Boolean— whether the matrix is singular
transform(point)Transforms a point and returns the result.
Parameters:
point:Point— the point to be transformedReturns:
Point— the transformed point
transform(src, dst, count)Transforms an array of coordinates by this matrix and stores the results into the destination array, which is also returned.
Parameters:
src:Array ofNumbers— the array containing the source points as x, y value pairsdst:Array ofNumbers— the array into which to store the transformed point pairscount:Number— the number of points to transformReturns:
Array of Numbers— the dst array, containing the transformed coordinates
inverseTransform(point)Inverse transforms a point and returns the result.
Parameters:
point:Point— the point to be transformedReturns:
Point
decompose()Decomposes the affine transformation described by this matrix into
scaling,rotationandskewing, and returns an object with these properties.Returns:
Object— the decomposed matrix
applyToContext(ctx)Applies this matrix to the specified Canvas Context.
Parameters:
ctx:CanvasRenderingContext2D
Was this helpful?