JavaScript
The JavaScript API loads the WebAssembly module and exposes persistent diagram objects with sites, cells, edges, neighbors, and polygon points.
API overview
Types
Voronoi | Loaded module used to generate diagrams. |
VoronoiWorker | One-shot asynchronous generator that releases its WebAssembly runtime. |
Diagram | Persistent generated result and entry point for topology. |
Site | Retained input point and cell metadata. |
Cell | One site's polygon, edges, and neighbors. |
Edge | Voronoi segment, adjacent sites, and vertex indices. |
Point | Two-dimensional coordinate. |
PointInput | Object points or packed float coordinates accepted by generators. |
GenerateOptions | Diagram bounds or width and height. |
PathContext | Minimal drawing context used by render methods. |
VoronoiModuleOptions | WebAssembly module loading options. |
VoronoiWorkerOptions | Worker script configuration. |
Functions and methods
loadVoronoi(options?) | Load and initialize the WebAssembly module. |
loadVoronoiWorker(options?) | Create an asynchronous worker-backed generator. |
voronoi.generate(points, width, height) | Generate a persistent Diagram. |
voronoi.generate(points, options) | Generate a Diagram using explicit bounds or dimensions. |
workerVoronoi.generate(points, options) | Generate a Diagram in a one-shot worker. |
workerVoronoi.generate(points, width, height) | Generate a worker-backed Diagram using dimensions. |
voronoi.edges(points, width, height) | Return flat Voronoi edge coordinates. |
voronoi.delaunayEdges(points, width, height) | Return flat Delaunay edge coordinates. |
voronoi.delaunayEdges(points, options) | Return flat Delaunay edge coordinates using explicit bounds. |
diagram.site(inputIndex) | Return the retained Site, or null when pruned. |
diagram.cell(inputIndex) | Return the site's Cell, or null when pruned. |
diagram.neighbors(inputIndex) | Return neighboring Site objects. |
diagram.render(context) | Add all Voronoi segments to a path context without creating edge objects. |
diagram.renderDelaunay(context) | Add all Delaunay segments to a path context without creating edge objects. |
diagram.dispose() | Eagerly release the JavaScript-owned result buffer. |
point.toJSON() | Return a plain { x, y } object. |
Load the module
import { loadVoronoi } from "./voronoi.js";
const voronoi = await loadVoronoi();Module options
loadVoronoi(options) accepts VoronoiModuleOptions:
| Member | Type | Purpose |
|---|---|---|
locateFile | (path, scriptDirectory) => string | Override the URL used to load jc_voronoi.wasm or another module file |
wasmBinary | Uint8Array | Provide the WebAssembly binary directly |
print | (...args) => void | Override standard runtime output |
printErr | (...args) => void | Override runtime error output |
Additional Emscripten module options are passed through unchanged.
Input and option types
PointInput is either an array of ordinary { x, y } objects or a flat
Float32Array containing x, y coordinate pairs. Input objects do not need to
implement the iterator exposed by generated Point objects.
GenerateOptions accepts one of these forms:
{ bounds: [minX, minY, maxX, maxY] }
{ width: 100, height: 100 }PathContext is the minimal drawing interface used by render() and
renderDelaunay():
interface PathContext {
moveTo(x: number, y: number): void;
lineTo(x: number, y: number): void;
}Voronoi
Generate a persistent diagram using either a width and height or explicit bounds:
const diagram = voronoi.generate(points, 100, 100);
const boundedDiagram = voronoi.generate(points, {
bounds: [minX, minY, maxX, maxY],
});points may be an array of { x, y } objects or a flat Float32Array. Generation performs one bulk copy from WebAssembly into a compact JavaScript-owned ArrayBuffer; subsequent access does not call into WebAssembly.
Worker-backed generation
Use the asynchronous generator for large, one-off diagrams when retained memory matters more than worker startup latency:
import { loadVoronoiWorker } from "./voronoi.js";
const voronoi = await loadVoronoiWorker();
const diagram = await voronoi.generate(points, {
bounds: [minX, minY, maxX, maxY],
});loadVoronoiWorker({ workerUrl }) accepts a string or URL overriding the
default voronoi.worker.js module URL.
Each call starts a module worker, generates and transfers the packed result, and
terminates the worker before resolving. The returned Diagram has the same API
as a synchronous result, but its WebAssembly heap is no longer retained. Input
Float32Array values are copied before transfer and are never detached.
Diagram
| Member | Result |
|---|---|
bounds | [minX, minY, maxX, maxY] |
inputCount | Number of input points |
byteLength | Exact size of the packed result buffer |
numSites | Number of retained sites |
numVertices | Number of unique vertices |
numEdges | Number of Voronoi edges |
numDelaunayEdges | Number of Delaunay adjacency edges |
sites | Retained Site objects |
edges | All Voronoi Edge objects |
site(inputIndex) | Input-order Site, or null when pruned |
cell(inputIndex) | Input-order Cell, or null when pruned |
neighbors(inputIndex) | Neighboring Site objects |
render(context) | Adds Voronoi segments through moveTo and lineTo |
renderDelaunay(context) | Adds Delaunay segments through moveTo and lineTo |
dispose() | Eagerly releases the result buffer and invalidates the diagram |
The result buffer is garbage-collected normally; dispose() is optional.
Site
| Member | Result |
|---|---|
p | Site position as a Point |
index | Original input index |
boundary | Whether the site touches the clipping boundary |
cell | The site’s Cell |
Cell
| Member | Result |
|---|---|
site | The cell’s Site |
edges | Counter-clockwise cell edges |
neighbors | Adjacent sites |
polygon | Closed polygon of Point objects |
Edge
| Member | Result |
|---|---|
sites | Two adjacent sites; boundary sides may be null |
pos | Two endpoint Point objects |
vertices | Two unique vertex indices |
Point
Point exposes .x and .y, can be destructured as [x, y], and provides
toJSON() to create a plain { x, y } object.
Compatibility helpers
voronoi.edges(points, width, height) and voronoi.delaunayEdges(points, width, height) return flat Float32Array coordinate pairs for applications that need the earlier bulk-output API. delaunayEdges also accepts { bounds: [minX, minY, maxX, maxY] } or { width, height }.
See Examples - JS for a complete example.