Skip to content
This is the alpha v4 version website. Looking for the v3 documentation?

Camera API

The camera controls what portion of the graph is visible. Access it via renderer.getCamera().

State

The camera state has four properties:

interface CameraState {
x: number; // Horizontal position in framed graph space (0.5 = center)
y: number; // Vertical position in framed graph space (0.5 = center)
angle: number; // Rotation in radians
ratio: number; // Zoom level (smaller = more zoomed in)
}

State access

MethodReturnsDescription
getState()CameraStateGet the current camera state
getPreviousState()CameraState | nullGet the state before the last change
hasState(state)booleanCheck if the camera matches a given state

State updates

MethodReturnsDescription
setState(partial)thisUpdate state immediately. Emits "updated" event
updateState(fn)thisUpdate state using a function: fn(currentState) => partialState

Both of these functions emit an "updated" event after, but only if the state has effectively changed.

Animation

// Promise-based
await camera.animate({ x: 0.5, y: 0.5, ratio: 0.5 });
// With options
await camera.animate({ ratio: 0.2 }, { duration: 1000, easing: "cubicInOut" });
// Callback-based
camera.animate({ x: 0.5 }, { duration: 500 }, () => console.log("done"));

AnimateOptions

PropertyTypeDefaultDescription
durationnumber150Animation duration in ms
easingEasing"quadraticInOut"Easing function (see below)

Easing options

Named: "linear", "quadraticIn", "quadraticOut", "quadraticInOut", "cubicIn", "cubicOut", "cubicInOut", "exponentialIn", "exponentialOut", "exponentialInOut"

Custom easing: any (t: number) => number function where t goes from 0 to 1.

Convenience methods

MethodReturnsDescription
animatedZoom(factor?)Promise<void>Zoom in by factor (default: 1.5). Also accepts { factor?, duration?, easing? }
animatedUnzoom(factor?)Promise<void>Zoom out by factor (default: 1.5)
animatedReset(opts?)Promise<void>Animate to default state (x=0.5, y=0.5, angle=0, ratio=1)
isAnimated()booleanCheck if an animation is currently running

Lifecycle

MethodReturnsDescription
enable()thisEnable camera updates
disable()thisDisable all camera updates
copy()CameraCreate an independent copy with the same state
Camera.from(state)CameraStatic: create a new camera initialized to the given state

Interaction flags

Fine-grained flags toggle what kind of state updates the camera accepts. They are honored by validateState(), so both direct setState calls and animations respect them.

PropertyTypeDefaultDescription
enabledZoomingbooleantrueAccept ratio changes
enabledPanningbooleantrueAccept x/y changes
enabledRotationbooleantrueAccept angle changes

Custom state validation

PropertyTypeDefaultDescription
clean((state: CameraState) => CameraState) | nullnullOptional hook applied inside validateState(). Receives the merged next state and returns a possibly constrained replacement.

Bounds

PropertyTypeDescription
minRationumber | nullMinimum zoom level (set via minCameraRatio setting)
maxRationumber | nullMaximum zoom level (set via maxCameraRatio setting)
MethodReturnsDescription
getBoundedRatio(ratio)numberConstrain a ratio to min/max bounds
validateState(partial)Partial<CameraState>Validate and constrain a state change

Events

The camera emits a single event:

camera.on("updated", (state: CameraState) => {
// Camera state changed
});