全部效果自动播放与循环

傅里叶圆周绘制

Fourier Epicycle Drawing

一组不同频率的旋转向量首尾相接,最后一个端点重建采样轮廓并留下笔迹。

播放或暂停演示

适用场景

适合图形标志的揭示动效,也能解释频率分量如何重建轮廓。 选容易辨认的闭合轮廓,并用不同颜色区分旋转圆周和笔迹,方便读者把运动和画出的形状对应起来。

应用示例

一篇可交互的数学文章在风筝轮廓旁放了播放按钮。点下后,一串圆首尾相接地旋转,最后的端点重新画出风筝,读者可以在下一轮开始前看清整个绘制过程。

AI 提示词

用离散傅里叶变换绘制【轮廓】。对闭合线等距采样,计算有符号频率的复系数,按振幅绘制主导旋转圆,另绘端点轨迹,并在相位绕回时重新开始。

示例代码

保存为 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 = 4.2;
      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 samples = Array.from({ length: 96 }, (_, i) => {
        const phase = i / 96 * turn;
        const radius = 40 + 10 * Math.cos(3 * phase) + 4.5 * Math.sin(5 * phase);
        return [radius * Math.cos(phase), radius * Math.sin(phase) * .88];
      });
      const circles = Array.from({ length: 17 }, (_, index) => {
        const frequency = index - 8;
        let real = 0;
        let imaginary = 0;
        samples.forEach(([x, y], sample) => {
          const angle = -turn * sample * frequency / samples.length;
          real += x * Math.cos(angle) - y * Math.sin(angle);
          imaginary += x * Math.sin(angle) + y * Math.cos(angle);
        });
        return {
          frequency,
          radius: Math.hypot(real, imaginary) / samples.length,
          phase: Math.atan2(imaginary, real),
        };
      }).filter(({ radius }) => radius > .3).sort((a, b) => b.radius - a.radius);

      function endpoint(angle, guides) {
        let x = 144;
        let y = 72;
        for (const circle of circles) {
          const next = circle.frequency * angle + circle.phase;
          const dx = Math.cos(next) * circle.radius;
          const dy = Math.sin(next) * circle.radius;
          if (guides) {
            ctx.beginPath();
            ctx.arc(x, y, circle.radius, 0, turn);
            ctx.moveTo(x, y);
            ctx.lineTo(x + dx, y + dy);
            ctx.stroke();
          }
          x += dx;
          y += dy;
        }
        return [x, y];
      }

      function draw(time) {
        ctx.fillStyle = '#142a2d';
        ctx.fillRect(0, 0, width, height);
        const angle = (time * .78) % turn;
        ctx.strokeStyle = '#91ead16e';
        const [headX, headY] = endpoint(angle, true);
        ctx.strokeStyle = '#dcfbe1';
        ctx.lineWidth = 1.6;
        ctx.beginPath();
        for (let i = 0; i <= 120; i++) {
          const [x, y] = endpoint((i / 120) * angle, false);
          if (i === 0) ctx.moveTo(x, y);
          else ctx.lineTo(x, y);
        }
        ctx.stroke();
        dot(headX, headY, 2.7, '#ffd684');
      }

      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 原创示例

相关参考