Version v26.11.26 Size 5.0 KB gzip Dependencies Zero dependencies
Why Wire?
Node editors always rediscover the same hard parts: wires that route around nodes instead of through them, bend counts that change every frame, and ordering or cycle checks that recurse until the stack dies on a big graph. Wire computes all of it as pure functions: cubic bezier geometry anchored to port sides, A* Manhattan routes over a padded-obstacle visibility graph with a per-bend penalty, input-order-stable topological sorting, and Tarjan SCC cycle detection — iterative, so 50,000-node chains are fine.
// Before
// Straight lines through nodes, hand-rolled A* with nondeterministic tie
// breaks, and recursive DFS that stack-overflows on deep graphs.
// After
import { bezierRoute, findCyclicGroups, orthogonalRoute, topologicalOrder } from '@vielzeug/wire';
const curve = bezierRoute({ side: 'right', x: 100, y: 40 }, { side: 'left', x: 320, y: 120 });
// Pass every node bound; the router exempts each port's own node for its exit.
const waypoints = orthogonalRoute(fromPort, toPort, { obstacles: nodeBounds });
// waypoints is null when every route is blocked — render a dashed fallback.| Feature | Wire | Ad-hoc routing | Graph libraries |
|---|---|---|---|
| Bundle size | 5.0 KB | n/a | Typically 10–80 kB |
| External dependencies | n/a | Varies | |
| Obstacle-avoiding orthogonal | Built in (A* + bend penalty) | Rare | Sometimes |
| Deterministic output | Rare | Varies | |
| Iterative cycle detection | Recursive DFS | Varies | |
| Headless: no DOM or renderer | Varies | Usually coupled |
Use Wire when you own the node layout and rendering but want production-grade edge geometry and graph ordering as pure functions.
Consider a graph-drawing library when you also want automatic layout (dagre-style ranking, force simulation) — Wire routes the edges you place, it does not place the nodes.
Installation
pnpm add @vielzeug/wirenpm install @vielzeug/wireyarn add @vielzeug/wireQuick Start
import { bezierRoute, bezierToSvgPath, findCyclicGroups, orthogonalRoute, topologicalOrder } from '@vielzeug/wire';
// Curved wire from a right-facing port to a left-facing port.
const curve = bezierRoute({ side: 'right', x: 100, y: 40 }, { side: 'left', x: 320, y: 120 });
const d = bezierToSvgPath(curve); // or draw curve.c1/curve.c2 on a canvas
// Manhattan wire that avoids the node boxes.
const points = orthogonalRoute(
{ side: 'right', x: 100, y: 40 },
{ side: 'left', x: 320, y: 120 },
{ obstacles: nodeBounds },
);
// Execution order and cycles for a validation pass.
const order = topologicalOrder(
['b', 'a', 'c'],
[
{ from: 'a', to: 'b' },
{ from: 'b', to: 'c' },
],
);
const groups = findCyclicGroups(
['a', 'b'],
[
{ from: 'a', to: 'b' },
{ from: 'b', to: 'a' },
],
); // [['a', 'b']]Features
bezierRoute(): cubic bezier geometry with control points anchored to port sidesorthogonalRoute(): A* Manhattan polyline over a visibility graph built from obstacle corners, with a per-bend penalty, owner-obstacle exemption, and deterministic expansion orderbezierToSvgPath()/polylineToSvgPath(): SVGdserialization with micro-precision formattingtopologicalOrder(): Kahn ordering with stable input-order tie breaks; throwsWireGraphErroron cyclesfindCyclicGroups(): iterative Tarjan SCCs — each cyclic group reported once, ordered deterministicallyWireError/WireGraphError/WireConfigError: typed errors- Zero runtime dependencies, no DOM requirement: routes and orders are pure computations
Documentation
See Also
- Atlas: the camera and rect shapes Wire's obstacles reuse.