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?