Interactive Voronoi Diagrams & Delaunay Triangulation in Canvas: Real-Time Centroid Physics & Cellular Shading

The Quick Answer

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.

Interactive Demo Live Preview
💎 Delaunay Dual Triangulation & Centroids · Hover to interact

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.

voronoi-seeds.js
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.

triangulate.js
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.

cell-shading.js
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:

LLOYD'S RELAXATION

1. Centroidal Voronoi Relaxation (Lloyd's Algorithm)

Explore in Mesh Gradient Studio →

Smooth irregular cellular jitter into uniform hexagonal honeycomb tiles by iteratively moving each seed point to its cell centroid.

📍
ACTIVE MOUSE SEED POINT

2. Interactive Mouse Seed Insertion

Explore in Particle Background Studio →

Inject the mouse coordinates as an active seed point so that surrounding Voronoi cells dynamically subdivide and wrap around the user cursor.

3. Low-Poly 3D Shading with Directional Lighting

Explore in 3D Tilt Studio →

Simulate 3D faceted crystal geometry by calculating surface normals for each Delaunay triangle and calculating Lambertian cosine light reflections.

Chaam
Chaam's Pro Tip

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.

Why I Built the Mesh Gradient Studio

I got tired of guessing values and testing browser quirks in DevTools, so I built this tool to tweak and preview values in real time with 1-click code export.

Open in Mesh Gradient Studio ↗

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.

← Back to all blog guides Launch Generator Studio →