Fast and Accurate Geodesic Distances in JavaScript
Compute geodesic distances on a triangle mesh in JavaScript and TypeScript. Distance along the surface from one seed or many, bounded by a radius, in float32 or float64. WebAssembly, runs in Node.js and the browser.
A geodesic distance is the distance from a point to a seed, measured along the surface of a mesh. Straight-line distance cuts through the object. Geodesic distance walks over it. trueform computes it with one call, tf.geodesicDistances, new in version 0.10.8. One npm install. It runs as WebAssembly in Node.js and the browser, with TypeScript types. This tutorial covers one seed, many seeds, a bounded radius, float32 and float64, and turning distances into rings and patches.

Install
npm install @polydera/trueform
You need version 0.10.8 or later. The WebAssembly binary loads on first use. In the browser, multithreading needs SharedArrayBuffer headers. See the installation guide.
Loading the mesh
import * as tf from "@polydera/trueform";
import { readFileSync } from "node:fs";
const mesh = tf.readStl(readFileSync("part.stl"));
In the browser, pass the bytes from fetch:
const bytes = new Uint8Array(await (await fetch("part.stl")).arrayBuffer());
const mesh = tf.readStl(bytes);
tf.readObj works the same way. If you already have typed arrays, pass them to tf.mesh(faces, points). To follow along without a file, make a unit sphere:
const mesh = tf.sphereMesh(1.0, 200, 400);
Distance from one seed
A seed is a point id. Seeds go in as an Int32Array wrapped in an NDArray. The result has one distance per point.
const seeds = tf.ndarray(new Int32Array([0]), [1]);
const d = tf.geodesicDistances(mesh, seeds);
// d: NDArray, shape [numberOfPoints], the mesh's dtype
// d.data[0] === 0

The distance is zero at the seed. On the unit sphere, the farthest point is the opposite pole, at a distance close to π.
You can check the result on this sphere. The exact geodesic distance on a unit sphere is the great-circle angle:
const p = mesh.points.data;
let maxErr = 0;
for (let i = 0; i < mesh.numberOfPoints; i++) {
const c = p[3*i]*p[0] + p[3*i+1]*p[1] + p[3*i+2]*p[2];
const exact = Math.acos(Math.min(1, Math.max(-1, c)));
maxErr = Math.max(maxErr, Math.abs(d.data[i] - exact));
}
The difference is small, and it shrinks as the sphere gets finer.
Most of the time the seed is a position, not an id. Find the nearest mesh point first:
const cloud = tf.pointCloud(mesh.points);
const { elementId: seed } = tf.neighborSearch(cloud, tf.point(0, 0, -1));
const fromSeed = tf.geodesicDistances(
mesh, tf.ndarray(new Int32Array([seed]), [1]));
Distance from many seeds
Pass several ids. Each point gets the distance to its nearest seed.
The seeds can be a whole curve. Then the field is the distance from that curve, measured along the surface. That is the distance from an edge: an offset from a trim line, a falloff from a seam.
Take the bunny and cut it in half. The cut is the boundary of the half. Its points are the seeds:
const bunny = tf.readStl(readFileSync("bunny.stl"));
// cut it at the middle height; the bunny stands along y
const height = bunny.points.take(null, 1);
let lo = Infinity, hi = -Infinity;
for (const y of height.data) { lo = Math.min(lo, y); hi = Math.max(hi, y); }
const middle = 0.5 * (lo + hi);
const { mesh: half } = tf.isobands(
bunny, height, [middle], { selectedBands: [1] });
// the cut is the open boundary of the half
const cut = tf.boundaryPaths(half).data; // point ids on the cut
const fromCut = tf.geodesicDistances(half, cut);

Keep the top band. The bunny has holes in its base, so the top half has one boundary: the cut. The ids index the half's own points, so nothing maps back. The distance is zero on the cut and grows toward the ear tips.
One call handles all the seeds, here a few hundred. You do not loop over them. Isocontours of the field are curves parallel to the cut:
const rings = tf.isocontours(half, fromCut, [1, 2, 3]);
Bounded by a radius
Often you only care about the neighborhood of a seed. Pass radius. The front stops there.
const near = tf.geodesicDistances(mesh, seeds, { radius: 0.5 });
const inside = near.lt(Infinity); // NDArray of bool
const region = tf.arange("int32", mesh.numberOfPoints).booleanIndex(inside);
// region: point ids within 0.5 of the seed

Points past the radius get Infinity. So do points on a piece of the mesh the seed is not connected to, with or without a radius. Test against Infinity, not against a large number.
float32 or float64
The result has the mesh's dtype. A float32 mesh gives float32 distances. A float64 mesh gives float64 distances.
const mesh64 = tf.sphereMesh(1.0, 200, 400, { dtype: "float64" });
const d64 = tf.geodesicDistances(mesh64, seeds); // d64.dtype === "float64"
tf.readStl and the mesh generators give float32 by default. Pass Float64Array points to tf.mesh for a double-precision mesh.
Distances are measured in the stored coordinates. A transformation set on the mesh does not change them.
Off the main thread
In the browser, a large mesh can block the UI. The async variant runs off the main thread and returns a Promise:
const d = await tf.async.geodesicDistances(mesh, seeds);
Working with results
The distance field is an NDArray, one value per point. d.data is the underlying typed array, ready to upload as a vertex attribute for coloring. Any scalar-field tool in trueform takes the field directly.
Draw rings of equal distance with isocontours:
const rings = tf.isocontours(mesh, d, [0.25, 0.5, 1.0]);
for (const pathIds of rings.paths) {
const ids = pathIds.data; // Int32Array, indices into rings.points
}

The rings do not come back in level order. If you need to know which ring is which, call tf.isocontours once per level.
Cut out the patch within a distance as its own mesh with isobands:
const { mesh: patch, faceLabels } = tf.isobands(
mesh, d, [0.5], { selectedBands: [0] });
const objBytes = tf.writeObj(patch).data; // Int8Array, ready to save or download
Band 0 is everything below 0.5. The patch boundary is cut exactly at the 0.5 ring. faceLabels maps each patch face back to its source face.
The field also feeds the curvature and boolean tools in the same library.
About other methods
People search for the heat method, fast marching, or exact MMP geodesics. They all answer the same question: distance along the surface from seeds. tf.geodesicDistances runs a label-correcting front with a triangle-unfolding update. It is not the heat method.
Related tutorials
- Fast and Robust Isocontours on a Triangle Mesh in JavaScript — curves and bands of equal value on a scalar field.
- Fast and Robust Principal Curvatures of a Mesh in JavaScript — curvature values and directions at every vertex.
- Fast and Robust Mesh Slicing and Cross-Sections in JavaScript — plane sections, layers and slabs of a mesh.
- Fast and Robust Closest Point on a Mesh in JavaScript — nearest surface point for a batch of queries.
Performance
We measured the 28 Thingi10K meshes of the Python benchmark, 10K to 3.2M faces, and the dragon at five sizes, from one seed and from 32 seeds. There is no peer library in JavaScript: npm has no maintained geodesic-distance package for meshes, so trueform is measured alone. Building the mesh's topology is inside the timer.
Apple M4 Max 16 threads Node.js 25 · WebAssembly
Median of 9 runs after 2 warm-ups, in one Node.js process. The distances carry the same error against exact MMP geodesics as the native build on the Python page.
Measured 2026-10-31 with trueform 0.10.8; harness and raw results: benchmarks/tutorials/javascript-geodesic-distances.
Full benchmarks · Documentation
@article{polydera:fast-geodesic-distances-in-javascript,
title={Fast and Accurate Geodesic Distances in JavaScript},
author={Sajovic, {\v{Z}}iga, Polydera},
year={2026},
url={https://polydera.com/tutorials/fast-geodesic-distances-in-javascript},
organization={Polydera}
}