Skip to content

Viewport ​

A region of the canvas that renders a stack of layers through a camera.

Every viewport draws into the shared canvas through the area of its host element. The element must be unique across viewports.

Viewports also route input. Pointer and wheel events on the host element are enriched with clip and world coordinates and a picking ray, sent to each layer in order and passed to the camera controls unless a layer stops propagation.

ts
const viewport = new Viewport({
  domElement: canvas,
  camera,
  layers: [imageLayer],
});
const idetik = new Idetik({ canvas, viewports: [viewport] });

viewport.addLayer(labelLayer);

Constructor ​

ts
new Viewport(props): Viewport;
ParameterDescription
props: ViewportProps

Types ​

ViewportProps ​

Initialization properties for constructing a viewport.

PropertyDescription
id?: stringUnique id. Defaults to the element id or a generated id.
domElement: HTMLElementHost element.
camera: CameraThe camera the viewport renders with.
layers?: Layer[]Layers to render in order.
cameraControls?: CameraControlsInput controls driving the camera.

Properties ​

id ​

The viewport's unique identifier.

ts
readonly id: string;

domElement ​

The host element defining the viewport's area.

ts
readonly domElement: HTMLElement;

camera ​

The camera the viewport renders with.

ts
readonly camera: Camera;

events ​

The pointer and wheel event dispatcher for the host element.

ts
readonly events: EventDispatcher;

cameraControls? ​

Input controls driving the camera.

ts
optional cameraControls?: CameraControls;

Accessors ​

layers readonly Layer[] ​

The layers rendered by this viewport in order. Layers with occludes set draw before non-occluding layers regardless of stack order.

ts
get layers(): readonly Layer[];

Methods ​

addLayer() void ​

Adds a layer to the top of the stack. Throws if the viewport already contains the layer.

ts
addLayer(layer): void;
ParameterDescription
layer: LayerThe layer to add.

removeLayer() void ​

Removes a previously added layer.

ts
removeLayer(layer): void;
ParameterDescription
layer: LayerThe layer to remove.

removeAllLayers() void ​

Removes all layers from the viewport.

ts
removeAllLayers(): void;

updateSize() void ​

Syncs the camera's aspect ratio to the host element's size. Called automatically when the host element resizes.

ts
updateSize(): void;

getBoxRelativeTo() Box2 ​

Computes the viewport's box relative to the given element in device pixels, with y flipped to match WebGL's bottom-left origin.

ts
getBoxRelativeTo(element): Box2;
ParameterDescription
element: HTMLElementThe element to compute the box against, typically the canvas.

getBufferRect() ​

The viewport's size in device pixels. Measured relative to its own host element, so x and y are always 0.

ts
getBufferRect(): {
  x: number;
  y: number;
  width: number;
  height: number;
};

clientToClip() vec3 ​

Converts a client-space position to clip space. The y axis points down, matching the renderer's mirrored projection.

ts
clientToClip(position, depth?): vec3;
ParameterDescription
position: vec2The client-space position to convert.
depth: numberThe clip-space z value. Defaults to 0.

clientToWorld() vec3 ​

Converts a client-space position such as a pointer location to world space.

ts
clientToWorld(position, depth?): vec3;
ParameterDescription
position: vec2The client-space position to convert.
depth: numberThe clip-space z value. Defaults to 0.

Released under the MIT License