Reaction-Diffusion

First appeared 1952 · Alan Turing — Alan Turing, "The Chemical Basis of Morphogenesis" (1952); Gray–Scott model, 1980s. · timeline ↗

community / folk Name — A descriptive term from chemistry/physics (reaction + diffusion); Turing's 1952 mechanism is separately eponymized as the "Turing pattern". Not coined as an algorithm name by any single author.

In one line A system of partial differential equations in which two substances diffuse at different rates while reacting with each other. The Gray–Scott model is the best-known; it spontaneously forms organic, biological-looking patterns (spots, stripes, mazes) — Turing's "morphogenesis".

Reaction-Diffusion · Open in new tab ↗ · Motion begins only when you press Run.

The code

The actual sketch behind the demo above — sandboxed and reproducible. This is the whole thing.

reaction-diffusion/index.html
const SEED = Math.floor(Math.random()*1e9);

// Gray-Scott reaction-diffusion on a small grid, scaled up to the canvas.
// Two chemicals A and B diffuse and react; B is rendered.
const W = 150, H = 150;
let A, B, A2, B2, dA = 1.0, dB = 0.5, feed, kill, ITER = 20;
const DT = 1.0;

function setup() {
  randomSeed(SEED);
  const s = Math.min(windowWidth, windowHeight);
  pixelDensity(1);
  const cnv = createCanvas(W, H);
  cnv.elt.style.width = s + 'px';
  cnv.elt.style.height = s + 'px';

  A = new Float32Array(W * H).fill(1);
  B = new Float32Array(W * H).fill(0);
  A2 = new Float32Array(W * H);
  B2 = new Float32Array(W * H);

  // Curated feed/kill pairs from Pearson's classification — each yields a
  // distinct, lively pattern (coral, mitosis, worms, spots, maze).
  const PAIRS = [
    [0.0545, 0.062],  // coral
    [0.0367, 0.0649], // mitosis
    [0.039, 0.058],   // worms
    [0.030, 0.062],   // spots/holes
    [0.029, 0.057],   // maze
    [0.062, 0.0609],  // u-skate
  ];
  [feed, kill] = PAIRS[Math.floor(random(PAIRS.length))];

  // Seed a few CHUNKY B squares. Small/dispersed seeds disperse below the
  // reaction threshold before a front can establish (especially in the worms
  // regime), so use solid blocks ~18-26px wide.
  const seeds = 6 + Math.floor(random(5));
  for (let k = 0; k < seeds; k++) {
    const half = Math.floor(random(8, 12));
    const cx = Math.floor(random(half + 4, W - half - 4));
    const cy = Math.floor(random(half + 4, H - half - 4));
    for (let y = -half; y <= half; y++)
      for (let x = -half; x <= half; x++) {
        B[(cy + y) * W + (cx + x)] = 1;
      }
  }
  noStroke();
}

function laplA(x, y) {
  const i = y * W + x;
  return (
    A[i] * -1 +
    (A[((y-1+H)%H)*W+x] + A[((y+1)%H)*W+x] + A[y*W+((x-1+W)%W)] + A[y*W+((x+1)%W)]) * 0.2 +
    (A[((y-1+H)%H)*W+((x-1+W)%W)] + A[((y-1+H)%H)*W+((x+1)%W)] + A[((y+1)%H)*W+((x-1+W)%W)] + A[((y+1)%H)*W+((x+1)%W)]) * 0.05
  );
}
function laplB(x, y) {
  const i = y * W + x;
  return (
    B[i] * -1 +
    (B[((y-1+H)%H)*W+x] + B[((y+1)%H)*W+x] + B[y*W+((x-1+W)%W)] + B[y*W+((x+1)%W)]) * 0.2 +
    (B[((y-1+H)%H)*W+((x-1+W)%W)] + B[((y-1+H)%H)*W+((x+1)%W)] + B[((y+1)%H)*W+((x-1+W)%W)] + B[((y+1)%H)*W+((x+1)%W)]) * 0.05
  );
}

function step() {
  for (let y = 0; y < H; y++) {
    for (let x = 0; x < W; x++) {
      const i = y * W + x;
      const a = A[i], b = B[i];
      const abb = a * b * b;
      // dt < 1 + clamp to [0,1] keeps the explicit Euler integration stable
      // (otherwise hot seeds blow up and collapse to a dead A=1, B=0 state).
      let na = a + (dA * laplA(x, y) - abb + feed * (1 - a)) * DT;
      let nb = b + (dB * laplB(x, y) + abb - (kill + feed) * b) * DT;
      A2[i] = na < 0 ? 0 : na > 1 ? 1 : na;
      B2[i] = nb < 0 ? 0 : nb > 1 ? 1 : nb;
    }
  }
  [A, A2] = [A2, A];
  [B, B2] = [B2, B];
}

function draw() {
  for (let k = 0; k < ITER; k++) step();
  loadPixels();
  for (let i = 0; i < W * H; i++) {
    const v = Math.max(0, Math.min(1, A[i] - B[i]));
    const idx = i * 4;
    // teal → cream ramp
    pixels[idx]     = 20 + v * 235;
    pixels[idx + 1] = 60 + v * 195;
    pixels[idx + 2] = 70 + v * 160;
    pixels[idx + 3] = 255;
  }
  updatePixels();
}

Open the demo source ↗

How it works

Hold two concentration fields, A and B, over a grid. Each timestep update every cell from the Gray–Scott equations: - A += (Dₐ·∇²A) − A·B² + f·(1−A) - B += (D_b·∇²B) + A·B² − (k+f)·B where ∇² is the Laplacian (diffusion, computed by convolving with a small kernel over the neighbours), A·B² is the reaction that converts A into B, f is the feed rate of A and k the kill rate of B. Because A diffuses faster than B (Dₐ > D_b), small fluctuations grow into stable patterns. Tuning f and k alone sweeps the system through spots, stripes, mazes, and self-replicating blobs.

Parameters

Lineage

Precedents
Alan Turing, Scott Draves
Related
Cellular Automata

Frequently combined with

Works using this algorithm (2)

Calian
eric-de-giuli
Multi-Scale Turing Patterns
jonathan-mccabe

Last updated 2026-07-16