@rasamjs/viewport
v0.3.1
Published
Pan and zoom for rasam SVG drawings.
Readme
@rasamjs/viewport
The optional editor adapter owns minimap lifecycle and follows document/viewport replacement through structural capabilities.
Pan and zoom for rasam SVG drawings. Peer dependency: @rasamjs/core version
^0.2.0. @rasamjs/[email protected] is published. The programmatic
camera, wheel pan/zoom, keyboard shortcuts, Space/middle-button pan and two-finger
pinch are available.
Unreleased editor additions provide primary-button hand mode, native-input
suspension and immutable camera snapshots with direct restoration.
import { SVG } from '@rasamjs/core';
import { Viewport } from '@rasamjs/viewport';
import { exportSvg } from '@rasamjs/core/interchange';
const drawing = SVG(document.querySelector<HTMLElement>('#drawing')!, { width: 600, height: 400 });
const viewport = new Viewport(drawing);
const camera = viewport.camera;
viewport.hand = true; // Primary-button drag pans with a grab cursor.
viewport.suspended = true; // Pause camera input while another tool owns a gesture.
viewport.setCamera(camera); // Programmatic methods still work while suspended.
viewport.suspended = false;
viewport.hand = false;
viewport.zoomTo(2, { x: 100, y: 80 });
viewport.panBy(30, 20);
viewport.fit();
// Export the original document viewBox, then restore the camera immediately.
const svg = viewport.withHome(() => exportSvg(drawing));
// Dispose explicitly, or destroy the drawing to dispose automatically.
viewport.destroy();The camera is the root <svg>'s viewBox. Camera changes are written as
temporary changes, so @rasamjs/history never records them as undo steps.
Give the drawing host overflow-anchor: none to keep page scroll anchoring from
following shapes when the camera moves. Pass editor overlays in overlays so they
redraw once per animation frame. Do not edit the root viewBox directly while a
viewport is active. Use a synchronous withHome callback for export; a plain
exportSvg(drawing) saves the current camera.
Wheel pans; Shift+wheel pans across; Ctrl/Cmd+wheel zooms at the pointer. Set
wheel: 'zoom' to zoom plain wheels as well. Ctrl/Cmd+wheel and trackpad pinch
continue to zoom in either mode. Ctrl/Cmd+= and
Ctrl/Cmd+- zoom about the view center; Ctrl/Cmd+0 shows actual size. Shift+1 fits
the drawing and Shift+2 fits options.selection.nodes. Keyboard shortcuts accept
body or drawing-host focus, leave editable fields and outside focus alone, and
can be disabled with keyboard: false.
Hold Space and drag, or drag with the middle mouse button, to pan without moving
or selecting shapes. Two touch pointers take over an existing edit, restore that
edit's starting state, then zoom and pan around their midpoint. The host disables
native touch gestures while pinch is enabled. Use spacePan: false,
middlePan: false or pinch: false to opt out. Destroy restores the host cursor
and touch-action styles.
Hand mode also supports a primary touch. Suspension releases camera captures and
leaves pointer presses available for editing; resume restores hand mode's cursor.
See API.md and RELEASING.md.
Use with an editor
The viewport only needs core at runtime. Connect the optional editing packages through their public APIs:
import { History } from '@rasamjs/history';
import { Selection, connectTransformer } from '@rasamjs/selection';
import { Transformer } from '@rasamjs/transformer';
const history = new History(drawing);
const transformer = new Transformer(drawing);
const selection = new Selection(drawing);
const disconnect = connectTransformer(selection, transformer);
const viewport = new Viewport(drawing, { selection, overlays: [transformer] });
selection.on('change', () => history.breakMerge());
viewport.on('change', ({ zoom }) => {
zoomLabel.textContent = `${Math.round(zoom * 100)}%`;
});
// On teardown, disconnect the bridge and restore host styles before destroying editors.
disconnect();
viewport.destroy();
selection.destroy();
transformer.destroy();
history.destroy();Selection refreshes itself after camera changes; list the transformer in overlays
to refresh its controls. Make the drawing host focusable (tabindex="0") and return
focus to it after toolbar actions so keyboard shortcuts remain available. Existing
roots must be mounted, outermost SVGs with uniform preserveAspectRatio scaling.
Run bun run --filter @rasamjs/viewport dev from the workspace, then open
http://127.0.0.1:4179/examples/index.html. The editor example
combines zoom controls, selection, shared handles, undo/redo and home-view SVG
download. Build the workspace first with bun run build for editor dependencies.
Run bun run --filter @rasamjs/viewport bench:wheel for the 5,000-shape wheel benchmark.
It writes wheel-benchmark.json, including frame
intervals and input-to-change latency for Chromium, Firefox and WebKit. Synthetic
input excludes OS routing; rAF intervals do not isolate paint time. Results are
observations rather than a hardware speed requirement. Correctness checks cover
coalescing, focus, history and an unchanged home-view export.
Use the optional minimap for a limited-rate drawing preview with
a draggable camera rectangle:
import { Minimap } from '@rasamjs/viewport/minimap';
const minimap = new Minimap(drawing, minimapHost, { viewport });