Skip to content

Camera

The camera is created alongside the scene and renderer. It manages the view matrix on the Context3D and supports orbit, pan, zoom, and lookAt operations. Property changes are batched via microtasks for efficient updates.

Creation

ts
import { createCamera } from '@ripl/3d';

const camera = createCamera(scene, {
    position: [0, 2, 5],
    target: [0, 0, 0],
    fov: 60,
    near: 0.1,
    far: 1000,
    projection: 'perspective',
});

Options

OptionTypeDefaultDescription
positionVector3[0, 0, 5]Camera position in world space
targetVector3[0, 0, 0]Point the camera looks at
upVector3[0, 1, 0]Up direction
fovnumber60Field of view in degrees
nearnumber0.1Near clipping plane
farnumber1000Far clipping plane
projectionstring'perspective''perspective' or 'orthographic'

Methods

orbit(deltaTheta, deltaPhi)

Orbits the camera around the target point.

ts
camera.orbit(0.1, 0.05);

pan(deltaX, deltaY)

Pans the camera (shifts both position and target).

ts
camera.pan(1, 0);

zoom(delta)

Moves the camera along the eye-to-target vector.

ts
camera.zoom(2);

lookAt(target)

Points the camera at a new target.

ts
camera.lookAt([5, 5, 5]);

flush()

Immediately applies pending changes (bypasses microtask batching).

ts
camera.flush();

Reactive Updates

Setting properties like camera.position, camera.target, or camera.fov schedules a microtask to update the context. Multiple changes in the same synchronous block are batched into a single update.

ts
camera.position = [1, 2, 3];
camera.target = [0, 0, 0];
camera.fov = 90;
// All three changes are applied in a single microtask

Interactions

The camera supports built-in mouse interactions for zoom, pivot (orbit), and pan. Enable them via the interactions option:

ts
const camera = createCamera(scene, {
    interactions: true, // enable all interactions with default sensitivity
});

For granular control, pass an object:

ts
const camera = createCamera(scene, {
    interactions: {
        zoom: { enabled: true, sensitivity: 5 },
        pivot: true,
        pan: true,
    },
});

Interaction Options

OptionTypeDefaultDescription
interactionsboolean | CameraInteractionsundefinedEnable/disable all interactions or configure individually

Each interaction (zoom, pivot, pan) accepts boolean or CameraInteractionConfig:

PropertyTypeDefaultDescription
enabledbooleantrueWhether the interaction is active
sensitivitynumber1Multiplier for the interaction speed

Controls

ActionInput
Orbit / PivotLeft-click + drag
PanMiddle-click + drag, or Shift + left-click + drag
ZoomScroll wheel

dispose()

Removes all interaction event listeners.

ts
camera.dispose();

Demo