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): PinAdjusts 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
pinwith. resultoptional- Optional
Pinto write the result into; it is returned. Without it, a newPinis 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): PinAdjusts 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
pinwith. resultoptional- Optional
Pinto write the result into; it is returned. Without it, a newPinis 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): RealGets the current rotation angle of the viewport.
origin- The viewport state to read from.
resultoptional- Optional
Realto write the result into; it is returned. Without it, a newRealis returned.
Returns The viewport's current rotation angle.
getPanstatic
static getPan(origin: Pin | PinLike, result?: Point | null): PointGets the pan: where world (0, 0) appears in viewport coordinates.
origin- The viewport state to read from.
resultoptional- Optional
Pointto write the result into; it is returned. Without it, a newPointis returned.
Returns Where world (0, 0) appears in viewport coordinates.
getZoomstatic
panBystatic
static panBy(origin: Pin | PinLike, delta: Vector | VectorLike, result?: Pin | null): PinPans the viewport by the specified direction vector.
origin- The viewport state to transform.
delta- The amount and direction to pan by, in viewport coordinates.
resultoptional- Optional
Pinto write the result into; it is returned. Without it, a newPinis returned.
Returns A viewport state shifted by delta.
panPointTostatic
static panPointTo(origin: Pin | PinLike, point: Point | VectorLike, target: Point | VectorLike, result?: Pin | null): PinPans 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
pointto. resultoptional- Optional
Pinto write the result into; it is returned. Without it, a newPinis returned.
Returns A pin representing the panned viewport where point coincides with target.
resetstatic
resetPanstatic
static resetPan(origin: Pin | PinLike, result?: Pin | null): PinResets only the pan of the viewport, leaving zoom and rotation unchanged.
origin- The viewport state to transform.
resultoptional- Optional
Pinto write the result into; it is returned. Without it, a newPinis returned.
Returns A viewport state with pan reset to its default.
resetZoomstatic
static resetZoom(origin: Pin | PinLike, result?: Pin | null): PinResets only the zoom of the viewport, leaving pan and rotation unchanged.
origin- The viewport state to transform.
resultoptional- Optional
Pinto write the result into; it is returned. Without it, a newPinis 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): PinRotates 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.
resultoptional- Optional
Pinto write the result into; it is returned. Without it, a newPinis 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): PinRotates 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.
resultoptional- Optional
Pinto write the result into; it is returned. Without it, a newPinis 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): PinPositions 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
pinwith. scale- The zoom level to apply to the viewport.
resultoptional- Optional
Pinto write the result into; it is returned. Without it, a newPinis 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): PointConverts a point in viewport coordinates to world coordinates.
origin- The viewport state defining the current transform.
viewportPoint- The point in viewport space to convert.
resultoptional- Optional
Pointto write the result into; it is returned. Without it, a newPointis returned.
Returns The corresponding point in world space.
worldToViewportstatic
static worldToViewport(origin: Pin | PinLike, worldPoint: Point | VectorLike, result?: Point | null): PointConverts a point in world coordinates to viewport coordinates.
origin- The viewport state defining the current transform.
worldPoint- The point in world space to convert.
resultoptional- Optional
Pointto write the result into; it is returned. Without it, a newPointis returned.
Returns The corresponding point in viewport space.
zoomBystatic
static zoomBy(origin: Pin | PinLike, factor: Real | NumberLike, anchor: Point | VectorLike, result?: Pin | null): PinMultiplies 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.
resultoptional- Optional
Pinto write the result into; it is returned. Without it, a newPinis 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): PinZooms 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.
resultoptional- Optional
Pinto write the result into; it is returned. Without it, a newPinis returned.
Returns A pin representing the zoomed viewport with screenTarget anchored in place.
