{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"glyph-storm","type":"registry:ui","title":"Glyph Storm","description":"A full grid of monospace glyphs that keep swapping characters and colors, framed by a soft vignette. Canvas 2D.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/glyph-storm.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface GlyphStormProps {\n  /** Palette the glyphs flicker between. */\n  colors?: string[];\n  /** Background color behind the grid (also used by the vignette). */\n  background?: string;\n  /** Glitch rate multiplier; 0 freezes the grid. */\n  speed?: number;\n  /** Glyph size in px. */\n  fontSize?: number;\n  /** Characters to pick from. */\n  characters?: string;\n  /** Fade glyph colors instead of snapping them. */\n  smooth?: boolean;\n  /** Darken the edges. */\n  vignette?: boolean;\n  className?: string;\n}\n\ntype RGB = [number, number, number];\n\nfunction rgb(hex: string): RGB {\n  let h = hex.replace(\"#\", \"\");\n  if (h.length === 3) h = h.replace(/./g, \"$&$&\");\n  const n = parseInt(h.padEnd(6, \"0\").slice(0, 6), 16) || 0;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nconst DEFAULT_COLORS = [\"#1f3b2f\", \"#4ade80\", \"#38bdf8\", \"#a78bfa\"];\nconst DEFAULT_CHARS = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*<>[]{}/\\\\=+?;:\";\n\nexport function GlyphStorm({\n  colors = DEFAULT_COLORS,\n  background = \"#05070a\",\n  speed = 1,\n  fontSize = 16,\n  characters = DEFAULT_CHARS,\n  smooth = true,\n  vignette = true,\n  className,\n}: GlyphStormProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ colors, background, speed, characters, smooth });\n  const rebuild = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = { colors, background, speed, characters, smooth };\n  }, [colors, background, speed, characters, smooth]);\n  useEffect(() => {\n    rebuild.current?.();\n  }, [colors, background, characters]);\n\n  useEffect(() => {\n    const el = host.current!;\n    const canvas = el.querySelector(\"canvas\")!;\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n\n    let cols = 0;\n    let rows = 0;\n    let cw = 0;\n    let ch = 0;\n    let dpr = 1;\n    let glyph: string[] = [];\n    // Per cell: current color, target color, fade progress (1 = settled).\n    let cur = new Float32Array(0);\n    let tgt = new Float32Array(0);\n    let prog = new Float32Array(0);\n    let palette: RGB[] = [];\n    let raf = 0;\n    let visible = true;\n    let last = 0;\n    let budget = 0;\n\n    const pick = () => palette[(Math.random() * palette.length) | 0];\n    const char = () => {\n      const c = opts.current.characters || DEFAULT_CHARS;\n      return c[(Math.random() * c.length) | 0];\n    };\n\n    const paint = (i: number) => {\n      const x = (i % cols) * cw;\n      const y = ((i / cols) | 0) * ch;\n      ctx.fillStyle = opts.current.background;\n      ctx.fillRect(x, y, cw, ch);\n      ctx.fillStyle = `rgb(${cur[i * 3] | 0},${cur[i * 3 + 1] | 0},${cur[i * 3 + 2] | 0})`;\n      ctx.fillText(glyph[i], x + cw / 2, y + ch / 2);\n    };\n\n    const paintAll = () => {\n      ctx.fillStyle = opts.current.background;\n      ctx.fillRect(0, 0, canvas.width, canvas.height);\n      for (let i = 0; i < cols * rows; i++) paint(i);\n    };\n\n    const build = () => {\n      const size = Math.max(6, fontSize) * dpr;\n      cw = Math.ceil(size * 0.72);\n      ch = Math.ceil(size * 1.15);\n      cols = Math.ceil(canvas.width / cw);\n      rows = Math.ceil(canvas.height / ch);\n      const n = cols * rows;\n      palette = (opts.current.colors.length ? opts.current.colors : DEFAULT_COLORS).map(rgb);\n      glyph = Array.from({ length: n }, char);\n      cur = new Float32Array(n * 3);\n      tgt = new Float32Array(n * 3);\n      prog = new Float32Array(n).fill(1);\n      for (let i = 0; i < n; i++) {\n        const c = pick();\n        for (let k = 0; k < 3; k++) cur[i * 3 + k] = tgt[i * 3 + k] = c[k];\n      }\n      ctx.font = `600 ${size}px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace`;\n      ctx.textAlign = \"center\";\n      ctx.textBaseline = \"middle\";\n      paintAll();\n    };\n\n    const step = (dt: number) => {\n      const n = cols * rows;\n      // Roughly every cell changes once per second at speed 1.\n      budget += n * dt * opts.current.speed;\n      const smoothOn = opts.current.smooth;\n      while (budget >= 1) {\n        budget--;\n        const i = (Math.random() * n) | 0;\n        glyph[i] = char();\n        const c = pick();\n        for (let k = 0; k < 3; k++) tgt[i * 3 + k] = c[k];\n        prog[i] = 0;\n        if (!smoothOn) {\n          for (let k = 0; k < 3; k++) cur[i * 3 + k] = c[k];\n          prog[i] = 1;\n          paint(i);\n        }\n      }\n      if (!smoothOn) return;\n      const a = 1 - Math.exp(-dt * 8);\n      for (let i = 0; i < n; i++) {\n        if (prog[i] >= 1) continue;\n        prog[i] = Math.min(1, prog[i] + dt * 2.5);\n        for (let k = 0; k < 3; k++) {\n          const j = i * 3 + k;\n          cur[j] = prog[i] >= 1 ? tgt[j] : cur[j] + (tgt[j] - cur[j]) * a;\n        }\n        paint(i);\n      }\n    };\n\n    const loop = (now: number) => {\n      const dt = Math.min(now - last, 50) / 1000;\n      last = now;\n      step(dt);\n      raf = requestAnimationFrame(loop);\n    };\n    const play = () => {\n      if (raf || !visible || reduced) return;\n      last = performance.now();\n      raf = requestAnimationFrame(loop);\n    };\n    const stop = () => {\n      cancelAnimationFrame(raf);\n      raf = 0;\n    };\n    const resize = () => {\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = Math.max(1, Math.round(el.clientWidth * dpr));\n      canvas.height = Math.max(1, Math.round(el.clientHeight * dpr));\n      build();\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(el);\n    const io = new IntersectionObserver(([e]) => {\n      visible = e.isIntersecting;\n      if (visible) play();\n      else stop();\n    });\n    io.observe(el);\n    rebuild.current = build;\n\n    return () => {\n      stop();\n      ro.disconnect();\n      io.disconnect();\n      rebuild.current = null;\n    };\n  }, [fontSize]);\n\n  return (\n    <div\n      ref={host}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, overflow: \"hidden\", pointerEvents: \"none\", background }}\n    >\n      <canvas style={{ position: \"absolute\", inset: 0, width: \"100%\", height: \"100%\", display: \"block\" }} />\n      {vignette && (\n        <div\n          style={{\n            position: \"absolute\",\n            inset: 0,\n            background: `radial-gradient(ellipse at center, transparent 25%, ${background} 95%)`,\n          }}\n        />\n      )}\n    </div>\n  );\n}\n"}]}