Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Note SPBoucher AI8 * File : src/lib/canvas/viewport.ts9 * Description : viewport mathematics of the canvas. Convention for10 * `viewport: {x, y, zoom}` of spn-canvas-1 (documented in11 * docs/next/CANVAS.md so every client reads it the same12 * way): (x, y) is the WORLD point shown at the top-left13 * corner of the view and `zoom` is screen pixels (points)14 * per world unit — screen = (world − {x, y}) × zoom. Pure.15 * Module : lib16 * Repository : https://git.spboucher.ai/note-spboucher-ai17 * Project Website : https://note.spboucher.ai18 * Created : 2026-11-1919 * Last Modified : 2026-11-1920 *21 * Author : Simon-Pierre Boucher22 * Position : Professor of Finance23 * Institution : Université du Québec en Outaouais (UQO)24 * Organization : SP Boucher25 * Website : https://spboucher.ai26 * Git : https://git.spboucher.ai27 *28 * Preferred Contact: contact@spboucher.ai29 * Development : dev@spboucher.ai30 * Support : support@spboucher.ai31 * Security/Admin : admin@spboucher.ai32 *33 * Copyright (c) 202634 * Simon-Pierre Boucher35 * All rights reserved where applicable. This file is governed by the36 * applicable project license, copyright notices and relevant law.37 * Existing third-party rights and notices remain unaffected.38 *39 * License : None specified (no license file in this repository)40 *41 * Do not embed credentials, private keys, access tokens,42 * certificates or confidential personal information.43 *44 * =============================================================================45 * END OF SP BOUCHER SOURCE HEADER46 * =============================================================================47 */4849import type { Point, Rect } from "./core";5051export interface Viewport { x: number; y: number; zoom: number; [k: string]: unknown }52export interface Size { w: number; h: number }53export const ZOOM_MIN = 0.05, ZOOM_MAX = 4;54export const clampZoom = (z: number, min = ZOOM_MIN, max = ZOOM_MAX) => (Number.isFinite(z) ? Math.min(max, Math.max(min, z)) : 1);55export const worldToScreen = (v: Viewport, p: Point): Point => ({ x: (p.x - v.x) * v.zoom, y: (p.y - v.y) * v.zoom });56export const screenToWorld = (v: Viewport, p: Point): Point => ({ x: p.x / v.zoom + v.x, y: p.y / v.zoom + v.y });57/** The world rectangle visible in a view of `size` screen pixels, grown by `margin` screen pixels on every side. */58export const visibleRect = (v: Viewport, size: Size, margin = 0): Rect => ({ x: v.x - margin / v.zoom, y: v.y - margin / v.zoom, w: (size.w + 2 * margin) / v.zoom, h: (size.h + 2 * margin) / v.zoom });59/** Pans by a screen-space delta (dragging the content by (dx, dy) pixels). */60export const panBy = (v: Viewport, dx: number, dy: number): Viewport => ({ ...v, x: v.x - dx / v.zoom, y: v.y - dy / v.zoom });61/** Zooms to `zoom` (clamped) keeping the world point under the screen point `at` fixed — zoom around the cursor / the pinch centre. */62export function zoomTo(v: Viewport, zoom: number, at: Point): Viewport {63 const z = clampZoom(zoom); if (z === v.zoom) return v;64 const w = screenToWorld(v, at);65 return { ...v, zoom: z, x: w.x - at.x / z, y: w.y - at.y / z };66}67export const zoomBy = (v: Viewport, factor: number, at: Point): Viewport => zoomTo(v, v.zoom * factor, at);68/**69 * The viewport that shows `bounds` entirely, centred, with `padding` screen pixels around it. Never zooms in beyond `maxZoom`70 * (1 by default: fitting a single small card does not magnify it). Null bounds (empty canvas) → origin at zoom 1.71 */72export function fitToContent(bounds: Rect | null, size: Size, padding = 48, maxZoom = 1): Viewport {73 if (!bounds || !(size.w > 0) || !(size.h > 0)) return { x: 0, y: 0, zoom: 1 };74 const availW = Math.max(1, size.w - 2 * padding), availH = Math.max(1, size.h - 2 * padding);75 const zoom = clampZoom(Math.min(bounds.w > 0 ? availW / bounds.w : Infinity, bounds.h > 0 ? availH / bounds.h : Infinity, maxZoom));76 return { zoom, x: bounds.x + bounds.w / 2 - size.w / (2 * zoom), y: bounds.y + bounds.h / 2 - size.h / (2 * zoom) };77}78/** The viewport of the same zoom centred on a world point. */79export const centerOn = (v: Viewport, p: Point, size: Size): Viewport => ({ ...v, x: p.x - size.w / (2 * v.zoom), y: p.y - size.h / (2 * v.zoom) });80/** Wheel / pinch delta → zoom factor (exponential, so zooming in then out by the same delta returns to the start). */81export const wheelZoomFactor = (deltaY: number, pinch: boolean) => Math.exp(-deltaY * (pinch ? 0.01 : 0.0015));82