Skip to main content

Overlays

Overlays are an optional SVG layer that sits above nodes and edges. Use them for signals, annotations, selection indicators, grid labels, and other transient visual elements.

Mental model

An overlay has two halves:

  • Authoring: use builder.overlay((o) => ...) to describe what overlays exist and with what params
  • Rendering: the overlay registry turns each spec into SVG (built-ins ship pre-registered; custom visuals need a registered renderer)

Built-in primitive overlays

Three primitives work out of the box without a custom renderer: rect, circle, and text.

Signal overlay

The built-in signal overlay draws a moving marker between two nodes:

Combining multiple overlays

Use the callback form to compose several overlays in one call:

Built-in overlay kinds

These ship in the default registry and can be used immediately:

KindDescription
signalMoving marker between two nodes
grid-labelsAxis/grid labels for the grid system
data-pointsPoints attached to nodes
rectPrimitive rectangle (via .rect() helper)
circlePrimitive circle (via .circle() helper)
textPrimitive text label (via .text() helper)

Keys

Overlays reconcile by unique key:

  • If spec.key is present, that's used
  • Otherwise, spec.id is used

If you render multiple overlays with the same id, always provide a key. The callback builder auto-generates stable keys for unkeyed overlays of the same id.

Animating overlays

Overlay params can be targeted by the data-only timeline animation system. Give the overlay instance a stable key and target it in animations with aBuilder.overlay('key'):

Group overlays

Move multiple overlay elements as one unit using a group overlay:

Registering custom overlay kinds

When built-in kinds aren't enough, register a new renderer:

import { defaultCoreOverlayRegistry } from 'vizcraft';

defaultCoreOverlayRegistry.register('selection', {
render: ({ spec }) => {
const { x, y, w, h } = spec.params;
return `<rect x="${x}" y="${y}" width="${w}" height="${h}" rx="8" class="viz-selection" />`;
},
update: ({ spec }, g) => {
let rect = g.querySelector('rect');
if (!rect) {
rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
g.appendChild(rect);
}
rect.setAttribute('x', String(spec.params.x));
rect.setAttribute('y', String(spec.params.y));
rect.setAttribute('width', String(spec.params.w));
rect.setAttribute('height', String(spec.params.h));
rect.setAttribute('rx', '8');
rect.setAttribute('class', spec.className ?? 'viz-selection');
},
});

// Use it
builder.overlay('selection', { x: 10, y: 10, w: 200, h: 120 }, 'sel');
note

The default registry is a singleton. Register custom overlays once (idempotently).

For the complete overlay builder API, see the Overlay API Reference.


Found a problem? Open an issue on GitHub.