1. Introduction
This section is non-normative.
This specification describes several geometry interfaces for the representation of points, rectangles, quadrilaterals and transformation matrices with the dimension of 3x2 and 4x4.
The SVG interfaces SVGPoint
, SVGRect
and SVGMatrix
are aliasing the here defined
interfaces in favor for common interfaces used by SVG, Canvas 2D Context and CSS
Transforms. [SVG11] [HTML] [CSS3-TRANSFORMS]
2. The DOMPoint interfaces
A 2D or a 3D point can be represented by the following WebIDL interfaces:
[Exposed =(Window ,Worker ),Serializable ]interface {
DOMPointReadOnly constructor (optional unrestricted double = 0,
x optional unrestricted double = 0,
y optional unrestricted double = 0,
z optional unrestricted double = 1); [
w 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 ; [NewObject ]DOMPoint matrixTransform (optional DOMMatrixInit = {}); [
matrix Default ]object (); }; [
toJSON Exposed =(Window ,Worker ),Serializable ,LegacyWindowAlias =]
SVGPoint interface :
DOMPoint DOMPointReadOnly {constructor (optional unrestricted double = 0,
x optional unrestricted double = 0,
y optional unrestricted double = 0,
z optional unrestricted double = 1); [
w 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 = 0;
x unrestricted double = 0;
y unrestricted double = 0;
z unrestricted double = 1; };
w
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
must 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.
x
attribute, on getting, must return the x
coordinate value. For the DOMPoint
interface, setting the x
attribute must set the x coordinate to the new value.
The y
attribute, on getting, must return the y
coordinate value. For the DOMPoint
interface, setting the y
attribute must set the y coordinate to the new value.
The z
attribute, on getting, must return the z
coordinate value. For the DOMPoint
interface, setting the z
attribute must set the z coordinate to the new value.
The w
attribute, on getting, must return the w
perspective value. For the DOMPoint
interface, setting the w
attribute must set the w perspective to the new value.
The matrixTransform(matrix)
method, when invoked, must run the following steps:
-
Let matrixObject be the result of invoking create a
DOMMatrix
from the dictionary matrix. -
Return the result of invoking transform a point with a matrix, given the current point and matrixObject. The current point does not get modified.
matrixTransform()
on a DOMPoint
instance is
called with a DOMMatrix
instance as argument.
var point = new DOMPoint( 5 , 4 );
var matrix = new DOMMatrix([ 2 , 0 , 0 , 2 , 10 , 10 ]);
var transformedPoint = point. matrixTransform( matrix);
The point variable is set to a new DOMPoint
object with x
coordinate initialized to 5 and y coordinate initialized to 4. This new DOMPoint
is now scaled and the translated by matrix. This resulting transformedPoint has the x coordinate 20 and y
coordinate 18.
2.1. Transforming a point with a matrix
To transform a point with a matrix, given point and matrix:
-
Let x be point’s x coordinate.
-
Let y be point’s y coordinate.
-
Let z be point’s z coordinate.
-
Let w be point’s w perspective.
-
Let pointVector be a new column vector with the elements being x, y, z, and w, respectively.
-
Set pointVector to pointVector pre-multiplied by matrix.
-
Let transformedPoint be a new
DOMPoint
object. -
Set transformedPoint’s x coordinate to pointVector’s first element.
-
Set transformedPoint’s y coordinate to pointVector’s second element.
-
Set transformedPoint’s z coordinate to pointVector’s third element.
-
Set transformedPoint’s w perspective to pointVector’s fourth element.
-
Return transformedPoint.
Note: If matrix’s is 2D is true, point’s z coordinate is 0 or -0, and point’s w perspective is 1, then this is a 2D transformation. Otherwise this is a 3D transformation.
3. The DOMRect interfaces
Objects implementing the DOMRectReadOnly
interface represent a rectangle.
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.
[Exposed =(Window ,Worker ),Serializable ]interface {
DOMRectReadOnly constructor (optional unrestricted double = 0,
x optional unrestricted double = 0,
y optional unrestricted double = 0,
width optional unrestricted double = 0); [
height 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 Default ]object (); }; [
toJSON Exposed =(Window ,Worker ),Serializable ,LegacyWindowAlias =]
SVGRect interface :
DOMRect DOMRectReadOnly {constructor (optional unrestricted double = 0,
x optional unrestricted double = 0,
y optional unrestricted double = 0,
width optional unrestricted double = 0); [
height 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 = 0;
x unrestricted double = 0;
y unrestricted double = 0;
width unrestricted double = 0; };
height
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.
x
attribute, on getting, must return the x
coordinate value. For the DOMRect
interface, setting the x
attribute must set the x coordinate to the new value.
The y
attribute, on getting, it must return the y
coordinate value. For the DOMRect
interface, setting the y
attribute must set the y coordinate to the new value.
The width
attribute, on getting, must return the width
dimension value. For the DOMRect
interface, setting the width
attribute must set the width dimension 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 the NaN-safe minimum of
the y coordinate and the sum of the y coordinate and the height dimension.
The right
attribute, on getting, must return the NaN-safe maximum of
the x coordinate and the sum of the x coordinate and the width dimension.
The bottom
attribute, on getting, must return the NaN-safe maximum of the y coordinate and the sum of the y coordinate and
the height dimension.
The left
attribute, on getting, must return the NaN-safe minimum of
the x coordinate and the sum of the x coordinate and the width dimension.
4. The DOMRectList interface
[Exposed =Window ]interface {
DOMRectList readonly attribute unsigned long length ;getter DOMRect ?