全部效果自动播放与循环

反应扩散纹理生长

Reaction–Diffusion Surface

两组数值在网格中反应和扩散,由初始种子逐步生成有机斑块与迷宫状纹路。

播放或暂停演示

适用场景

适合作为可生成的材质样本,或节奏缓慢的艺术背景。 给初始点留足时间生长成连通纹样,并让暂停后的画面也能作为材质样片查看。

应用示例

纺织工作室展示一款可播放的印花样片。几个初始点慢慢扩散成迷宫状的分叉斑块,旁边的固定色样则留出一个可静看对照的版本。

AI 提示词

为【纹理】模拟 Gray–Scott 反应扩散。使用两组浓度缓冲,以固定模板和时间步交替更新。用可复现种子生成图案,先渲染到小尺寸栅格再放大。限制更新预算,离屏或减少动态时保留静态图。

示例代码

保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。

查看 HTML
<!doctype html>
<html lang="zh">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>反应扩散纹理生长</title>
  <style>
    :root {
      color-scheme: dark;
      --demo-bg: #171629;
      --demo-ink: #f8f5ff;
      --demo-accent: #b9a6ff;
      --demo-muted: #bfbbd3;
      font-family: system-ui, sans-serif;
      color: var(--demo-ink);
      background: var(--demo-bg);
    }

    *, *::before, *::after { box-sizing: border-box; }
    body { margin: 0; min-width: 0; }
    button, input { font: inherit; }

    .demo {
      display: grid;
      place-items: center;
      align-content: center;
      min-height: 100vh;
      padding: 14px;
      overflow: hidden;
      background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
    }

    .demo-button {
      border: 1px solid #ffffff3d;
      border-radius: 7px;
      background: #ffffff12;
      color: var(--demo-ink);
      padding: 6px 11px;
      font-size: 12px;
      cursor: pointer;
    }

    :focus-visible {
      outline: 2px solid var(--demo-accent);
      outline-offset: 3px;
    }

    .specimen {
      display: grid;
      width: min(100%, 356px);
      gap: 8px;
      margin: 0;
      justify-items: center;
    }
    .specimen canvas {
      display: block;
      width: 100%;
      aspect-ratio: 2;
      max-height: max(72px, calc(100vh - 65px));
      border-radius: 11px;
      background: linear-gradient(145deg, #232943, #14172c);
      box-shadow: 0 8px 22px #090a16a8;
    }
    .specimen label {
      display: grid;
      width: 100%;
      min-height: 25px;
      grid-template-columns: minmax(0, auto) minmax(56px, 1fr);
      align-items: center;
      gap: 8px;
      color: var(--demo-ink);
      font-size: 12px;
    }
    .specimen input {
      width: 100%;
      margin: 0;
      accent-color: #b9a6ff;
    }
    @media (prefers-reduced-motion: reduce) {
      .specimen [data-loop-toggle] { display: none; }
    }
  </style>
</head>
<body>
  <main class="demo">
    <figure class="specimen">
      <canvas role="img" aria-label="反应扩散纹理生长" width="288" height="144">反应扩散纹理生长</canvas>
      <button class="demo-button" type="button" data-loop-toggle data-testid="effect-demo-loop-toggle">播放演示</button>
    </figure>
  </main>
  <script>
    (() => {
      const canvas = document.querySelector('.specimen canvas');
      const ctx = canvas.getContext('2d');
      if (!ctx) {
        document.querySelector('.specimen :is(input, button)').disabled = true;
        return;
      }
      const width = 288;
      const height = 144;
      const turn = Math.PI * 2;
      let elapsed = 0;
      let variation = 0;

      function dot(x, y, radius, ink) {
        ctx.fillStyle = ink;
        ctx.beginPath();
        ctx.arc(x, y, radius, 0, turn);
        ctx.fill();
      }

      function mix(a, b, amount) {
        return a + (b - a) * amount;
      }


      const columns = 72;
      const rows = 36;
      let a = new Float32Array(columns * rows).fill(1);
      let b = new Float32Array(columns * rows);
      let nextA = new Float32Array(a.length);
      let nextB = new Float32Array(b.length);
      const seedCenters = [[16, 13], [34, 25], [40, 10], [59, 19], [7, 30]];
      for (const [x, y] of seedCenters) {
        for (let vertical = -3; vertical <= 3; vertical++) {
          for (let horizontal = -3; horizontal <= 3; horizontal++) {
            if (horizontal * horizontal + vertical * vertical > 10) continue;
            const index = (y + vertical) * columns + x + horizontal;
            a[index] = .52;
            b[index] = .27 + ((index * 13) % 17) * .006;
          }
        }
      }
      function advance() {
        for (let y = 0; y < rows; y++) {
          const top = ((y + rows - 1) % rows) * columns;
          const bottom = ((y + 1) % rows) * columns;
          const row = y * columns;
          for (let x = 0; x < columns; x++) {
            const left = (x + columns - 1) % columns;
            const right = (x + 1) % columns;
            const i = row + x;
            const ac = a[i];
            const bc = b[i];
            const reaction = ac * bc * bc;
            const diffusionA = .2 * (a[top + x] + a[bottom + x] + a[row + left] + a[row + right]) + .05 * (a[top + left] + a[top + right] + a[bottom + left] + a[bottom + right]) - ac;
            const diffusionB = .2 * (b[top + x] + b[bottom + x] + b[row + left] + b[row + right]) + .05 * (b[top + left] + b[top + right] + b[bottom + left] + b[bottom + right]) - bc;
            nextA[i] = ac + diffusionA - reaction + .0367 * (1 - ac);
            nextB[i] = bc + .5 * diffusionB + reaction - (.0367 + .0649) * bc;
          }
        }
        [a, nextA] = [nextA, a];
        [b, nextB] = [nextB, b];
      }
      const field = document.createElement('canvas');
      field.width = columns;
      field.height = rows;
      const raster = field.getContext('2d');
      const pixels = raster.createImageData(columns, rows);
      for (let step = 0; step < 420; step++) advance();
      let simulated = 0;

      function draw(time) {
        const target = Math.floor(time * 58);
        const steps = Math.min(5, target - simulated);
        for (let step = 0; step < steps; step++) advance();
        simulated = target;
        for (let i = 0; i < b.length; i++) {
          const strength = Math.max(0, Math.min(1, (b[i] - .055) * 3.8));
          pixels.data[i * 4] = mix(28, 231, strength);
          pixels.data[i * 4 + 1] = mix(33, 188, strength ** .69);
          pixels.data[i * 4 + 2] = mix(79, 149, strength);
          pixels.data[i * 4 + 3] = 255;
        }
        raster.putImageData(pixels, 0, 0);
        ctx.imageSmoothingEnabled = true;
        ctx.drawImage(field, 0, 0, width, height);
      }

      function render() {
        const ratio = Math.min(devicePixelRatio || 1, 2);
        const pixelsWide = Math.max(1, Math.round(canvas.clientWidth * ratio));
        const pixelsTall = Math.max(1, Math.round(canvas.clientHeight * ratio));
        if (canvas.width !== pixelsWide || canvas.height !== pixelsTall) {
          canvas.width = pixelsWide;
          canvas.height = pixelsTall;
        }
        ctx.setTransform(pixelsWide / width, 0, 0, pixelsTall / height, 0, 0);
        ctx.clearRect(0, 0, width, height);
        ctx.save();
        draw(elapsed, variation);
        ctx.restore();
      }

      render();
      new ResizeObserver(render).observe(canvas);

      const button = document.querySelector('[data-loop-toggle]');
      const preference = matchMedia('(prefers-reduced-motion: reduce)');
      let playing = false;
      let intersecting = false;
      let pendingFrame = null;
      let previousTime = 0;

      function canAnimate() {
        return playing && intersecting && !preference.matches && !document.hidden;
      }

      function tick(now) {
        pendingFrame = null;
        if (!canAnimate()) return;
        elapsed += Math.min(Math.max(0, now - previousTime), 45) / 1000;
        previousTime = now;
        render();
        pendingFrame = requestAnimationFrame(tick);
      }

      function syncPlayback() {
        if (pendingFrame !== null) cancelAnimationFrame(pendingFrame);
        pendingFrame = null;
        if (canAnimate()) {
          previousTime = performance.now();
          pendingFrame = requestAnimationFrame(tick);
        }
      }

      button.addEventListener('click', () => {
        playing = !playing;
        button.textContent = playing ? "暂停演示" : "播放演示";
        button.closest('.specimen').toggleAttribute('data-playing', playing);
        syncPlayback();
      });
      new IntersectionObserver(([entry]) => {
        intersecting = entry.isIntersecting;
        syncPlayback();
      }).observe(canvas);
      preference.addEventListener('change', syncPlayback);
      document.addEventListener('visibilitychange', syncPlayback);
    })();
  </script>
</body>
</html>

UI Skills 原创示例

相关参考