Geometry Interfaces Module Level 1

Editor’s Draft,

More details about this document
This version:
https://drafts.fxtf.org/geometry/
Latest published version:
https://www.w3.org/TR/geometry-1/
Previous Versions:
Test Suite:
http://test.csswg.org/suites/geometry-1_dev/nightly-unstable/
Editors:
(Adobe Inc.)
(Google)
Former Editors:
(Opera Software AS)
(Magic Leap)
Issue Tracking:
GitHub Issues
Suggest an Edit for this Spec:
GitHub Editor

Abstract

This specification provides basic geometric interfaces to represent points, rectangles, quadrilaterals and transformation matrices that can be used by other modules or specifications.

Status of this document

This is a public copy of the editors’ draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don’t cite this document other than as work in progress.

GitHub Issues are preferred for discussion of this specification. When filing an issue, please put the text “geometry” in the title, preferably like this: “[geometry] …summary of comment…”. All issues and comments are archived, and there is also a historical archive.

This document was produced by the CSS Working Group (part of the Style Activity).

This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This document is governed by the 03 November 2023 W3C Process Document.

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 x = 0, optional unrestricted double y = 0,
            optional unrestricted double z = 0, optional unrestricted double w = 1);

    [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 x = 0, optional unrestricted double y = 0,
            optional unrestricted double z = 0, optional unrestricted double w = 1);

    [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:

  1. Let point be a new DOMPointReadOnly or DOMPoint object as appropriate.

  2. Set point’s variables x coordinate to x, y coordinate to y, z coordinate to z and w perspective to w.

  3. 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:

  1. Let point be a new DOMPointReadOnly or DOMPoint as appropriate.

  2. Set point’s variables x coordinate to other’s x dictionary member, y coordinate to other’s y dictionary member, z coordinate to other’s z dictionary member and w perspective to other’s w dictionary member.

  3. Return point.

The 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:

  1. Let matrixObject be the result of invoking create a DOMMatrix from the dictionary matrix.

  2. Return the result of invoking transform a point with a matrix, given the current point and matrixObject. The current point does not get modified.

In this example the method 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:

  1. Let x be point’s x coordinate.

  2. Let y be point’s y coordinate.

  3. Let z be point’s z coordinate.

  4. Let w be point’s w perspective.

  5. Let pointVector be a new column vector with the elements being x, y, z, and w, respectively.

    x y z w
  6. Set pointVector to pointVector pre-multiplied by matrix.

  7. Let transformedPoint be a new DOMPoint object.

  8. Set transformedPoint’s x coordinate to pointVector’s first element.

  9. Set transformedPoint’s y coordinate to pointVector’s second element.

  10. Set transformedPoint’s z coordinate to pointVector’s third element.

  11. Set transformedPoint’s w perspective to pointVector’s fourth element.

  12. 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 x = 0, optional unrestricted double y = 0,
            optional unrestricted double width = 0, optional unrestricted double height = 0);

    [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 x = 0, optional unrestricted double y = 0,
            optional unrestricted double width = 0, optional unrestricted double height = 0);

    [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:

  1. Let rect be a new DOMRectReadOnly or DOMRect object as appropriate.

  2. Set rect’s variables x coordinate to x, y coordinate to y, width dimension to width and height dimension to height.

  3. 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:

  1. Let rect be a new DOMRectReadOnly or DOMRect as appropriate.

  2. Set rect’s variables x coordinate to other’s x dictionary member, y coordinate to other’s y dictionary member, width dimension to other’s width dictionary member and height dimension to other’s height dictionary member.

  3. Return rect.

The 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? item(unsigned long index);
};

The length attribute must return the total number of DOMRect objects associated with the object.

The item(index) method, when invoked, must return null when index is greater than or equal to the number of DOMRect objects associated with the DOMRectList. Otherwise, the DOMRect object at index must be returned. Indices are zero-based.

DOMRectList only exists for compatibility with legacy Web content. When specifying a new API, DOMRectList must not be used. Use sequence<DOMRect> instead. [WEBIDL]

5. The DOMQuad interface

Objects implementing the DOMQuad interface represents a quadrilateral.

[Exposed=(Window,Worker),
 Serializable]
interface DOMQuad {
    constructor(optional DOMPointInit p1 = {}, optional DOMPointInit p2 = {},
            optional DOMPointInit p3 = {}, optional DOMPointInit p4 = {});

    [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();

    [Default] object toJSON();
};

dictionary DOMQuadInit {
  DOMPointInit p1;
  DOMPointInit p2;
  DOMPointInit p3;
  DOMPointInit p4;
};

The following algorithms assume that DOMQuad objects have the internal member variables point 1, point 2, point 3, and point 4, which are DOMPoint objects. DOMQuad must be able to access and set the value of these variables. The author can modify these DOMPoint objects, which directly affects the quadrilateral.

An interface returning a DOMQuad 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 DOMQuad(p1, p2, p3, p4) constructor, when invoked, must run the following steps:

  1. Let point1 be a new DOMPoint object with its attributes set to the values of the namesake dictionary members in p1.

  2. Let point2 be a new DOMPoint object with its attributes set to the values of the namesake dictionary members in p2.

  3. Let point3 be a new DOMPoint object with its attributes set to the values of the namesake dictionary members in p3.

  4. Let point4 be a new DOMPoint object with its attributes set to the values of the namesake dictionary members in p4.

  5. Return a new DOMQuad with point 1 set to point1, point 2 set to point2, point 3 set to point3 and point 4 set to point4.

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:

  1. Let x, y, width and height be the value of other’s x, y, width and height dictionary members, respectively.

  2. Let point1 be a new DOMPoint object with x coordinate set to x, y coordinate set to y, z coordinate set to 0 and w perspective set to 1.

  3. Let point2 be a new DOMPoint object with x coordinate set to x + width, y coordinate set to y, z coordinate set to 0 and w perspective set to 1.

  4. Let point3 be a new DOMPoint object with x coordinate set to x + width, y coordinate set to y + height, z coordinate set to 0 and w perspective set to 1.

  5. Let point4 be a new DOMPoint object with x coordinate set to x, y coordinate set to y + height, z coordinate set to 0 and w perspective set to 1.

  6. Return a new DOMQuad with point 1 set to point1, point 2 set to point2, point 3 set to point3 and point 4 set to point4.

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:

  1. Let point1 be the result of invoking create a DOMPoint from the dictionary p1 dictionary member of other, if it exists.

  2. Let point2 be the result of invoking create a DOMPoint from the dictionary p2 dictionary member of other, if it exists.

  3. Let point3 be the result of invoking create a DOMPoint from the dictionary p3 dictionary member of other, if it exists.

  4. Let point4 be the result of invoking create a DOMPoint from the dictionary p4 dictionary member of other, if it exists.

  5. Return a new DOMQuad with point 1 set to point1, point 2 set to point2, point 3 set to point3 and point 4 set to point4.

The p1 attribute must return point 1.

The p2 attribute must return point 2.

The p3 attribute must return point 3.

The p4 attribute must return point 4.

The getBounds() method, when invoked, must run the following algorithm:

  1. Let bounds be a DOMRect object.

  2. Let left be the NaN-safe minimum of point 1’s x coordinate, point 2’s x coordinate, point 3’s x coordinate and point 4’s x coordinate.

  3. Let top be the NaN-safe minimum of point 1’s y coordinate, point 2’s y coordinate, point 3’s y coordinate and point 4’s y coordinate.

  4. Let right be the NaN-safe maximum of point 1’s x coordinate, point 2’s x coordinate, point 3’s x coordinate and point 4’s x coordinate.

  5. Let bottom be the NaN-safe maximum of point 1’s y coordinate, point 2’s y coordinate, point 3’s y coordinate and point 4’s y coordinate.

  6. Set x coordinate of bounds to left, y coordinate of bounds to top, width dimension of bounds to right - left and height dimension of bounds to bottom - top.

  7. 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: 12, y: 10}, {x: 2, y: 10});

The attribute values of the resulting DOMQuad quad1 above are also equivalent 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});
An irregular quadrilateral represented by a DOMQuad. The four red colored circles represent the DOMPoint attributes p1 to p4. The dashed rectangle represents the bounding rectangle returned by the getBounds() method of the DOMQuad.

6. 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.

m 11 m 21 m 31 m 41 m 12 m 22 m 32 m 42 m 13 m 23 m 33 m 43 m 14 m 24 m 34 m 44
A 4x4 abstract matrix 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.

[Exposed=(Window,Worker),
 Serializable]
interface DOMMatrixReadOnly {
    constructor(optional (DOMString or sequence<unrestricted double>) init);

    [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
    [NewObject] DOMMatrix translate(optional unrestricted double tx = 0,
                                    optional unrestricted double ty = 0,
                                    optional unrestricted double tz = 0);
    [NewObject] 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);
    [NewObject] DOMMatrix scaleNonUniform(optional unrestricted double scaleX = 1,
                                          optional unrestricted double scaleY = 1);
    [NewObject] DOMMatrix scale3d(optional unrestricted double scale = 1,
                                  optional unrestricted double originX = 0,
                                  optional unrestricted double originY = 0,
                                  optional unrestricted double originZ = 0);
    [NewObject] DOMMatrix rotate(optional unrestricted double rotX = 0,
                                 optional unrestricted double rotY,
                                 optional unrestricted double rotZ);
    [NewObject] DOMMatrix rotateFromVector(optional unrestricted double x = 0,
                                           optional unrestricted double y = 0);
    [NewObject] DOMMatrix rotateAxisAngle(optional unrestricted double x = 0,
                                          optional unrestricted double y = 0,
                                          optional unrestricted double z = 0,
                                          optional unrestricted double angle = 0);
    [NewObject] DOMMatrix skewX(optional unrestricted double sx = 0);
    [NewObject] DOMMatrix skewY(optional unrestricted double sy = 0);
    [NewObject] DOMMatrix multiply(optional DOMMatrixInit other = {});
    [NewObject] DOMMatrix flipX();
    [NewObject] DOMMatrix flipY();
    [NewObject] DOMMatrix inverse();

    [NewObject] DOMPoint transformPoint(optional DOMPointInit point = {});
    [NewObject] Float32Array toFloat32Array();
    [NewObject] Float64Array toFloat64Array();

    [Exposed=Window] stringifier;
    [Default] object toJSON();
};

[Exposed=(Window,Worker),
 Serializable,
 LegacyWindowAlias=(SVGMatrix,WebKitCSSMatrix)]
interface DOMMatrix : DOMMatrixReadOnly {
    constructor(optional (DOMString or sequence<unrestricted double>) init);

    [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);