1. Introduction
This specification describes several geometry interfaces [WEBIDL] for the representation of points, rectangles, quadrilaterals and transformation matrices with the dimension of 3x2 and 4x4.
The SVG interfaces SVGPoint, SVGRect and SVGMatrix [SVG11] are aliasing the here defined interfaces in favor for common interfaces used by SVG, Canvas 2D Context [2DCONTEXT] and CSS Transforms [CSS3-TRANSFORMS].
The IDL in this specification is currently in an experimental state. An approach of avoiding overloading in constructors and instead using factory static methods, and supporting dictionary types everywhere, is explored. Readers interested in a specification that more closely matches implementations as of April 2015 are referred to an earlier revision of this specification.
2. The DOMPoint interfaces
A 2D or a 3D point can be represented by the following WebIDL interfaces:
[Constructor(optional unrestricted double x = 0, optional unrestricted double y = 0, optional unrestricted double z = 0, optional unrestricted double w = 1), Exposed=(Window,Worker)] interface DOMPointReadOnly { [NewObject] static DOMPointReadOnly fromPoint(optional DOMPointInit other); readonly attribute unrestricted double x; readonly attribute unrestricted double y; readonly attribute unrestricted double z; readonly attribute unrestricted double w; DOMPoint matrixTransform(optional DOMMatrixInit matrix); serializer = { attribute }; }; [Constructor(optional unrestricted double x = 0, optional unrestricted double y = 0, optional unrestricted double z = 0, optional unrestricted double w = 1), Exposed=(Window,Worker)] interface DOMPoint : DOMPointReadOnly { [NewObject] static DOMPoint fromPoint(optional DOMPointInit other); inherit attribute unrestricted double x; inherit attribute unrestricted double y; inherit attribute unrestricted double z; inherit attribute unrestricted double w; }; dictionary DOMPointInit { unrestricted double x = 0; unrestricted double y = 0; unrestricted double z = 0; unrestricted double w = 1; };
The following algorithms assume that DOMPointReadOnly objects have the internal member variables x coordinate, y coordinate, z coordinate and w perspective. DOMPointReadOnly as well as the inheriting interface DOMPoint must be able to access and set the value of these variables.
An interface returning an DOMPointReadOnly object by an attribute or function may be able to modify internal member variable values. Such an interface must specify this ability explicitly in prose.
Internal member variables must not be exposed in any way.
The DOMPointReadOnly(x, y, z, w)
and DOMPoint(x, y, z, w)
constructors, when invoked, must run the following steps:
- Let point be a new
DOMPointReadOnly
orDOMPoint
object as appropriate. - Set point’s variables x coordinate to x, y coordinate to y, z coordinate to z and w perspective to w.
- Return point.
The fromPoint(other)
static method on DOMPointReadOnly
must create a DOMPointReadOnly
from the dictionary other.
The fromPoint(other)
static method on DOMPoint
create a DOMPoint
from the dictionary other.
To create a DOMPointReadOnly
from a dictionary other, or to create a DOMPoint
from a dictionary other, follow these
steps:
- Let point be a new
DOMPointReadOnly
orDOMPoint
as appropriate. - Set point’s variables x coordinate to other’s
x
dictionary member, y coordinate to other’sy
dictionary member, z coordinate to other’sz
dictionary member and w perspective to other’sw
dictionary member. - Return point.
The y attribute, on getting, must return the y coordinate value of the object it was invoked on. For the DOMPoint interface, setting the y attribute must set the y coordinate value of the object it was invoked on to the new value.
The z attribute, on getting, must return the z coordinate value of the object it was invoked on. For the DOMPoint interface, setting the z attribute must set the z coordinate value of the object it was invoked on to the new value.
The w attribute, on getting, must return the w perspective value of the object it was invoked on. For the DOMPoint interface, setting the w attribute must set the w perspective value of the object it was invoked on to the new value.
matrixTransform(matrix), when invoked, must run the following steps
- Create a new DOMPoint point initialized to x, y, z and w of the current point.
- Let matrix object be the result of invoking create a
DOMMatrix
from the dictionary matrix. - Post-multiply point with matrix object.
- Return point.
matrixTransform() does not modify the current DOMPointReadOnly object and returns a new DOMPoint object.
In this example the method matrixTransform() on point is called with a DOMMatrix argument matrix.
var point = new DOMPoint(5, 4);var matrix = new DOMMatrix([2, 0, 0, 2, 10, 10]);
var transformedPoint = point.matrixTransform(matrix);
point creates a new DOMPoint object initialized to the same attribute values as point. This new DOMPoint is now scaled and the translated by matrix. This resulting transformPoint has the attribute values x: 20
and y: 18
.
For historical reasons, Window
objects must also have a writable, configurable, non-enumerable
property named SVGPoint
whose value is the DOMPoint interface object.
3. The DOMRect interfaces
Objects implementing the DOMRectReadOnly interface represent a rectangle. The type of box is specified by the method or attribute that returns a DOMRect or DOMRectReadOnly object.
Rectangles have the following properties:
- origin
- When the rectangle has a non-negative width dimension, the rectangle’s horizontal origin is the left edge; otherwise, it is the right edge. Similarly, when the rectangle has a non-negative height dimension, the rectangle’s vertical origin is the top edge; otherwise, it is the bottom edge.
- x coordinate
- The horizontal distance between the viewport’s left edge and the rectangle’s origin.
- y coordinate
- The vertical distance between the viewport’s top edge and the rectangle’s origin.
- width dimension
- The width of the rectangle. Can be negative.
- height dimension
- The height of the rectangle. Can be negative.
[Constructor(optional unrestricted double x = 0, optional unrestricted double y = 0, optional unrestricted double width = 0, optional unrestricted double height = 0), Exposed=(Window,Worker)] interface DOMRectReadOnly { [NewObject] static DOMRectReadOnly fromRect(optional DOMRectInit other); readonly attribute unrestricted double x; readonly attribute unrestricted double y; readonly attribute unrestricted double width; readonly attribute unrestricted double height; readonly attribute unrestricted double top; readonly attribute unrestricted double right; readonly attribute unrestricted double bottom; readonly attribute unrestricted double left; serializer = { attribute }; }; [Constructor(optional unrestricted double x = 0, optional unrestricted double y = 0, optional unrestricted double width = 0, optional unrestricted double height = 0), Exposed=(Window,Worker)] interface DOMRect : DOMRectReadOnly { [NewObject] static DOMRect fromRect(optional DOMRectInit other); inherit attribute unrestricted double x; inherit attribute unrestricted double y; inherit attribute unrestricted double width; inherit attribute unrestricted double height; }; dictionary DOMRectInit { unrestricted double x = 0; unrestricted double y = 0; unrestricted double width = 0; unrestricted double height = 0; };
The following algorithms assume that DOMRectReadOnly objects have the internal member variables x coordinate, y coordinate, width dimension and height dimension. DOMRectReadOnly as well as the inheriting interface DOMRect must be able to access and set the value of these variables.
An interface returning an DOMRectReadOnly object by an attribute or function may be able to modify internal member variable values. Such an interface must specify this ability explicitly in prose.
Internal member variables must not be exposed in any way.
The DOMRectReadOnly(x, y, width, height)
and DOMRect(x, y, width, height)
constructors, when invoked, must run the following steps:
- Let rect be a new
DOMRectReadOnly
orDOMRect
object as appropriate. - Set rect’s variables x coordinate to x, y coordinate to y, width dimension to width and height dimension to height.
- Return rect.
The fromRect(other)
static method on DOMRectReadOnly
must create a DOMRectReadOnly
from the dictionary other.
The fromRect(other)
static method on DOMRect
must create a DOMRect
from the dictionary other.
To create a DOMRectReadOnly
from a dictionary other, or to create a DOMRect
from a dictionary other, follow these
steps:
- Let rect be a new
DOMRectReadOnly
orDOMRect
as appropriate. - Set rect’s variables x coordinate to other’s
x
dictionary member, y coordinate to other’sy
dictionary member, width dimension to other’swidth
dictionary member and height dimension to other’sheight
dictionary member. - Return rect.
The y attribute, on getting, it must return the y coordinate value of the object it was invoked on. For the DOMRect interface, setting the y attribute must set the y coordinate value of the object it was invoked on to the new value.
The width attribute, on getting, must return the width dimension value of the object it was invoked on. For the DOMRect interface, setting the width attribute must set the width dimension value of the object it was invoked on to the new value.
The height attribute, on getting, must return the height dimension value. For the DOMRect interface, setting the height attribute must set the height dimension value to the new value.
The top attribute, on getting, must return min(y coordinate, y coordinate + height dimension) of the object it was invoked on.
The right attribute, on getting, must return max(x coordinate, x coordinate + width dimension) of the object it was invoked on.
The bottom attribute, on getting, must return max(y coordinate, y coordinate + height dimension) of the object it was invoked on.
The left attribute, on getting, must return min(x coordinate, x coordinate + width dimension) of the object it was invoked on.
For historical reasons, Window
objects must also have a writable, configurable, non-enumerable
property named SVGRect whose value is the DOMRect interface object.
4. The DOMQuad interface
Objects implementing the DOMQuad interface represents a quadrilateral.
[Constructor(optional DOMPointInit p1, optional DOMPointInit p2, optional DOMPointInit p3, optional DOMPointInit p4), Exposed=(Window,Worker)] interface DOMQuad { [NewObject] static DOMQuad fromRect(optional DOMRectInit other); [NewObject] static DOMQuad fromQuad(optional DOMQuadInit other); [SameObject] readonly attribute DOMPoint p1; [SameObject] readonly attribute DOMPoint p2; [SameObject] readonly attribute DOMPoint p3; [SameObject] readonly attribute DOMPoint p4; [NewObject] DOMRect getBounds(); serializer = { attribute }; }; dictionary DOMQuadInit { DOMPointInit p1; DOMPointInit p2; DOMPointInit p3; DOMPointInit p4; };
The DOMQuad(p1, p2, p3, p4)
constructor, when invoked, must run the following steps:
- Let point 1 be a new DOMPoint object with its attributes set to the values of the namesake dictionary members in p1.
- Let point 2 be a new DOMPoint object with its attributes set to the values of the namesake dictionary members in p2.
- Let point 3 be a new DOMPoint object with its attributes set to the values of the namesake dictionary members in p3.
- Let point 4 be a new DOMPoint object with its attributes set to the values of the namesake dictionary members in p4.
-
Return a new DOMQuad with p1 set to point 1, p2 set to point 2, p3 set to point 3 and p4 set to point 4.
Note: It is possible to pass DOMPoint/DOMPointReadOnly arguments as well. The passed arguments will be transformed to the correct object type internally following the WebIDL rules [WEBIDL].
The fromRect(other)
static
method on DOMQuad
must create a DOMQuad
from the DOMRectInit
dictionary other.
To create a DOMQuad
from a DOMRectInit
dictionary other, follow these steps:
- Let x, y, width and height be the value of other’s x, y, width and height attributes, respectively.
- Let point 1 be a new DOMPoint object with x set to x, y set to y, z set to zero and w set to one.
- Let point 2 be a new DOMPoint object with x set to x + width, y set to y, z set to zero and w set to one.
- Let point 3 be a new DOMPoint object with x set to x + width, y set to y + height, z set to zero and w set to one.
- Let point 4 be a new DOMPoint object with x set to x, y set to y + height, z set to zero and w set to one.
-
Return a new DOMQuad with p1 set to point 1, p2 set to point 2, p3 set to point 3 and p4 set to point 4.
The fromQuad(other)
static
method on DOMQuad
must create a DOMQuad
from the DOMQuadInit
dictionary other.
To create a DOMQuad
from a DOMQuadInit
dictionary other, follow these steps:
-
Let point 1 be the result of invoking create a
DOMPoint
from the dictionaryp1
dictionary member of other, if it exists. -
Let point 2 be the result of invoking create a
DOMPoint
from the dictionaryp2
dictionary member of other, if it exists. -
Let point 3 be the result of invoking create a
DOMPoint
from the dictionaryp3
dictionary member of other, if it exists. -
Let point 4 be the result of invoking create a
DOMPoint
from the dictionaryp4
dictionary member of other, if it exists. -
Return a new
DOMQuad
withp1
set to point 1,p2
set to point 2,p3
set to point 3 andp4
set to point 4.
The p2 attribute must return a DOMPoint that represents p2 of the quadrilateral it was invoked on. The author can modify the returned DOMPoint object, which directly affects the quadrilateral.
The p3 attribute must return a DOMPoint that represents p3 of the quadrilateral it was invoked on. The author can modify the returned DOMPoint object, which directly affects the quadrilateral.
The p4 attribute must return a DOMPoint that represents p4 of the quadrilateral it was invoked on. The author can modify the returned DOMPoint object, which directly affects the quadrilateral.
The getBounds() method, when invoked, must run the following algorithm:
- Let bounds be a DOMRect object.
- Let left be the minimum of p1.x, p2.x, p3.x and p4.x.
- Let top be the minimum of p1.y, p2.y, p3.y and p4.y.
- Let right be the maximum of p1.x, p2.x, p3.x and p4.x.
- Let bottom be the maximum of p1.y, p2.y, p3.y and p4.y.
- Let x coordinate of bounds be left, y coordinate of bounds be top, width dimension of bounds be right - left and height dimension of bounds be bottom - top.
- Return bounds.
In this example the DOMQuad constructor is called with arguments of type DOMPoint and DOMPointInit. Both arguments are accepted and can be used.
var point = new DOMPoint(2, 0);var quad1 = new DOMQuad(point, {x: 12, y: 0}, {x: 2, y: 10}, {x: 12, y: 10});
The attribute values of the resulting DOMQuad quad1 above are also equal to the attribute values of the following DOMQuad quad2:
var rect = new DOMRect(2, 0, 10, 10);var quad2 = DOMQuad.fromRect(rect);
This is an example of an irregular quadrilateral:
new DOMQuad({x: 40, y: 25}, {x: 180, y: 8}, {x: 210, y: 150}, {x: 10, y: 180});
5. The DOMMatrix interfaces
The DOMMatrix and DOMMatrixReadOnly interfaces each represent a mathematical matrix with the purpose of describing transformations in a graphical context. The following sections describe the details of the interface.
A 4x4 matrix representing a DOMMatrix with items m11 to m44.
In the following sections, terms have the following meaning:
- post-multiply
- Term A post-multiplied by term B is equal to A · B.
- pre-multiply
- Term A pre-multiplied by term B is equal to B · A.
- multiply
- Multiply term A by term B is equal to A · B.
[Constructor, Constructor(DOMString transformList), Constructor(sequence<unrestricted double> numberSequence), Exposed=(Window,Worker)] interface DOMMatrixReadOnly { [NewObject] static DOMMatrixReadOnly fromMatrix(optional DOMMatrixInit other); [NewObject] static DOMMatrixReadOnly fromFloat32Array(Float32Array array32); [NewObject] static DOMMatrixReadOnly fromFloat64Array(Float64Array array64); // These attributes are simple aliases for certain elements of the 4x4 matrix readonly attribute unrestricted double a; readonly attribute unrestricted double b; readonly attribute unrestricted double c; readonly attribute unrestricted double d; readonly attribute unrestricted double e; readonly attribute unrestricted double f; readonly attribute unrestricted double m11; readonly attribute unrestricted double m12; readonly attribute unrestricted double m13; readonly attribute unrestricted double m14; readonly attribute unrestricted double m21; readonly attribute unrestricted double m22; readonly attribute unrestricted double m23; readonly attribute unrestricted double m24; readonly attribute unrestricted double m31; readonly attribute unrestricted double m32; readonly attribute unrestricted double m33; readonly attribute unrestricted double m34; readonly attribute unrestricted double m41; readonly attribute unrestricted double m42; readonly attribute unrestricted double m43; readonly attribute unrestricted double m44; readonly attribute boolean is2D; readonly attribute boolean isIdentity; // Immutable transform methods DOMMatrix translate(optional unrestricted double tx = 0, optional unrestricted double ty = 0, optional unrestricted double tz = 0); DOMMatrix scale(optional unrestricted double scaleX = 1, optional unrestricted double scaleY, optional unrestricted double scaleZ = 1, optional unrestricted double originX = 0, optional unrestricted double originY = 0, optional unrestricted double originZ = 0); DOMMatrix scale3d(optional unrestricted double scale = 1, optional unrestricted double originX = 0, optional unrestricted double originY = 0, optional unrestricted double originZ = 0); DOMMatrix rotate(optional unrestricted double rotX = 0, optional unrestricted double rotY, optional unrestricted double rotZ); DOMMatrix rotateFromVector(optional unrestricted double x = 0, optional unrestricted double y = 0); DOMMatrix rotateAxisAngle(optional unrestricted double x = 0, optional unrestricted double y = 0, optional unrestricted double z = 0, optional unrestricted double angle = 0); DOMMatrix skewX(optional unrestricted double sx = 0); DOMMatrix skewY(optional unrestricted double sy = 0); DOMMatrix multiply(optional DOMMatrixInit other); DOMMatrix flipX(); DOMMatrix flipY(); DOMMatrix inverse(); DOMPoint transformPoint(optional DOMPointInit point); Float32Array toFloat32Array(); Float64Array toFloat64Array(); stringifier; serializer = { attribute }; }; [Constructor, Constructor(DOMString transformList), Constructor(sequence<unrestricted double> numberSequence), Exposed=(Window,Worker)] interface DOMMatrix : DOMMatrixReadOnly { [NewObject] static DOMMatrix fromMatrix(optional DOMMatrixInit other); [NewObject] static DOMMatrix fromFloat32Array(Float32Array array32); [NewObject] static DOMMatrix fromFloat64Array(Float64Array array64); // These attributes are simple aliases for certain elements of the 4x4 matrix inherit attribute unrestricted double a; inherit attribute unrestricted double b; inherit attribute unrestricted double c; inherit attribute unrestricted double d; inherit attribute unrestricted double e; inherit attribute unrestricted double f; inherit attribute unrestricted double m11; inherit attribute unrestricted double m12; inherit attribute unrestricted double m13; inherit attribute unrestricted double m14; inherit attribute unrestricted double m21; inherit attribute unrestricted double m22; inherit attribute unrestricted double m23; inherit attribute unrestricted double m24; inherit attribute unrestricted double m31; inherit attribute unrestricted double m32; inherit attribute unrestricted double m33; inherit attribute unrestricted double m34; inherit attribute unrestricted double m41; inherit attribute unrestricted double m42; inherit attribute unrestricted double m43; inherit attribute unrestricted double m44; // Mutable transform methods DOMMatrix multiplySelf(optional DOMMatrixInit other); DOMMatrix preMultiplySelf(optional DOMMatrixInit other); DOMMatrix translateSelf(optional unrestricted double tx = 0, optional unrestricted double ty = 0, optional unrestricted double tz = 0); DOMMatrix scaleSelf(optional unrestricted double scaleX = 1, optional unrestricted double scaleY, optional unrestricted double scaleZ = 1, optional unrestricted double originX = 0, optional unrestricted double originY = 0, optional unrestricted double originZ = 0); DOMMatrix scale3dSelf(optional unrestricted double scale = 1, optional unrestricted double originX = 0, optional unrestricted double originY = 0, optional unrestricted double originZ = 0); DOMMatrix rotateSelf(optional unrestricted double rotX = 0, optional unrestricted double rotY, optional unrestricted double rotZ); DOMMatrix rotateFromVectorSelf(optional unrestricted double x = 0, optional unrestricted double y = 0); DOMMatrix rotateAxisAngleSelf(optional unrestricted double x = 0, optional unrestricted double y = 0, optional unrestricted double z = 0, optional unrestricted double angle = 0); DOMMatrix skewXSelf(optional unrestricted double sx = 0); DOMMatrix skewYSelf(optional unrestricted double sy = 0); DOMMatrix invertSelf(); DOMMatrix setMatrixValue(DOMString transformList); }; dictionary DOMMatrixInit { unrestricted double a; unrestricted double b; unrestricted double c; unrestricted double d; unrestricted double e; unrestricted double f; unrestricted double m11; unrestricted double m12; unrestricted double m13 = 0; unrestricted double m14 = 0; unrestricted double m21; unrestricted double m22; unrestricted double m23 = 0; unrestricted double m24 = 0; unrestricted double m31 = 0; unrestricted double m32 = 0; unrestricted double m33 = 1; unrestricted double m34 = 0; unrestricted double m41; unrestricted double m42; unrestricted double m43 = 0; unrestricted double m44 = 1; boolean is2D; };
The following algorithms assume that DOMMatrixReadOnly objects have the internal member variables m11 element, m12 element, m13 element, m14 element, m21 element, m22 element, m23 element, m24 element, m31 element, m32 element, m33 element, m34 element, m41 element, m42 element, m43 element, m44 element and is2D. DOMMatrixReadOnly as well as the inheriting interface DOMMatrix must be able to access and set the value of these variables.
An interface returning an DOMMatrixReadOnly object by an attribute or function may be able to modify internal member variable values. Such an interface must specify this ability explicitly in prose.
Internal member variables must not be exposed in any way.
The DOMMatrix and DOMMatrixReadOnly interfaces replace the SVGMatrix interface from SVG [SVG11].
For historical reasons, Window
objects must also have writable, configurable, non-enumerable
properties named SVGMatrix and WebKitCSSMatrix whose value is the DOMMatrix interface
object.
5.1. DOMMatrixInit dictionary
To validate and fixup a DOMMatrixInit
dictionary dict, run the following steps:
-
If if at least one of the following conditions are true for dict, throw a
TypeError
exception and abort these steps.a
andm11
are both present and their values are not the same.b
andm12
are both present and their values are not the same.c
andm21
are both present and their values are not the same.d
andm22
are both present and their values are not the same.e
andm41
are both present and their values are not the same.f
andm42
are both present and their values are not the same.is2D
istrue
and at least one ofm31
,m32
,m13
,m23
,m43
,m14
,m24
,m34
are present with a value other than 0, or at least one ofm33
,m44
are present with a value other than 1.
- If
m11
is not present then set it to the value of membera
, or value 1 ifa
is also not present. - If
m12
is not present then set it to the value of memberb
, or value 0 ifb
is also not present. - If
m21
is not present then set it to the value of memberc
, or value 0 ifc
is also not present. - If
m22
is not present then set it to the value of memberd
, or value 1 ifd
is also not present. - If
m41
is not present then set it to the value of membere
, or value 0 ife
is also not present. - If
m42
is not present then set it to the value of memberf
, or value 0 iff
is also not present. - If
is2D
is not present and at least one ofm31
,m32
,m13
,m23
,m43
,m14
,m24
,m34
are present with a value other than 0, or at least one ofm33
,m44
are present with a value other than 1, setis2D
tofalse
. - If
is2D
is still not present, set it totrue
.
For the purpose of the algorithm above, two WebIDL unrestricted double values are the same if they have identical IEEE 754 double-precision bit patterns.
Note: This means that two NaN values are the same.
5.2. Creating DOMMatrixReadOnly and DOMMatrix objects
To create a 2d matrix of type type being either DOMMatrixReadOnly
or DOMMatrix
,
with a sequence numberSequence of 6 elements, follow these steps:
-
Let matrix be a new instance of type.
-
Set m11 element, m12 element, m21 element, m22 element, m41 element and m42 element to the values of numberSequence in order starting with the first value.
-
Set m31 element, m32 element, m13 element, m23 element, m43 element, m14 element, m24 element and m34 element to 0.
-
Set m33 element and m44 element to 1.
-
Set is2D to
true
. -
Return matrix
To create a 3d matrix with type being either DOMMatrixReadOnly
or DOMMatrix
,
with a sequence numberSequence of 16 elements, follow these steps:
-
Let matrix be a new instance of type.
-
Set m11 element to m44 element to the values of numberSequence in column-major order.
-
Set is2D to
false
. -
Return matrix
The DOMMatrixReadOnly()
and
the DOMMatrix()
constructors, when
invoked with no argument, must run the following steps:
- Return the result of invoking create a 2d matrix of type
DOMMatrixReadOnly
orDOMMatrix
as appropriate, with the sequence [1, 0, 0, 1, 0, 0].
The DOMMatrixReadOnly(transformList)
and
the DOMMatrix(transformList)
constructors, when invoked with a DOMString
argument, must follow these steps:
- If transformList is the empty string, set it to the string "
matrix(1, 0, 0, 1, 0, 0)
". - Parse transformList into parsedValue by following the syntax description in
“Syntax of the SVG ‘transform’
attribute” [CSS3-TRANSFORMS] to a <transform-list> or the keyword none. If
parsing is not successful, or any <transform-function> has <length> values without absolute length units
, or any keyword other than none is used, throw a
SyntaxError
exception. - If parsedValue is none, set parsedValue to a <transform-list> containing a single identity matrix
-
Let 2dTransform track the 2D/3D dimension status of parsedValue.
- If parsedValue consists of any 3D Transform functions
- Set 2dTransform to
false
. - Otherwise
- Set 2dTransform to
true
.
- Transform all <transform-function>s to 4x4 matrices by following the “Mathematical Description of Transform Functions” [CSS3-TRANSFORMS].
- Let matrix be a 4x4 matrix as shown in the initial figure of this section. Post-multiply all matrices from left to right and set matrix to this product.
-
- If 2dTransform is set to
true
- Return the result of invoking create a 2d matrix of type
DOMMatrixReadOnly
orDOMMatrix
as appropriate, with a sequence of numbers, the values being the elements m11, m12, m21, m22, m41 and m42 of matrix. - If 2dTransform is set to
false
- Return the result of invoking create a 3d matrix of type
DOMMatrixReadOnly
orDOMMatrix
as appropriate, with a sequence of numbers, the values being the 16 elements of matrix.
- If 2dTransform is set to
The DOMMatrixReadOnly(numberSequence)
and
the DOMMatrix(numberSequence)
constructors,
when invoked with a sequence argument, must run the following steps:
- If numberSequence has 6 elements
- Return the result of invoking create a 2d matrix of type
DOMMatrixReadOnly
orDOMMatrix
as appropriate, with the sequence numberSequence. - If numberSequence has 16 elements
- Return the result of invoking create a 3d matrix of type
DOMMatrixReadOnly
orDOMMatrix
as appropriate, with the sequence numberSequence. - Otherwise
- Throw a
TypeError
exception.
The fromMatrix(other)
static method on DOMMatrixReadOnly
must create a DOMMatrixReadOnly
from the dictionary other.
The fromMatrix(other)
static method on DOMMatrix
must create a DOMMatrix
from the dictionary other.
To create a DOMMatrixReadOnly
from a dictionary other or to create a DOMMatrix
from a dictionary other, follow these
steps:
- Validate and fixup other.
-
- If the
is2D
dictionary member of other istrue
- Return the result of invoking create a 2d matrix of type
DOMMatrixReadOnly
orDOMMatrix
as appropriate, with a sequence of numbers, the values being the 6 elementsm11
,m12
,m21
,m22
,m41
andm42
of other in the given order. - Otherwise
- Return the result of invoking create a 3d matrix of type
DOMMatrixReadOnly
orDOMMatrix
as appropriate, with a sequence of numbers, the values being the 16 elementsm11
,m12
,m13
, ...,m44
of other in the given order.
- If the
The fromFloat32Array(array32)
static method on DOMMatrixReadOnly
and
the fromFloat32Array(array32)
static method on DOMMatrix
must follow these steps:
- If array32 has 6 elements
- Return the result of invoking create a 2d matrix of type
DOMMatrixReadOnly
orDOMMatrix
as appropriate, with a sequence of numbers taking the values from array32 in the provided order. - If array32 has 16 elements
- Return the result of invoking create a 3d matrix of type
DOMMatrixReadOnly
orDOMMatrix
as appropriate, with a sequence of numbers taking the values from array32 in the provided order. - Otherwise
- Throw a
TypeError
exception.
The fromFloat64Array(array64)
static method on DOMMatrixReadOnly
and
the fromFloat64Array(array64)
static method on DOMMatrix
must follow these steps:
- If array64 has 6 elements
- Return the result of invoking create a 2d matrix of type
DOMMatrixReadOnly
orDOMMatrix
as appropriate, with a sequence of numbers taking the values from array64 in the provided order. - If array32 has 16 elements
- Return the result of invoking create a 3d matrix of type
DOMMatrixReadOnly
orDOMMatrix
as appropriate, with a sequence of numbers taking the values from array64 in the provided order. - Otherwise
- Throw a
TypeError
exception.
5.3. DOMMatrix attributes
The following attributes m11 to m44 correspond to the 16 items of the matrix interfaces. For the DOMMatrix interface, if the attributes m31, m32, m13, m23, m43, m14, m24, m34 are set to something else than 0 or m33, m44 are set to something else than 1 set is2D to false
.
The m12 attribute, on getting, must return the m12 element value of the object it was invoked on. For the DOMMatrix interface, setting the m12 attribute must set the m12 element value of the object it was invoked on to the new value.
The m13 attribute, on getting, must return the m13 element value of the object it was invoked on. For the DOMMatrix interface, setting the m13 attribute must set the m13 element value of the object it was invoked on to the new value.
The m14 attribute, on getting, must return the m14 element value of the object it was invoked on. For the DOMMatrix interface, setting the m14 attribute must set the m14 element value of the object it was invoked on to the new value.
The m21 attribute, on getting, must return the m21 element value of the object it was invoked on. For the DOMMatrix interface, setting the m21 attribute must set the m21 element value of the object it was invoked on to the new value.
The m22 attribute, on getting, must return the m22 element value of the object it was invoked on. For the DOMMatrix interface, setting the m22 attribute must set the m22 element value of the object it was invoked on to the new value.
The m23 attribute, on getting, must return the m23 element value of the object it was invoked on. For the DOMMatrix interface, setting the m23 attribute must set the m23 element value of the object it was invoked on to the new value.
The m24 attribute, on getting, must return the m24 element value of the object it was invoked on. For the DOMMatrix interface, setting the m24 attribute must set the m24 element value of the object it was invoked on to the new value.
The m31 attribute, on getting, must return the m31 element value of the object it was invoked on. For the DOMMatrix interface, setting the m31 attribute must set the m31 element value of the object it was invoked on to the new value.
The m32 attribute, on getting, must return the m32 element value of the object it was invoked on. For the DOMMatrix interface, setting the m32 attribute must set the m32 element value of the object it was invoked on to the new value.
The m33 attribute, on getting, must return the m33 element value of the object it was invoked on. For the DOMMatrix interface, setting the m33 attribute must set the m33 element value of the object it was invoked on to the new value.
The m34 attribute, on getting, must return the m34 element value of the object it was invoked on. For the DOMMatrix interface, setting the m34 attribute must set the m34 element value of the object it was invoked on to the new value.
The m41 attribute, on getting, must return the m41 element value of the object it was invoked on. For the DOMMatrix interface, setting the m41 attribute must set the m41 element value of the object it was invoked on to the new value.
The m42 attribute, on getting, must return the m42 element value of the object it was invoked on. For the DOMMatrix interface, setting the m42 attribute must set the m42 element value of the object it was invoked on to the new value.
The m43 attribute, on getting, must return the m43 element value of the object it was invoked on. For the DOMMatrix interface, setting the m43 attribute must set the m43 element value of the object it was invoked on to the new value.
The m44 attribute, on getting, must return the m44 element value of the object it was invoked on. For the DOMMatrix interface, setting the m44 attribute must set the m44 element value of the object it was invoked on to the new value.
The a attribute is an alias to the m11 attribute.
The b attribute is an alias to the m12 attribute.
The c attribute is an alias to the m21 attribute.
The d attribute is an alias to the m22 attribute.
The e attribute is an alias to the m41 attribute.
The f attribute is an alias to the m42 attribute.
The following attributes provide status information about DOMMatrixReadOnly.
- is2D, of type boolean, readonly
-
Every DOMMatrixReadOnly object must be flagged with a boolean is2D. This flag indicates that
- The current matrix was initialized as a 2D matrix. See individual creators for more details.
- Only 2D transformation operations were applied. Each mutable or immutable transformation method defines if is2D must be set to
false
.
is2D can never be set to
true
when it was set tofalse
before on a DOMMatrix object with the exception of calling thesetMatrixValue()
method.Returns the value of is2D.
- isIdentity, of type boolean, readonly
- Returns
true
if m12, m13, m14, m21, m23, m24, m31, m32, m34, m41, m42, m43 are 0 and m11, m22, m33, m44 are 1. Otherwise returnsfalse
.
5.4. Immutable transformation methods
The following methods do not modify the current matrix and return a new DOMMatrix object.
- translate(tx, ty, tz)
-
- Let result be the resulting matrix initialized to the values of the current matrix.
- Perform a translateSelf() transformation on result with the arguments tx, ty, tz.
- Return result.
The current matrix is not modified.
- scale(scaleX, scaleY, scaleZ, originX, originY, originZ)
-
- Let result be the resulting matrix initialized to the values of the current matrix.
- Perform a scaleSelf() transformation on result with the arguments scaleX, scaleY, scaleZ, originX, originY, originZ.
- Return result.
The current matrix is not modified.
- scale3d(scale, originX, originY, originZ)
-
- Let result be the resulting matrix initialized to the values of the current matrix.
- Perform a scale3dSelf() transformation on result with the arguments scale, originX, originY, originZ.
- Return result.
The current matrix is not modified.
- rotate(rotX, rotY, rotZ)
-
- Let result be the resulting matrix initialized to the values of the current matrix.
- Perform a rotateSelf() transformation on result with the arguments rotX, rotY, rotZ.
- Return result.
The current matrix is not modified.
- rotateFromVector(x, y)
-
- Let result be the resulting matrix initialized to the values of the current matrix.
- Perform a rotateFromVectorSelf() transformation on result with the arguments x, y.
- Return result.
The current matrix is not modified.
- rotateAxisAngle(x, y, z, angle)
-
- Let result be the resulting matrix initialized to the values of the current matrix.
- Perform a rotateAxisAngleSelf() transformation on result with the arguments x, y, z, angle.
- Return result.
The current matrix is not modified.
- skewX(sx)
-
- Let result be the resulting matrix initialized to the values of the current matrix.
- Perform a skewXSelf() transformation on result with the argument sx.
- Return result.
The current matrix is not modified.
- skewY(sy)
-
- Let result be the resulting matrix initialized to the values of the current matrix.
- Perform a skewYSelf() transformation on result with the argument sy.
- Return result.
The current matrix is not modified.
- multiply(other)
-
- Let result be the resulting matrix initialized to the values of the current matrix.
- Perform a multiplySelf() transformation on result with the argument other.
- Return result.
The current matrix is not modified.
- flipX()
-
- Let result be the resulting matrix initialized to the values of the current matrix.
- Post-multiply result with
DOMMatrix(-1, 0, 0, 1, 0, 0)
. - Return result.
The current matrix is not modified.
- flipY()
-
- Let result be the resulting matrix initialized to the values of the current matrix.
- Post-multiply result with
DOMMatrix(1, 0, 0, -1, 0, 0)
. - Return result.
The current matrix is not modified.
- inverse()
-
- Let result be the resulting matrix initialized to the values of the current matrix.
- Perform a invertSelf() transformation on result.
- Return result.
The current matrix is not modified.
The following methods do not modify the current matrix.
- transformPoint(point)
-
Let point object be the result of invoking create a
DOMPoint
from the dictionary point. Point object is post-multiplied to the current matrix and returns the resulting point. The passed argument does not get modified.Note: Even if is2D of the current matrix returns
true
, a 4x4 matrix multiplication will be performed if the z attribute of point is not 0 or the w attribute of point is not 1. - toFloat32Array()
- Returns the serialized 16 elements m11 to m44 of the current matrix in column-major order as Float32Array [typedarray].
- toFloat64Array()
- Returns the serialized 16 elements m11 to m44 of the current matrix in column-major order as Float64Array [typedarray].
- stringifier()
-
- If is2D is
true
- Return a DOMString in the form of a CSS Transforms <matrix()> function where a is a attribute, b is b attribute, c is c attribute, d is d attribute, e is e attribute, f is f attribute, [CSS3-TRANSFORMS].
- Otherwise
- Return a DOMString in the form of a CSS Transforms <matrix3d()> function where m11 to m44 are set to m11 to m44 attributes [CSS3-TRANSFORMS].
In this example, a matrix is created and several 2D transformation methods are called:
var matrix = new DOMMatrix();matrix.scaleSelf(2); matrix.translateSelf(20,20);
Calling
matrix.toString()
returns the DOMString:"matrix(2, 0, 0, 2, 20, 20)"
In the following example, a matrix is created and several 3D transformation methods are called:
var matrix = new DOMMatrix();matrix.scale3dSelf(2);
For 3D operations, the stringifier returns a DOMString representing a 3D matrix.
Calling
matrix.toString()
after the snippet above returns the DOMString:"matrix3d(2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1)"
- If is2D is
5.5. Mutable transformation methods
The following methods modify the current matrix, so that each method returns the matrix where it was invoked on. The primary benefit of this is allowing content creators to chain method calls.
The following code example:
var matrix = new DOMMatrix();matrix.translateSelf(20, 20);
matrix.scaleSelf(2);
matrix.translateSelf(-20, -20);
is equivalent to:
var matrix = new DOMMatrix();matrix.translateSelf(20, 20).scaleSelf(2).translateSelf(-20, -20);
Note: Authors who use chained method calls are recommended to use mutable transformation methods to avoid unnecessary memory allocations due to creation of intermediate DOMMatrix objects in User Agents.
- multiplySelf(other)
-
- Let other object be the result of invoking create a
DOMMatrix
from the dictionary other. - The other object matrix gets post-multiplied to the current matrix.
- If is2D of other object is
false
, set is2D of the current matrix tofalse
. - Return the current matrix.
- Let other object be the result of invoking create a
- preMultiplySelf(other)
-
- Let other object be the result of invoking create a
DOMMatrix
from the dictionary other. - The other object matrix gets pre-multiplied to the current matrix.
- If is2D of other object is
false
, set is2D of the current matrix tofalse
. - Return the current matrix.
- Let other object be the result of invoking create a
- translateSelf(tx, ty, tz)
-
- Post-multiply a translation transformation on the current matrix. The 3D translation matrix is described in CSS Transforms [CSS3-TRANSFORMS].
-
If tz is specified and not 0, set is2D of the current matrix to
false
. - Return the current matrix.
- scaleSelf(scaleX, scaleY, scaleZ, originX, originY, originZ)
-
- Perform a translateSelf() transformation on the current matrix with the arguments originX, originY, originZ.
- If scaleY is missing, set scaleY to the value of scaleX.
- Post-multiply a non-uniform scale transformation on the current matrix. The 3D scale matrix is described in CSS Transforms with sx = scaleX, sy = scaleY and sz = scaleZ. [CSS3-TRANSFORMS].
- Negate originX, originY and originZ.
- Perform a translateSelf() transformation on the current matrix with the arguments originX, originY, originZ.
- If scaleZ is not 1 or originZ is not 0, set is2D of
the current matrix to
false
. - Return the current matrix.
- scale3dSelf(scale, originX, originY, originZ)
-
- Apply a translateSelf(originX, originY, originZ) transformation to the current matrix.
- Post-multiply a uniform 3D scale transformation (m11 = m22 = m33 = scale) on the current matrix. The 3D scale matrix is described in CSS Transforms with sx = sy = sz = scale. [CSS3-TRANSFORMS].
- Apply a translateSelf(-originX, -originY, -originZ) transformation to the current matrix.
- If scale is not 1, set is2D of the current matrix to
false
. - Return the current matrix.
- rotateSelf(rotX, rotY, rotZ)
-
- If rotY and rotZ are both missing, set rotZ to the value of rotX and set rotX and rotY to zero.
- If rotY is still missing, set rotY to zero.
- If rotZ is still missing, set rotZ to zero.
- If rotX or rotY are non-zero, set is2D of the current matrix
to
false
. - Post-multiply a rotation transformation on the current matrix around the vector 0, 0, 1 by the specified rotation rotZ in degrees. The 3D rotation matrix is described in CSS Transforms with alpha = rotZ in degrees [CSS3-TRANSFORMS].
- Post-multiply a rotation transformation on the current matrix around the vector 0, 1, 0 by the specified rotation rotY in degrees. The 3D rotation matrix is described in CSS Transforms with alpha = rotY in degrees [CSS3-TRANSFORMS].
- Post-multiply a rotation transformation on the current matrix around the vector 1, 0, 0 by the specified rotation rotX in degrees. The 3D rotation matrix is described in CSS Transforms with alpha = rotX in degrees [CSS3-TRANSFORMS].
- Return the current matrix.
- rotateFromVectorSelf(x, y)
-
- Post-multiply a rotation transformation on the current matrix. The rotation angle is determined by the angle between the vector (1,0)T and (x,y)T in the clockwise direction. If x and y should both be zero, the angle is specified as zero. The 2D rotation matrix is described in CSS Transforms where
alpha
is the angle between the vector (1,0)T and (x,y)T in degrees [CSS3-TRANSFORMS]. - Return the current matrix.
- Post-multiply a rotation transformation on the current matrix. The rotation angle is determined by the angle between the vector (1,0)T and (x,y)T in the clockwise direction. If x and y should both be zero, the angle is specified as zero. The 2D rotation matrix is described in CSS Transforms where
- rotateAxisAngleSelf(x, y, z, angle)
-
- Post-multiply a rotation transformation on the current matrix around the specified vector x, y, z by the specified rotation angle in degrees. The 3D rotation matrix is described in CSS Transforms with alpha = angle in degrees [CSS3-TRANSFORMS].
- If x or y are not 0, set is2D of the current matrix to
false
. - Return the current matrix.
- skewXSelf(sx)
-
- Post-multiply a skewX transformation on the current matrix by the specified angle sx in degrees. The 2D skewX matrix is described in CSS Transforms with alpha = sx in degrees [CSS3-TRANSFORMS].
- Return the current matrix.
- skewYSelf(sy)
-
- Post-multiply a skewX transformation on the current matrix by the specified angle sy in degrees. The 2D skewY matrix is described in CSS Transforms with beta = sy in degrees [CSS3-TRANSFORMS].
- Return the current matrix.
- invertSelf()
-
- Invert the current matrix.
- If the current matrix is not invertible set all attributes to NaN and set is2D to
false
. - Return the current matrix.
- setMatrixValue(transformList)
-
- If transformList is the empty string, set it to the string "
matrix(1, 0, 0, 1, 0, 0)
". - Parse transformList by following the syntax description in “Syntax of the SVG ‘transform’
attribute” [CSS3-TRANSFORMS] to a <transform-list>. If parsing is not successful, or
any <transform-function> has <length> values without absolute
length units, or any
keyword other than none is used, throw a
SyntaxError
exception. - Set is2D to
false
if the <transform-list> consists of any 3D Transform functions. Otherwise set is2D totrue
. - Transform all <transform-function>s to 4x4 matrices by following the “Mathematical Description of Transform Functions” [CSS3-TRANSFORMS].
- Post-multiply all matrices from left to right to a combined 4x4 matrix.
- Set the m11 to m44 attributes to the element values of the 4x4 matrix in column-major order.
- Return the current matrix.
- If transformList is the empty string, set it to the string "
6. Cloning
When a user agent is asked to clone an DOMPointReadOnly, DOMPoint, DOMRectReadOnly, DOMRect, DOMQuad, DOMMatrixReadOnly or DOMMatrix object old, it must run the following steps, which return a new object of same type. These steps must be run atomically.
- Create a new object new of the same type as old. Copy all member variable values from old to new.
- Return new. It is the clone.
The above algorithm follows the “safe passing of structured data” [HTML5] definitions.
Changes since last publication
The following changes were made since the 18 September 2014 Working Draft.
-
Changed
DOMMatrixReadOnly
andDOMMatrix
to be compatible withWebKitCSSMatrix
:- Changed rotate() and rotateSelf() arguments from (angle, originX, originY) to (rotX, rotY, rotZ).
- Changed the scale() and scaleSelf() methods to be more like the previous
scaleNonUniform()
/scaleNonUniformSelf()
methods, and dropped thescaleNonUniform*
methods. - Made all arguments optional for
DOMMatrix
/DOMMatrixReadOnly
methods, except for setMatrixValue(). - Changed
fromString()
static method to overloaded constructor. - Added no-argument constructor.
- Defined
window.WebKitCSSMatrix
to aliasDOMMatrix
.
- Exposed DOMPointReadOnly, DOMPoint, DOMRectReadOnly, DOMRect, DOMQuad, DOMMatrixReadOnly and DOMMatrix to
Window
andWorker
. Defined cloning of the interfaces. - The live
bounds
attribute onDOMQuad
was replaced with a non-livegetBounds()
method. The "associated bounding rectangle" concept was also removed.
The following changes were made since the 26 June 2014 Last Call Public Working Draft.
- DOMPointReadOnly got a constructor taking 4 arguments.
- DOMRectReadOnly got a constructor taking 4 arguments.
- DOMMatrixReadOnly got a constructor taking a sequence of numbers as argument.
- DOMRectList turned to an ArrayClass. The interfaces must just be used for legacy interfaces.
- Put DOMRectList on at-Risk awaiting browser feedback.
- All interfaces are described in the sense of internal elements to describe the read-only/writable and inheriting behavior.
- Replace IndexSizeError exception with TypeError.
The following changes were made since the 22 May 2014 First Public Working Draft.
- Renamed mutable transformation methods *By to *Self. (E.g. translateBy() got to translateSelf().)
- Renamed invert() to invertSelf().
- Added
setMatrixValue()
which takes a transformation list as DOMString. - is2D and isIdentity are read-only attributes now.
- DOMMatrixReadOnly gets flagged to track 3D transformation and attribute settings for is2D.
- invertSelf() and inverse() do not throw exceptions anymore.
Acknowledgments
The editors would like to thank Robert O’Callahan for contributing to this specification. Many thanks to Dean Jackson for his initial proposal of DOMMatrix. Thanks to Adenilson Cavalcanti, Benoit Jacob, Boris Zbarsky, Brian Birtles, Cameron McCormack, Domenic Denicola, Kari Pihkala, Max Vujovic, Mike Taylor, Peter Hall, and Philip Jägenstedt for their careful reviews, comments, and corrections.