Skip to content

API reference

import {
Graphraum,
defineVisuals,
graphraumTheme,
type GraphraumData,
type GraphraumDataPatch,
type GraphraumDiagnostics,
type GraphraumLayoutPositions,
type GraphraumNodeUpdate,
type GraphraumOptions,
} from "@yggy-tech/graphraum";
new Graphraum(container: HTMLElement, options?: GraphraumOptions)

The container must have a non-zero width and height. graphraum appends one canvas, observes container resizing, creates the selected camera, and renders on demand.

interface GraphraumOptions {
mode?: "2d" | "3d"; // default: "2d"
antialias?: boolean; // default: false
depthOfField?: GraphraumDepthOfFieldOptions; // default: off
interactionQuality?: GraphraumInteractionQualityOptions; // default: off
farLook?: GraphraumFarLookOptions | false; // default: on in 2D
maxPixelRatio?: number; // default: 2
maxVisibleEdges?: number; // default: 100_000
maxVisibleNodes?: number; // default: 100_000
viewportCulling?: boolean; // default: true
viewportOverscan?: number; // default: 16 world units
theme?: Partial<GraphraumTheme>;
visuals?: GraphraumVisualMapper<NodeAttributes, EdgeAttributes>;
}

Invalid node and edge budgets, overscan values, positions, and node sizes fail at the boundary with an actionable error.

interface GraphraumData<NodeAttributes = undefined, EdgeAttributes = undefined> {
nodes: readonly GraphraumNode<NodeAttributes>[];
edges: readonly GraphraumEdge<EdgeAttributes>[];
}
interface GraphraumDataPatch<NodeAttributes = undefined, EdgeAttributes = undefined> {
addedNodes?: readonly GraphraumNode<NodeAttributes>[];
addedEdges?: readonly GraphraumEdge<EdgeAttributes>[];
removedNodeIds?: readonly string[];
removedEdgeIds?: readonly string[];
}
interface GraphraumNode {
id: string;
position: { x: number; y: number; z?: number };
color?: string | number;
shape?: GraphraumNodeShape;
size?: number; // shorthand; width/height default to size (then 4)
width?: number;
height?: number;
strokeWidth?: number; // world units; 0 by default (no stroke)
strokeColor?: string | number; // defaults to theme.nodeStroke when strokeWidth > 0
glow?: number; // 0..1, opt-in halo; 0 by default (no halo)
}
type GraphraumNodeShape =
| "circle"
| "square"
| "diamond"
| "hexagon"
| "triangle"
| "pill"
| "rounded";
interface GraphraumEdge {
id: string;
source: string;
target: string;
color?: string | number;
width?: number;
opacity?: number;
style?: "solid" | "dashed" | "dotted";
marker?: "none" | "triangle";
markerSize?: number;
markerEnd?: "target" | "source" | "both";
path?: "straight" | "quadratic" | "cubic";
controlPoints?: readonly { x: number; y: number; z?: number }[];
/** 0..1 evidence weight; read only by `flow.weightBy: "evidence"`. */
weight?: number;
}

Node and edge IDs must be unique, every edge endpoint must exist, coordinates must be finite, and node size, width, and height must be positive when present. strokeWidth must be finite and non-negative. glow must be a finite number between 0 and 1. An unknown shape fails with the node ID in the error. Edge width and markerSize must be positive, and opacity must be between 0 and 1. Positions and sizes use world units.

Supply generic attribute types and defineVisuals() to compile domain data into node color, shape, size (or independent width/height), stroke, glow, edge color, width, opacity, style, markers, and path, and immutable presentation metadata. See Data and visuals for the complete contract.

interface GraphraumEdgeVisual {
color?: string | number;
width?: number;
opacity?: number;
style?: "solid" | "dashed" | "dotted";
marker?: "none" | "triangle";
markerSize?: number;
markerEnd?: "target" | "source" | "both";
path?: "straight" | "quadratic" | "cubic";
controlPoints?: readonly { x: number; y: number; z?: number }[];
}

defineVisuals() maps typed edge attributes to this contract; direct edge fields use the same shape without a mapper. style selects a dash pattern, marker draws a triangle at markerEnd (target by default), and markerSize scales the triangle relative to width. path selects a polyline sampling of a straight, quadratic, or cubic curve; omit controlPoints for auto handles, or supply 1 (quadratic) / 2 (cubic) world-space points. Overview LOD collapses curves to a single straight segment. Endpoints always come from the source and target node positions. See Data and visuals for the full contract.

theme.endpointAttach ("boundary" by default) stops each edge at its node’s outline. trimEdgeEndpoints() and attachPointOnOutline() export the same trim; see Camera and 3D.

setData(data, options?)

Validates and replaces the complete topology, rebuilds canonical buffers, fits the camera, materializes the viewport, and schedules a render. Pass { fit: false } to keep the camera where it is, for example when a host sends a full snapshot on every change. On a graph that never had a fit the camera keeps its initial pose, so call fitView() once after the first data.

applyDataPatch(patch)

Merges streamed additions and removals without fitting the camera. Changes that stay within the preallocated capacity reuse existing GPU meshes; capacity growth falls back to one controlled rebuild. A patch that only adds nodes and edges costs time in proportion to the patch in 3D and in 2D with viewportCulling: false, because only the new instances are written. In 2D with viewport culling, in the density or overview tier, and for removals, the visible set is repacked.

updateNodes(updates)

Changes position, size, shape, or color for existing nodes and updates only the affected instance data and incident edge endpoints. Use setData when topology changes.

setIconAtlas(atlas, options?)

Draws a tinted icon inside every node that has an icon index, from an atlas built with createIconAtlas in @yggy-tech/graphraum/icons. Same pass, no extra draw call. null removes icons. See Icons.

applyLayout(layout)

Applies a transferable XYZ position batch for existing nodes. Call it repeatedly as a worker produces progressive positions.

setSelection(nodeIds)

Applies the selected-node theme color without mutating source graph data. Missing and off-screen IDs are safe.

setEdgeSelection(edgeIds)

Highlights edges with theme.selectedEdge. Only the edges whose selection changed are repainted, so per-hover calls stay cheap on large graphs. Unknown IDs are ignored.

setEdgeState(“dimmed”, edgeIds)

Recedes edges with theme.dimmedEdge and theme.dimmedEdgeOpacity, mirroring setNodeState(“dimmed”, ids). A selected edge stays selected. Unknown IDs are ignored and removed edges drop out of the state.

setEdgeColors(colors)

Paints edges in host colours from a map or entries of edge ID to colour, for example one colour per relation around a focused node. Each call replaces the previous set; an empty map clears it. Selection and dimming still win. It repaints in place without a recompile. Unknown IDs are ignored and removed edges drop out.

getNodePresentation(id)

Returns the compiled title, subtitle, properties, and action descriptors for a node, or null.

getEdgePresentation(id)

Returns the compiled presentation for an edge, or null.

render()

Schedules one render on the next animation frame. Repeated calls in the same frame are coalesced.

const updates: readonly GraphraumNodeUpdate[] = [
{ id: "person:ada", position: { x: 20, y: 12 } },
{ id: "place:london", color: "#73c7a5", shape: "square", size: 6 },
];
graph.updateNodes(updates);

GraphraumLayoutPositions carries node IDs and a transferable XYZ Float32Array. See Layout.

computeForcePositions(), computeClusteredForcePositions() and createForceSimulation() accept ForceSettings (defaults in DEFAULT_FORCE_SETTINGS). See Layout for settings, live simulations and clusters.

Use bindGraphology() when Graphology owns the mutable graph. The adapter renders the current graph immediately, maps node visual attributes (x, y, z, color, shape, and size), preserves all attributes for defineVisuals(), and subscribes to later mutations.

import Graph from "graphology";
import { Graphraum, bindGraphology } from "@yggy-tech/graphraum";
type NodeAttributes = {
kind: "person" | "place";
x?: number;
y?: number;
};
type EdgeAttributes = Record<string, never>;
const source = new Graph<NodeAttributes, EdgeAttributes>();
source.addNode("person:ada", { kind: "person" });
source.addNode("place:london", { kind: "place", x: 20, y: 12 });
source.addEdgeWithKey("born-in", "person:ada", "place:london");
const container = document.querySelector<HTMLElement>("#graph");
if (!container) throw new Error("Missing #graph container");
const graph = new Graphraum<NodeAttributes, EdgeAttributes>(container);
const unbind = bindGraphology(graph, source);
source.mergeNodeAttributes("person:ada", { x: -20, y: 12 });
// Stop Graphology mutations from reaching this renderer.
unbind();
graph.destroy();

Missing coordinates default to zero so layout does not block the first render. Mutations are coalesced into one snapshot replacement per microtask, which reruns defineVisuals() for changed domain attributes. The source graph and renderer remain application-owned; unbind() only removes the adapter’s listeners.

pick(clientX, clientY)

Returns the nearest node ID or null. Uses the spatial grid in 2D and instance raycasting in 3D.

setMode(mode)

Switches between orthographic 2D and perspective 3D, replaces controls, and fits the current graph.

getMode()

Returns the active “2d” or “3d” mode.

setAutoOrbit(options | false)

Slowly orbits the 3D camera around the graph. Off by default; pass false to stop. Throws in 2D, and switching to 2D turns it off.

setNodeDrag({ enabled })

Turns node dragging on or off (off by default). A drag moves one node and its edges follow; the camera controls are paused for it. Throws unless enabled is a boolean. See Drag nodes.

onNodeDrag(listener)

Subscribes to drag steps: { phase, nodeId, position } with phase “start”, “move”, “end” or “cancel”. Returns an unsubscribe function.

setDepthOfField(options, update?)

Turns 3D depth of field on or off and changes its lens and transition. Omitted fields keep their values. Fades to the new strength and resolves true when it arrives or false when interrupted; { animate: false } jumps. Off by default; depth mode is a no-op in 2D, mode: “radial” works in 2D and 3D. See Depth of field.

setFocusLens(options)

Turns the focus lens on or off and changes it: nodes near the center of the canvas draw larger and nodes toward its edge draw smaller, in 2D and 3D. Omitted fields keep their values. Off by default; invalid values throw. getFocusLens() returns the options in effect and getFocusLensLabelAlpha(nodeId) the label opacity for a node. See Focus lens.

setFlow(options | false)

Turns the edge flow on or off and changes it: small dots travel along the edges of the hovered or selected node, following each drawn edge path in 2D and 3D. Options: on (“off” by default, or “hover”, “select”, “both”), depth (1 or 2), direction (“relation”, “outward”, “inward”), speed, density, dotSize, weightBy and maxEdges. Omitted fields keep their values; false turns it off; invalid values throw and change nothing. getFlow() returns the options in effect. One extra draw call, only while dots exist. See Edge flow.

setFarLook(options)

Tunes the 2D far look, or turns it off with false: strength (default 0.2), nearPixels (default 10) and farPixels (default 3). It eases in as nodes shrink on screen and is off up close. Dark backgrounds get a soft additive glow per node; light ones get a soft multiply halo, colours muted about 30 percent and thin grey edges. It is one extra draw call, and only while a node is small enough. Omitted fields keep their values; invalid values throw.

getWorldUnitsPerPixel()

World units one CSS pixel covers in 2D, with wheel zoom and fitView both applied. Culling, the density tier, edge tiers, picking and the far look all read this one scale; use it for your own labels too. In 3D it is only a rough guide.

setInteractionQuality(options)

Turns lower quality while the camera moves on or off and tunes it: enabled (default false), targetFrameMs (default 25) and idleMs (default 200). Omitted fields keep their values; invalid values throw. While frames run slow it lowers the pixel ratio and the visible edge budget (and in 2D the node budget); at rest it restores full quality in one frame. See Keep the camera smooth.

setScene(options)

Changes the 3D scene: edgeDepth, shading (“flat”, “lit”, “toon”), sun and ambient. Omitted fields keep their values; graphraumScenes.lit sets a whole look. A no-op in 2D and no extra draw calls. getScene() returns the options in effect. See Lit and toon shading.

setTheme(theme)

Applies a named preset (“dark” / “light”) or merges a partial palette, then rematerializes colors.

setStyle(style)

Merges a look beyond colours onto the current one: theme colours, node.strokeWidth, edge.markerSize and label typography. Omitted fields keep their values; a preset sets them all. Invalid values throw and change nothing. See Styling.

setBackground(background)

Sets a solid color, “transparent” / null for host CSS, or a { type: "pattern", source } texture.

fitView(options?)

Fits the active camera to the complete graph and rematerializes the viewport. Pass { nodeIds } to frame only those nodes, for example a highlighted path; unknown IDs are ignored, and when none match (or the list is empty) it frames every node. In 2D it frames every node and resets any wheel zoom. In 3D it returns to the front view and moves as close as it can while every node, including its glow halo, stays on the canvas, leaving a small margin on the tighter canvas axis for labels beside edge nodes.

flyTo(options)

Animates the camera to the fitView({ nodeIds }) framing over durationMs (default 1200) and resolves true on arrival or false when cancelled by canvas input, another flight, fitView, setMode or destroy(). In 3D it keeps the current view direction, so an auto-orbit continues around the new target; distanceScale multiplies the fitted distance. Jumps with durationMs: 0 or reduced motion. See Fly to nodes.

getCameraState()

Returns the camera pose as plain JSON: in 2D the centre and the world height the canvas shows, in 3D the camera position and the point it orbits. Save it to return to the same view later.

setCameraState(state)

Puts the camera on a pose from getCameraState() at once, without fitting or animating, and cancels a running flyTo. Throws for a state of the other mode or with a non-finite number. See Save and restore the view.

resize()

Synchronizes the renderer and active camera immediately. A ResizeObserver already calls this for normal container changes.

destroy()

Disconnects observation and controls, cancels pending rendering, disposes GPU resources, and removes the canvas.

createOverlay(options)

Adds a bounded DOM layer for labels, a toolbar, and optional rich HTML focus cards. See Labels and focus.

setAutoOrbit({ speed, resumeAfterMs }) orbits the 3D camera; see Camera and 3D.

interface GraphraumDepthOfFieldOptions {
enabled?: boolean; // default: false
strength?: number; // aperture 0..1, default: 0.5; blur = strength × 24 × |d − f| / d CSS px
nearStrength?: number; // aperture 0..1 in front of the focus, default: strength
maxBlur?: number; // default: 12 CSS px
minBlur?: number; // default: 0; smaller radii render sharp
nearDistance?: number; // default: 0 world units; full strength up to this camera-to-focus distance
farDistance?: number; // default: Infinity; blur faded out from this distance
focus?: "selection" | number; // default: "selection"; a number is a camera distance in world units
transition?: {
durationMs?: number; // default: 400; 0 jumps
easing?: "linear" | "easeIn" | "easeOut" | "easeInOut" | ((t: number) => number); // default: "easeInOut"
};
}
setDepthOfField(options: GraphraumDepthOfFieldOptions, update?: { animate?: boolean }): Promise<boolean>;

Pass it as depthOfField to the constructor or to setDepthOfField. Invalid values throw. See Camera and 3D.

interface GraphraumSceneOptions {
edgeDepth?: "behind-nodes" | "scene"; // default: "behind-nodes"; "scene" lets edges pass in front of nodes in 3D
shading?: "flat" | "lit" | "toon"; // default: "flat"; 3D only
sun?: {
azimuth?: number; // degrees around the vertical axis, default: 220
elevation?: number; // degrees above the horizon, 10..85, default: 55
color?: GraphraumColor; // default: "#ffffff"
intensity?: number; // 0..4, default: 0.6
};
ambient?: {
sky?: GraphraumColor; // light from above, default: "#ffffff"
ground?: GraphraumColor; // light from below, default: "#a3a8b3"
intensity?: number; // 0..4, default: 0.55
};
}
setScene(options: GraphraumSceneOptions): void;
getScene(): ResolvedGraphraumSceneOptions;
graphraumScenes.flat | graphraumScenes.lit | graphraumScenes.toon; // style + edgeDepth "scene"

Pass it as scene to the constructor or to setScene. sun and ambient merge field by field. Invalid values throw with the option path, for example scene.sun.elevation must be 10..85, got 120.

graphraumThemes.dark / graphraumThemes.light (and the graphraumTheme dark alias) are immutable. Constructor options and setTheme copy values per renderer.

const graph = new Graphraum(container, {
theme: "light", // or { ...graphraumTheme, edgeWidth: 2 }
});
graph.setTheme("dark");
graph.setBackground("transparent");
graph.setBackground({ type: "pattern", source: image, repeat: [2, 2] });

background accepts a CSS color, "transparent" / null (clear canvas for host CSS), or a pattern object as shown in the example above.

See Theme for every token and the canonical palette.

const diagnostics: GraphraumDiagnostics = graph.getDiagnostics();
interface GraphraumDiagnostics {
aggregatedNodeClusters: number;
autoOrbit: "active" | "off" | "paused";
cpuFrameMilliseconds: number;
depthOfField: "active" | "inactive" | "off" | "unsupported";
gpuFrameMilliseconds: number | null;
gpuDrawCalls: number;
gpuGeometries: number;
gpuTextures: number;
interactionQuality: "full" | "minimal" | "reduced";
lodLevel: "density" | "detail" | "overview";
pickingStrategy: "raycaster-3d" | "spatial-grid-2d";
dimmedEdges: number;
flowDots: number;
flowEdges: number;
selectedEdges: number;
selectedNodes: number;
subPixelEdgesSkipped: number;
totalEdges: number;
totalNodes: number;
visibleEdgeCandidates: number;
visibleEdgeMarkers: number;
visibleEdgeSegments: number;
visibleEdges: number;
visibleGlowNodes: number;
visibleNodes: number;
visibleNodeCandidates: number;
}

Diagnostics describe the final rendered frame. GPU frame time is null when WebGL timer queries are unavailable. Node candidates and aggregate clusters expose density LOD decisions; total and visible counts expose the effective node and edge reductions without asking applications to re-derive them. visibleEdgeSegments counts rendered edge lines and visibleEdges is an alias for it; visibleEdgeMarkers counts rendered direction triangles, which the overview LOD tier drops first. subPixelEdgesSkipped counts overview edges left out in 2D because they cover under 0.75 CSS pixels; it is 0 in 3D and while edges fit the maxVisibleEdges budget. interactionQuality is "full" unless the option is on and the camera is moving slowly; see Keep the camera smooth. autoOrbit is "paused" when the orbit is enabled but stopped by input, a hidden tab or an off-screen canvas. visibleGlowNodes counts rendered glow halos (at most 256, and 0 in the density LOD tier). gpuDrawCalls is 2 without halos and 3 while at least one halo is visible; see Glow. flowDots and flowEdges count the dots and edges of the edge flow in the last frame, and both are 0 (with no draw call) when it is off or nothing is in focus; a running flow adds 1 draw call, see Edge flow. A frame that runs depth of field adds 4 draw calls. depthOfField is "off" when disabled, "inactive" when enabled with nothing to blur (2D, or nothing selected), "active" when the last frame blurred and "unsupported" when the WebGL context cannot run it.