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?