Interactive Voronoi Diagrams & Delaunay Triangulation in Canvas: Real-Time Centroid Physics & Cellular Shading
Voronoi diagrams partition a 2D canvas into polygonal cells where every point in a cell is closer to its seed generator than any other. Delaunay triangulation is the geometric dual: connecting seed points with non-overlapping triangles whose circumcircles contain no other points. Using Fortune's sweepline algorithm or the Bowyer-Watson method, you can compute real-time cellular mesh animations responding to cursor proximity.
Step 1: Understanding Delaunay Duality & Seed Points
Generate an array of 40–80 seed points with gentle autonomous velocity vectors. Connect neighboring points with Delaunay triangles, then construct Voronoi polygons by joining the circumcenters of adjacent triangles.
class SeedPoint {
constructor(x, y) {
this.x = x;
this.y = y;
this.vx = (Math.random() - 0.5) * 0.8;
this.vy = (Math.random() - 0.5) * 0.8;
}
update(w, h) {
this.x += this.vx;
this.y += this.vy;
if (this.x < 0 || this.x > w) this.vx *= -1;
if (this.y < 0 || this.y > h) this.vy *= -1;
}
}
Step 2: Triangulation with Delaunator or Bowyer-Watson
Compute Delaunay triangulation each frame. For 60fps performance, Delaunator processes 1,000 points in less than 1ms using half-edge data structures.
import Delaunator from 'delaunator';
function renderDelaunay(seeds, ctx) {
const coords = [];
for (let s of seeds) coords.push(s.x, s.y);
const delaunay = new Delaunator(coords);
const triangles = delaunay.triangles;
ctx.strokeStyle = 'rgba(99, 102, 241, 0.25)';
ctx.lineWidth = 1;
for (let i = 0; i < triangles.length; i += 3) {
const p0 = triangles[i];
const p1 = triangles[i + 1];
const p2 = triangles[i + 2];
ctx.beginPath();
ctx.moveTo(coords[2 * p0], coords[2 * p0 + 1]);
ctx.lineTo(coords[2 * p1], coords[2 * p1 + 1]);
ctx.lineTo(coords[2 * p2], coords[2 * p2 + 1]);
ctx.closePath();
ctx.stroke();
}
}
Step 3: Cellular Shading & Distance-to-Edge Gradients
Shade each polygonal Voronoi cell with gradient fills proportional to its distance from the cursor or seed point elevation, creating a stained-glass or low-poly terrain aesthetic.
function shadeCell(polygon, seed, mousePos, ctx) {
const dist = Math.hypot(seed.x - mousePos.x, seed.y - mousePos.y);
const alpha = Math.max(0.1, 1 - dist / 400);
ctx.fillStyle = `rgba(168, 85, 247, ${alpha * 0.4})`;
ctx.beginPath();
ctx.moveTo(polygon[0][0], polygon[0][1]);
for (let i = 1; i < polygon.length; i++) {
ctx.lineTo(polygon[i][0], polygon[i][1]);
}
ctx.closePath();
ctx.fill();
}
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
When building high-performance UI animations or particle systems, my golden rule is: never calculate layout triggers (like getBoundingClientRect() or offsetHeight) inside animation loops. Read measurements once upfront and use CSS transforms or Canvas for buttery 60fps.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
What is the algorithmic complexity of Voronoi diagram generation?
Brute-force pixel checking is O(n · width · height). Fortune's sweepline algorithm and Delaunay dual triangulation run in O(n log n), allowing you to recompute 100+ moving seeds in under 1ms on every animation frame.
Can Voronoi diagrams be created in pure CSS without Canvas or JS?
Pure CSS cannot calculate dynamic Voronoi math. However, static Voronoi patterns can be exported as SVG vector backgrounds or simulated with repeating radial gradients, though they lack interactive physics.
How do you handle bounding box clipping at canvas edges?
Add four corner boundary points at [-100, -100], [width+100, -100], [width+100, height+100], and [-100, height+100] to your seed list. This automatically encloses all outer Delaunay triangles and prevents infinite Voronoi rays.
How do you prevent Voronoi cells from looking too chaotic or sharp?
Apply Lloyd's Relaxation: calculate the center of mass (centroid) of each Voronoi polygon and nudge the seed point toward that centroid. 2–3 relaxation steps create uniform, aesthetically pleasing organic cell lattices.
How does mobile touch affect Voronoi canvas performance?
Keep the active seed count below 60 on mobile. Rather than rebuilding the Delaunay index on every single touchmove event, throttle re-triangulation to 60fps via requestAnimationFrame.