Skip to content

JavaScript

The JavaScript API loads the WebAssembly module and exposes persistent diagram objects with sites, cells, edges, neighbors, and polygon points.

API overview

Types

VoronoiLoaded module used to generate diagrams.
VoronoiWorkerOne-shot asynchronous generator that releases its WebAssembly runtime.
DiagramPersistent generated result and entry point for topology.
SiteRetained input point and cell metadata.
CellOne site's polygon, edges, and neighbors.
EdgeVoronoi segment, adjacent sites, and vertex indices.
PointTwo-dimensional coordinate.
PointInputObject points or packed float coordinates accepted by generators.
GenerateOptionsDiagram bounds or width and height.
PathContextMinimal drawing context used by render methods.
VoronoiModuleOptionsWebAssembly module loading options.
VoronoiWorkerOptionsWorker 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:

MemberTypePurpose
locateFile(path, scriptDirectory) => stringOverride the URL used to load jc_voronoi.wasm or another module file
wasmBinaryUint8ArrayProvide the WebAssembly binary directly
print(...args) => voidOverride standard runtime output
printErr(...args) => voidOverride 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

MemberResult
bounds[minX, minY, maxX, maxY]
inputCountNumber of input points
byteLengthExact size of the packed result buffer
numSitesNumber of retained sites
numVerticesNumber of unique vertices
numEdgesNumber of Voronoi edges
numDelaunayEdgesNumber of Delaunay adjacency edges
sitesRetained Site objects
edgesAll 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

MemberResult
pSite position as a Point
indexOriginal input index
boundaryWhether the site touches the clipping boundary
cellThe site’s Cell

Cell

MemberResult
siteThe cell’s Site
edgesCounter-clockwise cell edges
neighborsAdjacent sites
polygonClosed polygon of Point objects

Edge

MemberResult
sitesTwo adjacent sites; boundary sides may be null
posTwo endpoint Point objects
verticesTwo 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.

Last updated on