Views Canvas module
// Canvas module

Canvas module

@j_shelfwood/talos-ui/canvas: a pannable, zoomable node, edge and lane graph with a sticky time axis, minimap and legend on the .talos-graph chrome.

@j_shelfwood/talos-ui/canvas draws a graph on the graph canvas chrome. Where .talos-graph alone is the frame, the canvas module fills it: an infinite plane of boxes (nodes), the edges between them, bands (lanes) and a time axis, laid out in world coordinates you supply. It knows boxes, not what they are: your renderNode returns each node’s inner HTML, and the canvas positions, culls, selects and traces them.

  • Pan and zoom: drag or two-finger scroll pans; pinch or Ctrl/⌘ + scroll zooms; double-click zooms in. Keys on the focused canvas: arrows pan, + and - zoom, 0 fits everything, f fits the selection.
  • Selection: a click on a node selects it, lights its edges and neighbours and dims the rest (.is-tracing); a click on the empty plane clears it.
  • Time axis: sticky at the top (or the left edge when orientation is "vertical"), ticks from the scene’s time.
  • Lanes: bands across the world, with heads sticky at the left.
  • Minimap: the whole world in a corner; drag it to pan, scroll it to zoom, fold it with its toggle.
  • Culling: only the boxes inside the viewport are in the DOM, so thousands of nodes stay cheap.

Pan, zoom and the minimap run on @xyflow/system ^1, an optional peer dependency. The canvas entry imports it, so install it next to the package when you use this module:

bun add @j_shelfwood/talos-ui @xyflow/system@^1

The styles are the canvas section of talos-graph.css (in all.css).

Live demo

Four lanes over a day of work, on a time axis that collapses the quiet night into a hatched break. Drag to pan, pinch or Ctrl/⌘ + scroll to zoom, click a node to trace its edges.

LIVE
import { createCanvas, edgeLegend, timeScale, type CanvasNode } from "@j_shelfwood/talos-ui/canvas";

type Item = CanvasNode & { title: string };
const H = 3_600_000, since = Date.UTC(2026, 10, 19, 6), until = since + 30 * H;
const scale = timeScale([since + H, since + 2 * H, since + 20 * H], since, until, {
  origin: 20, pxPerMs: 160 / H, before: H / 2, after: H, minGap: 4 * H, breakPx: 28,
});
const nodes: Item[] = [
  { id: "a", x: scale.pos(since + H), y: 22, w: 140, h: 46, title: "fetch feed" },
  { id: "b", x: scale.pos(since + 2 * H), y: 112, w: 140, h: 46, title: "compile", tone: "info" },
];

const canvas = createCanvas<Item>(document.getElementById("graph")!, {
  scene: {
    nodes,
    edges: [{ id: "ab", from: "a", to: "b", kind: "causes" }],
    lanes: [{ key: "ingest", at: 0, size: 90 }, { key: "build", at: 90, size: 90 }],
    time: { tOf: scale.time, pOf: scale.pos, pxPerMs: scale.pxPerMs, breaks: scale.breaks },
  },
  renderNode: (n) => `<div class="my-node">${n.title}</div>`,
  timeAxis: { tz: "Europe/Amsterdam", locale: "en-GB" },
  legend: [{ title: "Edges", entries: edgeLegend() }],
  onSelect: (id) => console.log("selected", id),
});

canvas.update(nextScene);   // replace the scene
canvas.fit(["a", "b"]);     // fit these nodes, or everything with no ids
canvas.destroy();           // remove listeners and observers

The container needs a height; the canvas fills it. The root gets the classes talos-graph talos-graph--canvas talos-graph--horizontal (or --vertical), plus talos-graph--timed when the scene has a time.

createCanvas

createCanvas(el, options) builds a Canvas on el, wires the callbacks, sets the scene and, without a viewport, fits the scene once the element has a size. It returns a handle.

OptionTypeMeaning
sceneCanvasScenenodes, edges, lanes, and optional width, height, clusters, time
renderNode(n, level, selected) => stringa node’s inner HTML (required); the canvas sizes and places the box
renderLane(lane) => stringa lane head’s HTML (default: the lane’s head, else its escaped key)
renderCluster(c, level) => stringHTML of a cluster box, drawn instead of nodes at clusterLevels
levels(zoom) => Lsemantic zoom: the level name at a zoom, passed to the render functions
clusterLevelsL[]the levels that draw clusters instead of nodes
edgeBox(n, level) => Boxthe box an edge attaches to at a level (default: the node’s own box)
edgeStylesRecord<string, EdgeStyle>the look per edge kind (default EDGE_STYLES)
timeAxis{ tz?, locale? }timezone and locale of the axis labels (default: the runtime’s)
legendLegendGroup[]a collapsible key in the bottom-left corner
minZoom, maxZoomnumberzoom bounds (default 0.05 and 2.5)
fitFloornumberthe lowest zoom a selection fit goes to
marginnumberculling margin in screen px
minimapbooleanfalse removes the minimap
orientation"horizontal" | "vertical"time runs across (lanes are rows) or down (lanes are columns)
viewport{ x, y, zoom }the starting view; without it the scene is fitted
onSelect, onHover, onViewportChange, onLanecallbacksselection, hover, pan and zoom, and a click on a lane head

The handle has update(scene), setViewport(v, duration?), fit(ids?), select(id), viewport(), destroy(), and canvas, the underlying Canvas instance (for fitNodes, centreX and on).

Scene shapes

TypeShape
CanvasNode{ id, x, y, w, h, tone? }; tone colours the node in the minimap (info, success, warning, danger, accent)
CanvasEdge{ id, from, to, kind, badge?, title?, quiet?, channel? }; quiet edges show only while an end is hovered or selected
LaneBand{ key, at, size, head? }; at and size in world px along the cross axis
AxisTime{ tOf, pOf?, pxPerMs?, breaks? }; tOf maps a world position to a time

Edge kinds in EDGE_STYLES: causes, replies, part_of, about, waits_on, mentions and linked (the fallback for an unknown kind). edgeLegend() turns them into legend entries.

Time helpers

The time functions are pure and exported for any other ruler, such as a timeline or a scrubber.

ticks(range, pxPerMs, { tz, locale, minPx }) returns { step, scale, ticks } for a time range at a zoom: the smallest step whose ticks sit at least minPx apart (default 64), with scale one of "minute", "hour", "day" or "week". Each tick is { t, label, major }; local midnights are major, and steps of an hour or longer align to local time across a DST change. Labels read “Thu 19-11” for a day and “14:05” for a clock in every locale; only the weekday name follows locale.

timeScale(times, since, until, options) builds a world axis that collapses quiet stretches. Each time holds before and after ms around it; held spans closer than minGap merge; every gap between them becomes a break of breakPx. It returns { pos, time, breaks, end, pxPerMs }, where pos and time are monotonic inverses; pass them as the scene’s time to draw the breaks as hatched strips.

Also exported: dayLabel, clock, dayStamp, duration, tzOffset, localMidnight, TICK_STEPS, and the string builders edgePath, edgeSvg, bandsHtml, laneHeadsHtml, legendHtml and timeAxisHtml.