Camera
On this page
Imports
import { Camera } from "@snap-engine/core";
import { CameraControl } from "@snap-engine/asset-base"; Coordinate Conversion
const { x, y } = object.worldTransform;
const [cameraX, cameraY] = object.cameraPosition;
const [screenX, screenY] = object.screenPosition;
object.worldTransform = { x: 100, y: 200 };
object.cameraPosition = [50, 100];
object.screenPosition = [300, 400]; Camera API
The engine creates a stationary camera when a container is assigned. You can also assign a Camera instance directly.
import { Engine, Camera } from "@snap-engine/core";
const engine = new Engine();
const camera = new Camera(engine);
engine.camera = camera;
camera.containerDom = engine.containerElement; camera.setCameraPosition(0, 0);
camera.setCameraCenterPosition(100, 100);
const center = camera.getCameraCenterPosition();
const zoom = camera.zoom;
canvasElement.style.transform = camera.canvasStyle; Coordinate Spaces
| Space | Origin | Units |
|---|---|---|
| Screen | Browser viewport | Viewport (client) pixels |
| Camera | Camera container’s top-left corner | Viewport pixels |
| World | Scene origin | CSS pixels inside the camera layer |
Inside the camera layer, CSS lengths are world units: a 120px wide element
is 120 world units wide at any zoom, while its getBoundingClientRect() width
is 120 * zoom. ElementObject.readDom() reports world-space boxes and keeps
the viewport rectangle as box.screen.
Coordinate Methods
const [cameraX, cameraY] = camera.getCameraFromWorld(100, 200);
const [worldX, worldY] = camera.getWorldFromCamera(cameraX, cameraY);
const [screenX, screenY] = camera.getScreenFromCamera(cameraX, cameraY);
const [containerX, containerY] = camera.getCameraFromScreen(screenX, screenY); const [cameraWidth, cameraHeight] = camera.getCameraDeltaFromWorldDelta(10, 10);
const [worldDeltaX, worldDeltaY] = camera.getWorldDeltaFromCameraDelta(50, 30); The root package also exports helpers that resolve a point or rectangle in
every space at once. Each accepts null for “no camera”, in which case all
spaces coincide.
import {
pointerPositionFromScreen,
pointerPositionFromWorld,
worldRectFromScreenRect,
} from "@snap-engine/core";
const pointer = pointerPositionFromScreen(engine.camera, event.clientX, event.clientY);
pointer.x; // world
pointer.camera.x; // camera
pointer.screen.x; // viewport
const worldRect = worldRectFromScreenRect(
engine.camera,
element.getBoundingClientRect(),
); CameraControl Setup
<div id="container" style="overflow: hidden; position: relative;">
<div id="camera-wrapper">
<div class="object">Object 1</div>
<div class="object">Object 2</div>
</div>
</div> import { Engine } from "@snap-engine/core";
import { CameraControl } from "@snap-engine/asset-base";
const engine = new Engine();
const container = document.getElementById("container");
const wrapper = document.getElementById("camera-wrapper");
if (container && wrapper) {
engine.element = container;
const cameraControl = new CameraControl(engine, {
zoomLock: false,
panLock: false,
});
cameraControl.element = wrapper;
cameraControl.setCameraCenterPosition(0, 0);
} CameraControl Methods
cameraControl.setCameraPosition(0, 0);
cameraControl.setCameraCenterPosition(100, 100);
const center = cameraControl.getCameraCenterPosition();
cameraControl.zoomBy(0.1);
cameraControl.zoomBy(-0.1);
cameraControl.zoomBy(0.1, 200, 150); Camera changes and their painted transform are one commit: CameraControl writes the transform immediately when a DOM write is allowed, and a programmatic change requested during a read stage waits for the next write stage. A DOM read therefore never sees a zoom that is not on screen yet.
Edge panning for drag owners
Enable programmatic edge panning when composing the camera with a drag system such as SnapLine:
const cameraControl = new CameraControl(engine, {
edgePan: {
enabled: true,
edgeDistance: 48,
maxSpeed: 600,
},
}); Drag owners use startEdgePan(pointerId, position, onFrame), updateEdgePan(pointerId, position), and stopEdgePan(pointerId). The frame
callback receives the pointer position converted through the camera after each
pan, keeping dragged content anchored while the pointer is stationary.
Public API
| API | Description |
|---|---|
new Camera(engine, config?) | Creates a camera for an engine. |
camera.containerDom = element | Assigns the camera container. |
camera.cameraWidth | Current container width. |
camera.cameraHeight | Current container height. |
camera.cameraPositionX | Current world X position. |
camera.cameraPositionY | Current world Y position. |
camera.zoom | Current zoom value. |
camera.canvasStyle | CSS transform string for the camera wrapper. |
camera.setCameraPosition(x, y) | Sets the top-left world position. |
camera.setCameraCenterPosition(x, y) | Centers the camera on a world point. |
camera.getCameraCenterPosition() | Returns the current world center. |
camera.handleScroll(deltaZoom, cameraX, cameraY) | Applies zoom around a camera-space point. |
camera.handlePan(deltaX, deltaY) | Applies direct pan movement. |
camera.handlePanStart() | Starts tracked pan movement. |
camera.handlePanDrag(deltaX, deltaY) | Updates tracked pan movement. |
camera.handlePanEnd() | Ends tracked pan movement. |
new CameraControl(engine, config?) | Creates interactive pan/zoom controls. |
cameraControl.zoomBy(deltaZoom, originX?, originY?) | Programmatically changes zoom. |
pointerPositionFromScreen(camera, x, y) | Resolves a viewport point as a PointerPosition. |
pointerPositionFromWorld(camera, x, y) | Resolves a world point as a PointerPosition. |
worldRectFromScreenRect(camera, rect) | Maps a viewport rectangle into world space. |