/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * Pure camera maths for the Schematic renderer — the single source of truth for * the pan/zoom transform, the interaction-mode machine, viewport culling * geometry, and the `requestAnimationFrame` coalescer. Kept free of React, * Chakra, and Canvas2D so every piece is unit-testable in a `node` environment * and so the **same** world→screen projection is shared by the Canvas2D painter * ({@link ../paint}) and the DOM card layer — the two surfaces can never use a * different transform and drift apart (issue #57, P1). * * @packageDocumentation */ /** The pan/zoom state: `screen = world × fit × zoom + t`. */ export interface Viewport { zoom: number; tx: number; ty: number; } /** The identity camera — fully zoomed out, no pan: the camera before the canvas has a size to fit to. */ export declare const IDENTITY: Viewport; /** The room Fit leaves round the drawing, in CSS px each side — a zone's eyebrow, standing above its box, within it. */ export declare const FIT_MARGIN_PX = 24; /** * The fit, and the camera Fit takes (#1393): the drawing's extent fitted * inside the canvas less a margin each side ({@link FIT_MARGIN_PX}, no more * than an eighth of the canvas), so nothing drawn at its edge meets the * canvas's — and centred on both axes, so the canvas's middle, which the * toolbar's zoom zooms about, is the drawing's. * * @param width - The canvas's width, in CSS px * @param height - Its height, in CSS px * @param worldW - The extent's width, in world units * @param worldH - Its height, in world units * @returns The fit — px per world unit at zoom 1 — and the camera at it; `0` and {@link IDENTITY} while there is nothing to fit */ export declare function fitView(width: number, height: number, worldW: number, worldH: number): { fit: number; home: Viewport; }; /** * The transform in CSS pixels with `ppu` (pixels-per-world-unit) already * folded in: `screen = world × ppu + t`. Mirrors {@link ../paint!PaintCamera}. */ export interface ScreenCamera { ppu: number; tx: number; ty: number; } /** An axis-aligned bounding box (world or screen space). */ export interface Bbox { minX: number; minY: number; maxX: number; maxY: number; } /** * Project a world point to screen space (`x × ppu + tx`, `y × ppu + ty`). The * one projector shared by the painter and the card layer; the DOM cards apply * the identical arithmetic in CSS via {@link cardTranslateCss}. * * @param x - world x * @param y - world y * @param cam - the screen camera (ppu + translation) * @returns the screen-space point */ export declare function project(x: number, y: number, cam: ScreenCamera): { sx: number; sy: number; }; /** * The CSS `translate(...)` a DOM card uses to sit on its world anchor, reading * the live camera from the `--cam-ppu` / `--cam-tx` / `--cam-ty` custom * properties set on the card layer. The arithmetic is identical to * {@link project} (`ppu × coord + t`), so card and canvas-drawn footprint * resolve to the same screen point. The string is **camera-independent** (it * only embeds the item's world `x`/`y`), so a camera change is a single set of * custom-property writes on the parent — no per-card React re-layout. * * @param x - world x of the item anchor * @param y - world y of the item anchor * @returns a CSS `translate()` expression referencing the `--cam-*` vars */ export declare function cardTranslateCss(x: number, y: number): string; /** * The CSS length a DOM card uses for an explicit world-space `width`, reading * the live `--cam-ppu`. Expressing the width through the var (not a * render-time `width × ppu`) keeps an explicit-width card's box sized from the * **same** live ppu the canvas footprint is drawn from, so box and footprint * never diverge in size during a zoom (issue #57, Phase 2 invariant 6). * * @param width - the item's world-space width * @returns a CSS `calc()` length referencing `--cam-ppu` */ export declare function cardWidthCss(width: number): string; /** * A world box's CSS geometry on the card layer's camera (#1398): its left and * top edges where {@link cardTranslateCss} would place a card's anchor — * `ppu × coord + t` — and its width and height as {@link cardWidthCss} sizes a * card, each reading the live `--cam-ppu` / `--cam-tx` / `--cam-ty` custom * properties, so the box follows the camera with no render: an unread area's * wash. * * @param box - The box, in world units * @returns Its `left`, `top`, `width` and `height`, as CSS lengths referencing the `--cam-*` vars */ export declare function boxCss(box: Bbox): { left: string; top: string; width: string; height: string; }; /** * The world-space rectangle currently visible — the inverse projection of the * canvas corners. Used to cull off-screen zones / links (issue #57, P6). * * @param cam - the screen camera * @param width - canvas width in CSS px * @param height - canvas height in CSS px * @returns the visible world bbox */ export declare function viewportWorldBbox(cam: ScreenCamera, width: number, height: number): Bbox; /** * Whether two axis-aligned bounding boxes overlap (inclusive edges). Off-screen * shapes whose bbox misses the viewport bbox are culled. * * @param a - first bbox * @param b - second bbox * @returns `true` when the boxes touch or overlap */ export declare function bboxOverlaps(a: Bbox, b: Bbox): boolean; /** * The world-space AABB over a set of points. For a link this is taken over * **all** anchors (endpoints + waypoints), so a trunk whose two ends sit * off-screen but whose segment crosses the viewport is not wrongly culled * (issue #57, 1e). * * @param pts - the points to bound (world coords) * @returns the AABB, or `null` when the list is empty */ export declare function pointsBbox(pts: readonly { x: number; y: number; }[]): Bbox | null; /** * Apply a multiplicative zoom about a screen-space focal point, keeping that * point fixed on screen (cursor-anchored wheel zoom, centre-anchored button * zoom). Clamps to `[min, max]`; one clamped to `min` returns `home` — the * camera Fit takes ({@link fitView}) — so a full zoom-out also recentres. * * @param prev - the current camera * @param factor - multiply `zoom` by this * @param px - screen-x to keep fixed * @param py - screen-y to keep fixed * @param min - minimum zoom * @param max - maximum zoom * @param home - the camera at `min`: Fit's * @returns the next camera */ export declare function zoomAbout(prev: Viewport, factor: number, px: number, py: number, min: number, max: number, home?: Viewport): Viewport; /** * Apply one step of a pinch: zoom by its factor about the point the fingers' * midpoint stood at, and carry what the drawing shows there to where the * midpoint stands now — so each finger stays on the point of the drawing it * touched, and two fingers carried together, their distance kept, pan. * * The zoom is held to `[min, max]`, and what the fingers ask for past a limit * is remembered, not lost (`over`): drawn together past the least zoom and * apart again, the drawing zooms only once they stand further apart than where * the limit held it. Without that a limit is a ratchet — two fingers carried * together arrive one move at a time, the first changing their distance and the * second putting it back, and at the least zoom the first would be dropped and * the second zoom in. At a limit the step carries the drawing by the midpoint's * move alone. It never snaps to Fit's camera as {@link zoomAbout} does — a * camera that jumped under the fingers would leave them — so a pinch in that * comes to rest at `min` is settled there by its caller, as it ends. * * @param prev - the current camera * @param factor - the fingers' distance now over their distance before the move * @param from - where the fingers' midpoint stood, in px from the canvas's corner * @param to - where it stands now * @param min - minimum zoom * @param max - maximum zoom * @param over - how far past a limit the fingers stood before the move: the zoom they asked for over the zoom the * limits allowed, 1 within them * @returns the next camera, and how far past a limit the fingers stand now */ export declare function pinchAbout(prev: Viewport, factor: number, from: { readonly x: number; readonly y: number; }, to: { readonly x: number; readonly y: number; }, min: number, max: number, over?: number): { camera: Viewport; over: number; }; /** Moving slower than a viewport each this many ms, the canvas tells its reader where its view stands as it moves (#1398, CL6). */ export declare const SLOW_READ_MS = 200; /** * Zooming by less than this factor each {@link SLOW_READ_MS}, the canvas tells its reader as it zooms (#1398, CL6). A * faster zoom asks for a new view at every notch, which the next notch leaves before its read lands. */ export declare const SLOW_ZOOM = 1.25; /** Where the canvas last told its reader its view stood: when, the view's centre in world units, and its zoom in px a unit. */ export interface ViewTold { readonly at: number; readonly x: number; readonly y: number; readonly ppu: number; } /** * Whether the camera, still moving, tells its reader where its view stands now, rather than once it rests (#1398, CL6). * It does once {@link SLOW_READ_MS} have passed since it last told it, its centre has moved less than the view's span, * and its zoom has moved by less than {@link SLOW_ZOOM}, in or out. So a slow pan or a slow zoom reads as it goes, and * a fast one reads where it rests: a zoom about a point moves the view's centre little, but each notch of a fast one is * a view the next leaves before it lands. A time stamped before the last tell is no time since it. * * @param told - Where it last told its reader the view stood * @param now - Where the view stands now: when, its centre and its zoom, and its span in world units * @returns Whether it tells its reader now */ export declare function tellsAsItMoves(told: ViewTold, now: ViewTold & { readonly span: number; }): boolean; /** How long a fly takes, in ms. */ export declare const FLY_DURATION_MS = 350; /** * Where a fly stands a time into it (#1393): eased out of the camera it began * at towards the one it lands at. A time before its start stands where it * began — a frame the page stamped before the gesture that started it, or one * stamped on another clock — never thrown back past it, as an ease of a * negative time would; a time past its end stands where it lands. * * @param from - The camera it began at * @param to - The camera it lands at * @param elapsed - How long it has flown, in ms * @returns The camera */ export declare function flyStep(from: Viewport, to: Viewport, elapsed: number): Viewport; /** The interaction modes the camera can be in at any instant. */ export type CameraMode = "idle" | "panning" | "wheeling" | "flying"; /** The inputs that drive {@link nextMode} transitions. */ export type CameraEvent = "pointerDown" | "wheel" | "zoom" | "flyStart" | "pointerUp" | "flyEnd" | "cancel"; /** * The camera interaction-mode transition. Entering an input mode * (`pointerDown` / `wheel` / `zoom`) supersedes a running fly — see * {@link cancelsFly} for the companion side-effect predicate — so a user * gesture always wins over an animation rather than racing it (issue #57, P4 / * Phase 2 invariant 5). * * @param mode - the current mode * @param event - the input event * @returns the next mode */ export declare function nextMode(mode: CameraMode, event: CameraEvent): CameraMode; /** * Whether processing `event` in `mode` must cancel a running fly animation. * Pairs with {@link nextMode}: the reducer names the next mode, this names the * side-effect (cancel the in-flight `flyTo` rAF) the transition implies. Every * event that moves the machine OUT of `flying` (a user gesture, an explicit * `cancel`/`reset`) must cancel the rAF, so the mode and the live `animRef` can * never disagree — `flyEnd` is the sole exception (the rAF has already ended). * * @param mode - the current mode * @param event - the input event * @returns `true` when a fly animation is in progress and is being superseded */ export declare function cancelsFly(mode: CameraMode, event: CameraEvent): boolean; /** A frame-coalesced render scheduler — see {@link makeRafCoalescer}. */ export interface RafCoalescer { /** Schedule `run` for the next frame; repeat calls before it fires are no-ops. */ request(run: () => void): void; /** Cancel a pending frame (teardown). */ cancel(): void; } /** * Coalesce many `request` calls within a single frame down to **one** scheduled * callback. Pointer-move / wheel handlers mutate the live camera synchronously * and call `request` on every event; this guarantees at most one paint + * snapshot per frame, keeping the hot pan path off the per-event path (issue * #57, Phase 2 invariant 2). Injecting `schedule` / `cancel` (rather than * closing over `requestAnimationFrame`) keeps the coalescer unit-testable. * * @param schedule - schedules a callback for the next frame, returns a handle * @param cancel - cancels a scheduled callback by handle * @returns a {@link RafCoalescer} */ export declare function makeRafCoalescer(schedule: (cb: () => void) => number, cancel: (handle: number) => void): RafCoalescer; //# sourceMappingURL=camera.d.ts.map