/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * Pure Canvas2D paint layer for the Schematic. Given a 2D context, the East * value, a camera, the (already culled / LOD-decided) visible set, and a * theme-resolved palette, it draws the **bulk shapes** — zones (rect * outline / hatch + circle / polyline / polygon geometry, arcs included), links, * item footprints, and the dot / pin LOD markers. Rich item *cards* stay DOM * (the React layer draws * those at close zoom); this module never touches React, Chakra, or the DOM, so * it is unit-testable under any Canvas2D implementation (browser or node-skia). * * Every colour it draws is a design-system token the theme bridge resolved * (`theme.tsx`, #1387): its own marks' roles, and each entity's colour by its * id — its colour token, its tone, or its status's, resolved as the cards * resolve theirs (D14). Its labels draw in the font the bridge read from the * recipe: the design system's mono face, each letter in a cell of its own — * the page turns the face's joins off, which a canvas's text need not take, so * every text it draws or measures turns them off itself: `textRendering`, and a * non-joiner in each pair the face would join (`unjoined`) for Safari, whose * canvas has no `textRendering`. A marker whose colour is its status's while * its status is unread — its details row read — draws in the placeholders' * fill once the builder's reveal says so, and is unseen before (#1398): never a * status it has not read. While what it draws stands in for a new narrowing, * its words — a labelled pin's and a link's caption — draw in the third ink, as * a zone's eyebrow always does; every mark and edge is as it was (the loading * design's retained card). * * Its links and nets draw in the shared links look (#1403), through the shared * Canvas2D painter (`shared/links/canvas.ts`) the Plan's and the Allocation's * look is drawn by on SVG: each routed between the items it joins — orthogonal * or direct, through its bends, two of one pair fanned into lanes, a net as its * bus — its ends on the items' edges where they draw at the zoom (a card's * side, a labelled pin's edge, a dot's rim), its head where it arrives, a net's * on each of its destination legs; cased in the paper, in the muted ink or its * style's tone, a caption on its knockout at its route's middle, and the lit * and the selected ones in the strong ink, their ends ringed. Its colours and * its caption's font are the shared part's (`linkRibbons`), as the bridge * reads them. Its routes are pure too, in `routes.ts` (`linkRoutes`, * `netRoutes`), and its markers' geometry in `markers.ts` (`markerHitbox`), so * the hit-tests and a selected link's handles take the routes and the markers * the painter draws. * * @packageDocumentation */ import { type ValueTypeOf } from "@elaraai/east"; import { Schematic } from "@elaraai/e3-ui/internal"; import { type CanvasLinkLook } from "../../shared/links/canvas.js"; import type { SchematicDensity } from "../area.js"; import { type Bbox } from "./camera"; import { type DrawnItem, type SchematicDrawn } from "./drawn"; type SchematicGeometryValue = ValueTypeOf; type Pt = { x: number; y: number; }; /** A screen-space shape vertex carrying its DXF bulge (0 = straight to next). */ type Vert = { x: number; y: number; bulge: number; }; /** Per-item LOD tier (mirrors the React layer). */ export type LodTier = "card" | "label" | "dot"; /** An `[r, g, b]` triple in 0–255. */ export type RGB = [number, number, number]; /** * What the paint layer draws with, resolved by the theme bridge * (`theme.tsx`) from the design system's tokens through the recipe, once per * theme: its own marks' colours, each a role named after its token, and its * fonts. The paint layer is colour-system-agnostic and consumes RGB and font * strings. */ export interface SchematicPalette { /** `brandMark`: marks and dots — a match's ring, a connection's flash. */ brandMark: RGB; /** `brand.solid`: the brand's line — a selected zone's and link's edge, a draft, a frame. */ brandSolid: RGB; /** `brandTint`: the brand's area fill — a selected zone's wash. */ brandTint: RGB; /** `fg`: ink — a selected marker's ring, a label. */ fg: RGB; /** `fg.strong`: strong secondary ink. */ fgStrong: RGB; /** `fg.muted`: secondary ink — a metric, an eyebrow, a context mark. */ fgMuted: RGB; /** `fg.subtle`: labels and captions. */ fgSubtle: RGB; /** `fg.faint`: muted marks. */ fgFaint: RGB; /** `bg.surface`: paper — a pill's and a handle's fill, a dot's ring. */ bgSurface: RGB; /** `bg.canvas`: the canvas's paper — an eyebrow's halo. */ bgCanvas: RGB; /** `border.subtle`: a pill's rule. */ borderSubtle: RGB; /** `border.strong`: a pin's rule. */ borderStrong: RGB; /** `status.pos`. */ statusPos: RGB; /** `status.neg`: a refused connection. */ statusNeg: RGB; /** `status.warn`. */ statusWarn: RGB; /** `status.info`. */ statusInfo: RGB; /** `bg.subtle`: the placeholders' fill — a marker whose status is unread (#1398). */ bgSubtle: RGB; /** `heat.1` … `heat.5`, lightest first: a density cell's steps (#1393). */ heat1: RGB; heat2: RGB; heat3: RGB; heat4: RGB; heat5: RGB; /** The labels' font — a labelled pin's and a link's pill — in the design system's mono face, as `ctx.font` takes it. */ labelFont: string; /** A zone's eyebrow's font, in the mono face. */ eyebrowFont: string; /** * What links and nets draw in (#1403): the colours and the caption's font the page computes for the shared links * part's elements (`linkRibbons`), which the Plan's and the Allocation's links draw with too. */ links: SchematicLinkLook; } /** * What the Schematic's links draw in (#1403): the shared look's colours and caption font, and a link drag's new line * and a moved link's `was` line, each with its width — every one the page computes for the shared links part's * elements. */ export interface SchematicLinkLook extends CanvasLinkLook { /** A link drag's new line: Connect's draft. */ readonly preview: string; /** Its width (px). */ readonly previewWidth: number; /** A moved link's line while a retarget drags its end. */ readonly was: string; /** Its width (px). */ readonly wasWidth: number; } /** The pan/zoom transform in CSS pixels: `screen = world × ppu + t`. */ export interface PaintCamera { ppu: number; tx: number; ty: number; } /** * What the rail's narrowing paints (#1392, §10.3): the rows it leaves out drawn * as context, and its matches emphasised and framed. Absent from * {@link PaintInput} ⇒ nothing narrows. A narrowed-out row that is hidden is * never drawn, so `excluded` holds only what draws as context. */ export interface SchematicPaintEffect { /** What draws as context, by id — items, zones, links and nets — faded, and greyed when `excludedDesaturate`; a net's leg to a context item too. */ excluded: ReadonlySet; /** Context's alpha (0–1); `1` ⇒ no fade. */ excludedOpacity: number; /** Drain context's colour to the muted ink. */ excludedDesaturate: boolean; /** The matches, by id: what `emphasis` rings — an item's marker, a zone's edge, a link's or a net's line. */ matches: ReadonlySet; /** The matches' emphasis — a ring, or one that breathes; `undefined` ⇒ none. */ emphasis: "halo" | "pulse" | undefined; /** Animated pulse phase in `[0, 1)`; only read when `emphasis === "pulse"`. */ pulsePhase: number; /** World-coordinate bbox framing the matches; `undefined` ⇒ no frame. */ frame: Bbox | undefined; } /** Everything {@link paintSchematic} needs for one frame. */ export interface PaintInput { ctx: CanvasRenderingContext2D; /** What the canvas draws: each kind's entities, the local edits in place. */ value: SchematicDrawn; cam: PaintCamera; /** Canvas size in CSS px (the ctx is pre-scaled by devicePixelRatio). */ width: number; height: number; /** Viewport-culled items (the React layer's `visibleItems`). */ visibleItems: readonly DrawnItem[]; /** Per-item LOD tier after declutter. */ tiers: ReadonlyMap; /** The set of selected item keys (single/multiple/range all share it). */ selected: ReadonlySet; /** * The items holding a conflict not yet chosen, by key: each marker wears the conflicts' warn ring, a 1px ring just * outside it — a card's is its element's. None by default. */ conflicted?: ReadonlySet; /** The set of selected ZONE keys (#177); absent/empty ⇒ no zone highlight. */ selectedZones?: ReadonlySet; /** Item key → world centre, for link endpoints. */ centers: ReadonlyMap; palette: SchematicPalette; /** * Each entity's colour, by its id: an item's (its markers' and its * footprint's), a zone's, a link's and a net's — its colour token, else * its tone, else its status's or its pattern's, as the bridge resolved it * (`theme.tsx`). An entity it lacks draws in its kind's ground colour. */ tints: ReadonlyMap; /** What the rail's narrowing paints: context, emphasis and a frame (#1392). */ effect?: SchematicPaintEffect; /** Entity keys in a hidden layer — zones / links with a hidden-layer key are * skipped (items are already pre-filtered out of `visibleItems`). Empty ⇒ no * layer filtering. */ layerHiddenKeys?: ReadonlySet; /** Item key → layer opacity (0–1), for items in a dimmed layer. Multiplies * the item marker / footprint alpha. Absent / missing key ⇒ full. */ layerAlpha?: ReadonlyMap; /** * The in-flight connect draft (#176): the shared drag preview from the item it runs from — `from` its centre, in * world coords, `fromKey` its id — to the pointer (`to`), or once `snapped`, to the item `toKey` names, ending on * each one's edge, with a head (#1403); `forbiddenKey` the target the veto refuses, the draft in the danger ink and * its marker ringed. */ draftLink?: { from: Pt; to: Pt; fromKey: string; toKey?: string; snapped: boolean; forbiddenKey?: string; }; /** One-shot connect commit flash (#176); `phase` 0..1. Endpoints may be * undefined when an item vanished mid-flash — the painter skips then. */ connectFlash?: { from: Pt | undefined; to: Pt | undefined; phase: number; }; /** The selected link (#176): halo stroke; `editable` also draws the * endpoint connector handles for re-targeting. A net LEG selection * narrows the halo to that one stub (#189 follow-up). */ selectedLink?: { key: string; editable: boolean; leg?: { end: "source" | "destination"; item: string; }; }; /** * Where a library card's drop lands while its drag rests there (#1390): * the point a template's item or a backlog row lands at, in world units — * a ring there — or the entity a card lands on, by its id: a zone's * dashed brand edge, a link's or a net's brand wash, a marker's brand * ring. A card-tier item's mark is its DOM card's (`data-drop-target`). */ dropMark?: { kind: "point"; x: number; y: number; } | { kind: "entity"; id: string; }; /** * Each card's size as the page lays it out, by its item's id (#1403): what a link's end meets at card zoom. A card * it lacks is reckoned as the declutter reckons it (`tierSize`). */ cardSizes?: ReadonlyMap; /** The link or the net under the pointer, by its id (#1403): drawn last, in the strong ink, its ends ringed. */ lit?: string; /** The link whose end a retarget drags, by its id (#1403): drawn as its `was` line until the drag lets go. */ was?: string; /** * Each kind read by area drawing its density in place of its items (#1393, D25), by its slot: its raster's cells, * each in its heat step by how many rows it holds, under the zones and every link and item. */ density?: ReadonlyMap; /** * Whether the placeholders show (#1398, CL4): a marker — or a footprint — whose colour is its status's while its status * is unread draws in the placeholders' fill once the builder's reveal says so, and is unseen before, its place kept. * Absent, they show. */ placeholders?: boolean; /** * Whether what it draws stands in for a new narrowing while that is read (#1398, the loading design's retained card): * its words — a labelled pin's and a link's caption — in the third ink (`fg.muted`), every mark and edge as it is. * Absent, it draws as itself. */ stale?: boolean; /** * The theme's colour tokens (D14): a dot whose colour token names none of them draws in its tone's colour, and is * stacked by its tone ({@link drawnSeverity}). Absent, every token is the theme's. */ colours?: ReadonlySet; } /** What {@link planMarkers} reads of a frame: what the painter draws the markers not drawn as cards by. */ export type MarkerInput = Pick; /** * How the markers not drawn as cards paint in a frame (#1393, ruled 26 Oct 2026, "Legible dots"), bottom to top: * every dot's paper — an open dot's inside, a filled dot's ring of it — then every dot's colour a layer at a time, by * the severity of the colour each is drawn in, each layer's filled dots then its open rings, each look's dots in the * order they came; then what draws on its own — a labelled pin, the marker a drop lands on — and what is selected * last, over them all. The painter draws by it, and a tap finds the marker drawn on top by it ({@link topMarker}), so * the paint and the hit agree (invariant 9). */ export interface MarkerPlan { /** Every open dot's inside, in the paper. */ readonly paperInsides: DotBatch; /** Every filled dot's ring of paper. */ readonly paperRings: DotBatch; /** Each layer's colours, bottom to top: its filled dots', then its open rings'. */ readonly layers: readonly { readonly fills: DotBatch; readonly rings: DotBatch; }[]; /** What draws on its own after the dots, bottom to top: the pins and a drop's target, then what is selected. */ readonly alone: readonly DrawnItem[]; /** * A marker's colour, as its words: its own as the bridge resolved it; drained as context, the muted ink; its status * unread, the placeholders' fill. * * @param item - The item * @param unread - Whether its status is unread * @returns Its colour */ readonly tintOf: (item: DrawnItem, unread: boolean) => string; } /** * How the markers not drawn as cards paint in a frame (see {@link MarkerPlan}). * * @param input - The frame: the items in view, what each draws as, what is selected, a drop's mark, each entity's * colour, the narrowing's context, the layers' fades, whether the placeholders show, the theme's colour tokens, the * camera and the palette * @returns The plan */ export declare function planMarkers(input: MarkerInput): MarkerPlan; /** * Which of some markers draws on top in a frame (invariant 9): the last of them its plan draws — each layer's * colours, bottom to top, each look's dots in the order they came, then what draws on its own. A marker the plan * draws not at all — its status unread, unseen until the reveal — is none of them. * * @param plan - The frame's plan ({@link planMarkers}) * @param ids - The markers, by their items' ids * @returns The one drawn on top; `undefined` where the plan draws none of them */ export declare function topMarker(plan: MarkerPlan, ids: ReadonlySet): string | undefined; /** * A density raster's cell's heat step (#1393, ruled 23 Oct 2026): one of the * theme's five, by its share of the fullest cell on halving steps — over half * the fullest the fifth, over a quarter the fourth, over an eighth the third, * over a sixteenth the second, the first below — so cells that differ by a * row or two of a uniform block read alike, and a cell the rows fill in part * reads a step or two lighter. * * @param count - The rows the cell holds * @param most - The rows the fullest cell holds * @returns Its step, 1 to 5; `0` for an empty cell */ export declare function densityStep(count: number, most: number): 0 | 1 | 2 | 3 | 4 | 5; /** * Geometry's vertices in world units: each an offset from its entity's anchor * — an item's centre, a zone's box centre (D22) — placed about it. * * @param anchor - The entity's anchor, in world units * @param vertices - The geometry's vertices, offsets from the anchor * @returns The vertices placed in the world */ export declare function placedVertices(anchor: Pt, vertices: readonly V[]): Vert[]; /** A zone's anchor: its box's centre, what its geometry is drawn about. */ export declare function zoneAnchor(zone: { x: number; y: number; width: number; height: number; }): Pt; /** Centre / radius / sweep of a DXF bulge arc, ready for `CanvasRenderingContext2D.arc`. */ export interface BulgeArc { cx: number; cy: number; radius: number; startAngle: number; endAngle: number; anticlockwise: boolean; } /** * Pure geometry of the circular arc a DXF `bulge` encodes for the edge * `p1`→`p2` (`bulge = tan(includedAngle / 4)`; sign = turn direction). Returns * `null` for a straight or degenerate edge (`|bulge|` ~ 0, or a zero-length * chord). Screen space: the caller has already transformed the vertices, and * the uniform camera scale preserves the bulge (it is the tangent of an angle). * Exported so the arc maths is unit-testable without a Canvas2D context. */ export declare function arcFromBulge(p1: Pt, p2: Pt, bulge: number): BulgeArc | null; /** * Where a zone's eyebrow stands, in world units (#1546): on the outline its `geometry` draws, at the outline's point * nearest its box's top-left — of a polygon's edges, the last vertex's bulge curving the edge back to the first; of a * polyline's run, along its middle; on a circle, toward the corner — and for a zone with no geometry, a `rect` or a * shape of no vertices, its box's top-left itself. Its words run to the right of it. * * @param zone - The zone's box, in world units * @param geom - Its drawn shape, if it has one * @returns The point its eyebrow stands at, in world units */ export declare function zoneEyebrowAnchor(zone: { x: number; y: number; width: number; height: number; }, geom: SchematicGeometryValue | undefined): Pt; /** One look's dots: their centres in screen px, as `x, y, x, y, …`, and their items' ids, in the order they came. */ interface DotRun { readonly centres: number[]; readonly ids: string[]; } /** * Dots drawn alike, by their look (#1393): their style, then their line's * width (a fill's is 0), then their alpha, each look's dots in the order they * came. */ type DotBatch = Map>>; /** * The radius of a filled dot's colour, in px: what its ring of paper leaves of it, so a dot drawn on its own is * its colour within its ring as it ever was, whichever is drawn first. */ export declare const DOT_COLOUR_RADIUS: number; /** * How far a dot's drawn shape reaches from its centre, in px (invariant 9): a filled dot's ring of paper half its * width past its radius, an open dot's ring half its own. What a tap lands on the dot by, where dots overlap. * * @param item - The item, drawn as a dot * @returns Its reach */ export declare function dotDrawnRadius(item: DrawnItem): number; /** A status's tone, as an item's status names it: what a dot's colour is stacked by. */ export type StatusTone = Extract["value"]["tone"]["type"]; /** * The severity of the colour a dot is drawn in (ruled 26 Oct 2026, "Legible dots"; D14): its colour token's — the * token's valence, while the theme holds it, as the colour chain draws it — else its tone's, else its status's. A * colour of no valence — a heat step, the brand's, an ink — names none, and stacks with the dots showing no status. * * @param item - The item * @param colours - The theme's colour tokens: one naming none of them draws its tone, and stacks by it; absent, every token is the theme's * @returns The severity; `undefined` for none */ export declare function drawnSeverity(item: DrawnItem, colours?: ReadonlySet): StatusTone | undefined; /** Links hide their mid-path label below this zoom (px per world unit) — * aligned with the item labelled-pin band. */ export declare const LINK_LABEL_MIN_PPU = 16; /** Clamp a hatch `spacing` to a positive, finite minimum so the line sweep * always advances and terminates — a `0` / negative / NaN spacing would * otherwise hard-hang or stall the render thread (issue #57, P3). */ export declare function hatchSpacing(rawSpacing: number): number; /** * The bounded offset range `[oStart, oEnd)` for the diagonal hatch sweep of a * zone whose screen rect is `(x, y, w, h)` on an `width × height` canvas, for a * line family of direction `(dx, dy)` spaced `spacing` apart, with `diag` the * sweep half-length. The full sweep is `-diag … diag`; this narrows it to the * offsets whose line can reach the on-screen portion of the zone, so the step * count is O(visible area / spacing) not O(zone area / spacing) (issue #57, * 1e). `oStart` is snapped onto the SAME `{ -diag + k·spacing }` phase grid the * full sweep uses, so the bounded sweep draws the identical lines — fewer of * them, never phase-shifted. A horizontal family (`dy ≈ 0`, so the line normal * has no x-component) can't be solved this way and falls back to the full * `-diag … diag` range: fully off-screen zones are already culled upstream, so * the only cost case is an on-screen zone far taller than the viewport, which * then pays the full-height sweep (it still terminates — `spacing ≥ 1`; this * degenerate orientation renders as collinear lines and is rarely authored). * * @returns the inclusive-exclusive offset range to sweep */ export declare function hatchSweepBounds(x: number, y: number, w: number, h: number, width: number, height: number, dx: number, dy: number, diag: number, spacing: number): { oStart: number; oEnd: number; }; /** * An upper bound on the number of hatch lines swept across an `w × h` (screen * px) region at `rawSpacing`. Finite for any input because {@link hatchSpacing} * floors the spacing at 1 (issue #57, P3); the painter further bounds the sweep * to the on-screen portion of the zone (1e). * * @param w - region width in screen px * @param h - region height in screen px * @param rawSpacing - the raw (possibly zero/negative) spacing * @returns a finite line-count upper bound */ export declare function hatchLineCount(w: number, h: number, rawSpacing: number): number; /** A caption's text: a link's label and its metric (`uplink · 100G`), either alone, or none. */ export declare function captionText(label: string | undefined, metric: string | undefined): string | undefined; /** Draw the schematic's bulk-shape layer for one frame. Clears first. */ export declare function paintSchematic(input: PaintInput): void; export {}; //# sourceMappingURL=paint.d.ts.map