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 defaults to the canvas itself and 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 idetik = new Idetik({
  canvas,
  viewports: [{ id: 'main', camera, layers: [imageLayer] }],
});

const viewport = idetik.getViewport('main')!;
viewport.addLayer(labelLayer);

Types ​

ViewportProps ​

Initialization properties for constructing a viewport.

PropertyDescription
id?: stringUnique id. Defaults to the element id or a generated id.
element?: HTMLElementHost element. Defaults to the Idetik canvas.
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;

element ​

The host element defining the viewport's area.

ts
readonly element: 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.

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 canvas in device pixels.

ts
getBoxRelativeTo(canvas): Box2;
ParameterDescription
canvas: HTMLCanvasElementThe canvas to compute the box against.

getBufferRect() ​

The viewport's rectangle in the drawing buffer in device pixels.

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