{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "fourier-sum",
  "title": "Fourier Sum",
  "description": "Sine harmonics add up one by one into a square wave under a serif caption.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/fourier-sum/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/JetBrainsMono\";\nimport { loadFont as loadDisplay } from \"@remotion/google-fonts/Newsreader\";\nimport { useId } from \"react\";\nimport {\n  AbsoluteFill,\n  Easing,\n  interpolate,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\n\nconst { fontFamily: sans } = loadFont(\"normal\", {\n  weights: [\"400\"],\n  subsets: [\"latin\", \"greek\"],\n});\nconst { fontFamily: display } = loadDisplay(\"normal\", {\n  weights: [\"400\"],\n  subsets: [\"latin\"],\n});\nloadDisplay(\"italic\", { weights: [\"400\"], subsets: [\"latin\"] });\n\n// gradients.sh \"aurora\" palette: the summed signal is drawn in it.\nconst AURORA = [\"#a0f0c8\", \"#3fb5a3\", \"#5147b6\", \"#b25c9c\"] as const;\n\nexport interface FourierSumProps {\n  harmonics?: number[];\n  title?: string;\n  periods?: number;\n  accent?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst DRAW = Easing.bezier(0.45, 0, 0.25, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\nconst THEMES = {\n  dark: {\n    bg: \"#0A0A0B\",\n    fg: \"#F3F2EF\",\n    muted: \"#8B8A90\",\n    hair: \"rgba(243,242,239,0.82)\",\n    base: \"rgba(255,255,255,0.14)\",\n    wash: \"rgba(255,255,255,0.045)\",\n    grain: 0.06,\n  },\n  light: {\n    bg: \"#F5F4F0\",\n    fg: \"#121214\",\n    muted: \"#6E6C67\",\n    hair: \"rgba(18,18,20,0.78)\",\n    base: \"rgba(18,18,20,0.14)\",\n    wash: \"rgba(255,255,255,0.7)\",\n    grain: 0.045,\n  },\n} as const;\n\nconst ROW_DRAW_AT = 0;\nconst ROW_STAGGER = 6;\nconst ROW_DRAW_DUR = 22;\nconst SLOT_IN = [22, 40] as const;\nconst MERGE_AT = 44;\nconst MERGE_STAGGER = 11;\nconst MERGE_DUR = 20;\nconst PHASE = Math.PI / 2;\n\nconst lerp = (a: number, b: number, k: number) => a + (b - a) * k;\n\nfunction Grain({ opacity }: { opacity: number }) {\n  const id = `g${useId().replace(/[^a-zA-Z0-9_-]/g, \"\")}`;\n  return (\n    <svg\n      width=\"100%\"\n      height=\"100%\"\n      style={{ position: \"absolute\", inset: 0, opacity }}\n      aria-hidden\n    >\n      <filter id={id}>\n        <feTurbulence\n          type=\"fractalNoise\"\n          baseFrequency=\"0.9\"\n          numOctaves={2}\n          seed={23}\n          stitchTiles=\"stitch\"\n        />\n        <feColorMatrix values=\"0 0 0 0 0.5  0 0 0 0 0.5  0 0 0 0 0.5  0 0 0 0.9 0\" />\n      </filter>\n      <rect width=\"100%\" height=\"100%\" filter={`url(#${id})`} />\n    </svg>\n  );\n}\n\nfunction wavePath(\n  x0: number,\n  w: number,\n  yc: number,\n  amp: number,\n  fn: (theta: number) => number,\n  periods: number,\n  prog: number,\n  samples: number,\n) {\n  const n = Math.max(2, Math.ceil(samples * prog));\n  let d = \"\";\n  for (let i = 0; i <= n; i++) {\n    const k = (prog * i) / n;\n    const x = x0 + w * k;\n    const y = yc - amp * fn(k * periods * Math.PI * 2);\n    d += `${i ? \"L\" : \"M\"}${x.toFixed(2)} ${y.toFixed(2)}`;\n  }\n  return d;\n}\n\nexport function FourierSum({\n  harmonics = [1, 3, 5, 7, 9],\n  title = \"Many parts. One signal.\",\n  periods = 2,\n  accent = \"#3B5BFD\",\n  speed = 1,\n  theme = \"dark\",\n}: FourierSumProps) {\n  const t = useCurrentFrame() * speed;\n  const { width: W, height: H } = useVideoConfig();\n  const u = H / 720;\n  const p = THEMES[theme];\n  const count = harmonics.length;\n  const maxN = Math.max(...harmonics);\n  const samples = Math.max(240, Math.ceil(maxN * periods * 14));\n\n  const labelCol = 58 * u;\n  const waveW = W * 0.7;\n  const blockX = (W - labelCol - waveW) / 2;\n  const waveX = blockX + labelCol;\n  const gap = Math.min(80 * u, (H * 0.6) / Math.max(1, count));\n  const amp = Math.min(34 * u, gap * 0.42);\n  const midY = H * 0.5;\n\n  const mergeStart = (i: number) => MERGE_AT + i * MERGE_STAGGER;\n  const mergeEnd = mergeStart(count - 1) + MERGE_DUR;\n  const merge = harmonics.map((_, i) =>\n    interpolate(t, [mergeStart(i), mergeStart(i) + MERGE_DUR], [0, 1], {\n      ...CLAMP,\n      easing: EASE_IO,\n    }),\n  );\n  const merged = merge.reduce((a, b) => a + b, 0);\n  const remaining = count - merged;\n  const slotIndexY = (idx: number) => midY + (idx - remaining / 2) * gap;\n  const rowIndex = (i: number) => 1 + i - merged;\n\n  const FINAL = [mergeEnd - 2, mergeEnd + 26] as const;\n  const TITLE_AT = mergeEnd + 14;\n\n  const weights = harmonics.map((_, i) =>\n    interpolate(\n      t,\n      [mergeStart(i) + MERGE_DUR * 0.5, mergeStart(i) + MERGE_DUR],\n      [0, 1],\n      { ...CLAMP, easing: EASE_IO },\n    ),\n  );\n  const sumFn = (th: number) =>\n    harmonics.reduce(\n      (s, n, i) => s + (weights[i] * Math.sin(n * (th + PHASE))) / n,\n      0,\n    );\n\n  const slotY = slotIndexY(0);\n  const fin = interpolate(t, FINAL, [0, 1], { ...CLAMP, easing: EASE_IO });\n  const finW = W * 0.74;\n  const finY = H * 0.43;\n  const sumX = lerp(waveX, (W - finW) / 2, fin);\n  const sumW = lerp(waveW, finW, fin);\n  const sumY = lerp(slotY, finY, fin);\n  const sumAmp = lerp(amp, 108 * u, fin);\n  const sumStroke = lerp(2.6, 5.5, fin) * u;\n  const sumO = Math.min(1, weights[0] * 2.5);\n\n  const slotIn = interpolate(t, SLOT_IN, [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n  const slotO = slotIn * (1 - fin);\n  const titleA = interpolate(t, [TITLE_AT, TITLE_AT + 20], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n\n  const capY = lerp(slotY - amp - 40 * u, finY - 158 * u, fin);\n\n  const gid = useId().replace(/[^a-zA-Z0-9_-]/g, \"\");\n  const isDark = theme === \"dark\";\n  const drift = interpolate(t, [0, 180], [0, 1], { ...CLAMP, easing: EASE_IO });\n  const stops = isDark\n    ? [AURORA[0], AURORA[1], AURORA[2], AURORA[3]]\n    : [AURORA[1], AURORA[2], AURORA[3]];\n  const gx1 = sumX - sumW * 0.12 * drift;\n  const gx2 = sumX + sumW * (1.04 - 0.12 * drift);\n  const fillO = fin * (isDark ? 0.34 : 0.26);\n  const sentence = title.match(/^(.*?[.!?\\u2026])\\s+(.+)$/);\n  const inkText = {\n    backgroundImage: `linear-gradient(90deg, ${stops.join(\", \")})`,\n    WebkitBackgroundClip: \"text\",\n    backgroundClip: \"text\",\n    color: \"transparent\",\n  } as const;\n  const capX = lerp(waveX, W / 2, fin);\n\n  return (\n    <AbsoluteFill style={{ background: p.bg, overflow: \"hidden\" }}>\n      <AbsoluteFill\n        style={{\n          background: `radial-gradient(ellipse ${W * 0.5}px ${H * 0.45}px at 50% 46%, ${p.wash}, transparent 100%)`,\n        }}\n      />\n      <svg\n        width={W}\n        height={H}\n        style={{ position: \"absolute\", inset: 0 }}\n        aria-hidden\n      >\n        <defs>\n          <linearGradient\n            id={`${gid}s`}\n            gradientUnits=\"userSpaceOnUse\"\n            x1={gx1}\n            x2={gx2}\n            y1={0}\n            y2={0}\n          >\n            {stops.map((c, i) => (\n              <stop key={c} offset={i / (stops.length - 1)} stopColor={c} />\n            ))}\n          </linearGradient>\n          <linearGradient\n            id={`${gid}f`}\n            gradientUnits=\"userSpaceOnUse\"\n            x1={0}\n            x2={0}\n            y1={sumY - sumAmp * 1.25}\n            y2={sumY + sumAmp * 1.25}\n          >\n            <stop offset={0} stopColor=\"#fff\" stopOpacity={1} />\n            <stop offset={0.5} stopColor=\"#fff\" stopOpacity={0.05} />\n            <stop offset={1} stopColor=\"#fff\" stopOpacity={1} />\n          </linearGradient>\n          <mask id={`${gid}m`} maskUnits=\"userSpaceOnUse\">\n            <rect width={W} height={H} fill={`url(#${gid}f)`} />\n          </mask>\n        </defs>\n        <line\n          x1={waveX}\n          x2={waveX + waveW * slotIn}\n          y1={slotY}\n          y2={slotY}\n          stroke={p.base}\n          strokeWidth={Math.max(1, u)}\n          opacity={slotO}\n        />\n        {harmonics.map((n, i) => {\n          const drawAt = ROW_DRAW_AT + i * ROW_STAGGER;\n          const prog = interpolate(t, [drawAt, drawAt + ROW_DRAW_DUR], [0, 1], {\n            ...CLAMP,\n            easing: DRAW,\n          });\n          if (prog <= 0) return null;\n          const o = interpolate(\n            t,\n            [mergeStart(i) + MERGE_DUR * 0.6, mergeStart(i) + MERGE_DUR],\n            [1, 0],\n            CLAMP,\n          );\n          if (o <= 0) return null;\n          return (\n            <path\n              key={n}\n              d={wavePath(\n                waveX,\n                waveW,\n                slotIndexY(rowIndex(i)),\n                amp,\n                (th) => Math.sin(n * (th + PHASE)) / n,\n                periods,\n                prog,\n                samples,\n              )}\n              fill=\"none\"\n              stroke={p.hair}\n              strokeWidth={1.4 * u}\n              strokeLinecap=\"round\"\n              strokeLinejoin=\"round\"\n              opacity={o}\n            />\n          );\n        })}\n        {fillO > 0 && (\n          <path\n            d={`${wavePath(sumX, sumW, sumY, sumAmp, sumFn, periods, 1, samples)}L${(sumX + sumW).toFixed(2)} ${sumY.toFixed(2)}L${sumX.toFixed(2)} ${sumY.toFixed(2)}Z`}\n            fill={`url(#${gid}s)`}\n            opacity={fillO}\n            mask={`url(#${gid}m)`}\n          />\n        )}\n        {sumO > 0 && (\n          <path\n            d={wavePath(sumX, sumW, sumY, sumAmp, sumFn, periods, 1, samples)}\n            fill=\"none\"\n            stroke={`url(#${gid}s)`}\n            strokeWidth={sumStroke}\n            strokeLinecap=\"round\"\n            strokeLinejoin=\"round\"\n            opacity={sumO}\n          />\n        )}\n      </svg>\n\n      {harmonics.map((n, i) => {\n        const drawAt = ROW_DRAW_AT + i * ROW_STAGGER;\n        const a = interpolate(t, [drawAt + 2, drawAt + 14], [0, 1], {\n          ...CLAMP,\n          easing: EASE_OUT,\n        });\n        const out = interpolate(\n          t,\n          [mergeStart(i), mergeStart(i) + 8],\n          [1, 0],\n          CLAMP,\n        );\n        return (\n          <div\n            key={n}\n            style={{\n              position: \"absolute\",\n              left: blockX,\n              top: slotIndexY(rowIndex(i)) - 12 * u,\n              width: labelCol - 14 * u,\n              textAlign: \"right\",\n              fontFamily: sans,\n              fontWeight: 400,\n              fontSize: 15 * u,\n              lineHeight: `${24 * u}px`,\n              color: p.muted,\n              fontVariantNumeric: \"tabular-nums\",\n              opacity: a * out,\n              transform: `translateX(${(1 - a) * -8 * u}px)`,\n            }}\n          >\n            {n}×\n          </div>\n        );\n      })}\n\n      <div\n        style={{\n          position: \"absolute\",\n          left: blockX,\n          top: slotY - 16 * u,\n          width: labelCol - 14 * u,\n          textAlign: \"right\",\n          fontFamily: sans,\n          fontWeight: 400,\n          fontSize: 22 * u,\n          lineHeight: `${32 * u}px`,\n          color: accent,\n          opacity: slotO,\n          transform: `translateX(${(1 - slotIn) * 10 * u}px)`,\n        }}\n      >\n        Σ\n      </div>\n\n      <div\n        style={{\n          position: \"absolute\",\n          left: capX,\n          top: capY,\n          transform: `translateX(${-50 * fin}%)`,\n          display: \"flex\",\n          whiteSpace: \"nowrap\",\n          fontFamily: sans,\n          fontWeight: 400,\n          fontSize: 15 * u,\n          lineHeight: `${24 * u}px`,\n          color: p.muted,\n          fontVariantNumeric: \"tabular-nums\",\n        }}\n      >\n        {harmonics.map((n, i) => {\n          const a = interpolate(\n            t,\n            [mergeStart(i) + MERGE_DUR * 0.6, mergeStart(i) + MERGE_DUR + 4],\n            [0, 1],\n            { ...CLAMP, easing: EASE_OUT },\n          );\n          return (\n            <span\n              key={n}\n              style={{\n                opacity: a,\n                transform: `translateY(${(1 - a) * 6 * u}px)`,\n                display: \"inline-block\",\n              }}\n            >\n              {i ? <span style={{ padding: `0 ${8 * u}px` }}>+</span> : null}\n              {n}×\n            </span>\n          );\n        })}\n      </div>\n\n      <div\n        style={{\n          position: \"absolute\",\n          left: 0,\n          right: 0,\n          top: finY + 162 * u,\n          textAlign: \"center\",\n          fontFamily: display,\n          fontWeight: 400,\n          fontSize: 60 * u,\n          lineHeight: 1.05,\n          letterSpacing: \"-0.02em\",\n          color: p.fg,\n          textWrap: \"balance\",\n          opacity: titleA,\n          transform: `translateY(${(1 - titleA) * 18 * u}px)`,\n        }}\n      >\n        {sentence ? (\n          <>\n            {sentence[1]}{\" \"}\n            <span\n              style={{\n                ...inkText,\n                fontStyle: \"italic\",\n                paddingRight: \"0.08em\",\n              }}\n            >\n              {sentence[2]}\n            </span>\n          </>\n        ) : (\n          title\n        )}\n      </div>\n\n      <AbsoluteFill\n        style={{\n          background: `radial-gradient(ellipse 80% 80% at 50% 50%, transparent 62%, ${p.bg} 100%)`,\n          opacity: 0.6,\n        }}\n      />\n      <Grain opacity={p.grain} />\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/fourier-sum.tsx"
    }
  ],
  "type": "registry:component"
}
