class Geometry · Computations

Viewports

Computes the origin of a viewport: pan, zoom and rotation, fitting and focusing, and the conversion between world and viewport coordinates.

The state of a viewport is its origin, a pin: its position is where world (0, 0) appears in viewport coordinates, its direction holds the zoom (length) and the rotation (angle). A world point p appears at position + zoom · rotate(p, angle). Viewport coordinates are those of the viewport's frame.

The functions return a new pin, or write it into result; pass the viewport's origin as result to change the view. reset is the exception: it changes the pin it gets.

Static methods

focusOnstatic

static focusOn(origin: Pin | PinLike, pin: Pin | PinLike, screenTarget: Point | VectorLike, result?: Pin | null): Pin

Adjusts the viewport so that a given world pin aligns with a target screen position, preserving the current scale and orientation.

origin
The current viewport pin (position and orientation).
pin
The world-space pin to bring into view.
screenTarget
The screen-space position to align pin with.
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns A pin representing the viewport translated so that pin coincides with screenTarget.

focusOnScaledstatic

static focusOnScaled(pin: Pin | PinLike, screenTarget: Point | VectorLike, result?: Pin | null): Pin

Adjusts the viewport so that a given world pin aligns with a target screen position, applying the scale encoded in pin to fit the view.

pin
The world-space pin carrying both the target position and the desired scale.
screenTarget
The screen-space position to align pin with.
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns A pin representing the viewport scaled and translated so that pin coincides with screenTarget.

getAnglestatic

static getAngle(origin: Pin | PinLike, result?: Real | null): Real

Gets the current rotation angle of the viewport.

origin
The viewport state to read from.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The viewport's current rotation angle.

getPanstatic

static getPan(origin: Pin | PinLike, result?: Point | null): Point

Gets the pan: where world (0, 0) appears in viewport coordinates.

origin
The viewport state to read from.
result optional
Optional Point to write the result into; it is returned. Without it, a new Point is returned.

Returns Where world (0, 0) appears in viewport coordinates.

getZoomstatic

static getZoom(origin: Pin | PinLike, result?: Real | null): Real

Gets the current zoom level of the viewport.

origin
The viewport state to read from.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The viewport's current zoom factor.

panBystatic

static panBy(origin: Pin | PinLike, delta: Vector | VectorLike, result?: Pin | null): Pin

Pans the viewport by the specified direction vector.

origin
The viewport state to transform.
delta
The amount and direction to pan by, in viewport coordinates.
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns A viewport state shifted by delta.

panPointTostatic

static panPointTo(origin: Pin | PinLike, point: Point | VectorLike, target: Point | VectorLike, result?: Pin | null): Pin

Pans the viewport so that a specific world point aligns with a target screen position.

origin
The current viewport pin (position and orientation).
point
The world-space point to reposition.
target
The screen-space position to move point to.
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns A pin representing the panned viewport where point coincides with target.

resetstatic

static reset(origin: Pin | PinLike): Pin

Resets the pin it gets to no pan, zoom 1 and no rotation, and returns it.

origin
The viewport state to reset.

Returns A viewport state with all transforms reset to their defaults.

resetPanstatic

static resetPan(origin: Pin | PinLike, result?: Pin | null): Pin

Resets only the pan of the viewport, leaving zoom and rotation unchanged.

origin
The viewport state to transform.
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns A viewport state with pan reset to its default.

resetZoomstatic

static resetZoom(origin: Pin | PinLike, result?: Pin | null): Pin

Resets only the zoom of the viewport, leaving pan and rotation unchanged.

origin
The viewport state to transform.
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns A viewport state with zoom reset to its default.

rotateBystatic

static rotateBy(origin: Pin | PinLike, angle: Real | NumberLike, anchor: Point | VectorLike, result?: Pin | null): Pin

Rotates the viewport by the specified angle around a world-space anchor point.

origin
The viewport state to transform.
angle
The angle to rotate by.
anchor
The world-space point to rotate around.
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns A viewport state rotated by angle around anchor.

rotateTostatic

static rotateTo(origin: Pin | PinLike, anchor: Point | VectorLike, angle: Real | NumberLike, result?: Pin | null): Pin

Rotates the viewport around a given anchor point to a specified angle.

origin
The current viewport pin (position and orientation).
anchor
The world-space point to rotate around.
angle
The target rotation angle in radians.
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns A pin representing the viewport rotated to angle around anchor.

transformPinTostatic

static transformPinTo(pin: Pin | PinLike, screenTarget: Point | VectorLike, scale: Real | NumberLike, result?: Pin | null): Pin

Positions and scales the viewport so that a given world pin maps exactly to a target screen position at a specified scale.

pin
The world-space pin to bring into view.
screenTarget
The screen-space position to align pin with.
scale
The zoom level to apply to the viewport.
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns A pin representing the viewport at scale with pin coinciding with screenTarget.

viewportToWorldstatic

static viewportToWorld(origin: Pin | PinLike, viewportPoint: Point | VectorLike, result?: Point | null): Point

Converts a point in viewport coordinates to world coordinates.

origin
The viewport state defining the current transform.
viewportPoint
The point in viewport space to convert.
result optional
Optional Point to write the result into; it is returned. Without it, a new Point is returned.

Returns The corresponding point in world space.

worldToViewportstatic

static worldToViewport(origin: Pin | PinLike, worldPoint: Point | VectorLike, result?: Point | null): Point

Converts a point in world coordinates to viewport coordinates.

origin
The viewport state defining the current transform.
worldPoint
The point in world space to convert.
result optional
Optional Point to write the result into; it is returned. Without it, a new Point is returned.

Returns The corresponding point in viewport space.

zoomBystatic

static zoomBy(origin: Pin | PinLike, factor: Real | NumberLike, anchor: Point | VectorLike, result?: Pin | null): Pin

Multiplies the viewport's current zoom level by the specified factor, keeping the anchor point stationary in the view.

origin
The viewport state to transform.
factor
The factor to multiply the current zoom by.
anchor
The point in viewport coordinates that stays in place.
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns A viewport state with zoom scaled by factor around anchor.

zoomTostatic

static zoomTo(origin: Pin | PinLike, scale: Real | NumberLike, screenTarget: Point | VectorLike, result?: Pin | null): Pin

Zooms the viewport to a new scale, keeping a given screen position fixed in place. The world point currently under screenTarget remains stationary after the zoom.

origin
The current viewport pin (position and orientation).
scale
The new zoom level to apply.
screenTarget
The screen-space position that should remain fixed during the zoom.
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns A pin representing the zoomed viewport with screenTarget anchored in place.