{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-glyph-rain",
  "type": "registry:component",
  "title": "Glyph Rain",
  "description": "Falling columns of glyphs on three depth planes, with bright flickering heads, long fading tails and a slow scan line that lifts the glyphs it crosses. Choose hex, binary, block or symbol glyphs, or pass your own.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "code rain",
    "glyphs",
    "terminal",
    "canvas",
    "monospace"
  ],
  "docs": "https://hyperblur.dev/effects/bg-glyph-rain",
  "files": [
    {
      "path": "registry/hyperblur/bg-glyph-rain.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-glyph-rain.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport type BgGlyphRainVariant = \"hex\" | \"binary\" | \"blocks\" | \"symbols\";\n\nexport interface BgGlyphRainProps extends React.HTMLAttributes<HTMLDivElement> {\n  variant?: BgGlyphRainVariant;\n  chars?: string;\n  color?: string;\n  accent?: string;\n  background?: string;\n  fontSize?: number;\n  density?: number;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-ve5 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-ve5-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-ve5-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nconst sets: Record<BgGlyphRainVariant, string> = {\n  hex: \"0123456789ABCDEF\",\n  binary: \"01\",\n  blocks: \"▁▂▃▄▅▆▇█▏▎▍▌▋▊▉\",\n  symbols: \"+-=<>[]{}()/\\\\|*#%&$@;:~^\",\n};\n\nfunction rgbOf(value: string, fallback: [number, number, number]): [number, number, number] {\n  const raw = value.trim().replace(\"#\", \"\");\n  const hex = raw.length === 3 ? raw.split(\"\").map((c) => c + c).join(\"\") : raw;\n  const n = parseInt(hex.slice(0, 6), 16);\n  if (hex.length < 6 || Number.isNaN(n)) return fallback;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nfunction hash(i: number) {\n  let x = Math.imul(i ^ 0x7f4a7c15, 0x2c1b3c6d);\n  x ^= x >>> 12;\n  x = Math.imul(x, 0x297a2d39);\n  x ^= x >>> 15;\n  return x >>> 0;\n}\n\ninterface Layer {\n  size: number;\n  alpha: number;\n  rate: number;\n  cols: number;\n  head: Float32Array;\n  vel: Float32Array;\n  len: Uint8Array;\n  wait: Float32Array;\n}\n\nexport function BgGlyphRain({\n  variant = \"hex\",\n  chars,\n  color = \"#2f7bff\",\n  accent = \"#e6f0ff\",\n  background = \"#04060b\",\n  fontSize = 14,\n  density = 1,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgGlyphRainProps) {\n  const wrapRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n\n  React.useEffect(() => {\n    const wrap = wrapRef.current;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!wrap || !canvas || !ctx) return;\n    const g: CanvasRenderingContext2D = ctx;\n    const el: HTMLDivElement = wrap;\n    const cv: HTMLCanvasElement = canvas;\n\n    const glyphs = Array.from(chars && chars.length ? chars : sets[variant] ?? sets.hex);\n    const cc = rgbOf(color, [47, 123, 255]);\n    const ch = rgbOf(accent, [230, 240, 255]);\n    const mono = getComputedStyle(document.documentElement).getPropertyValue(\"--font-mono\").trim();\n    const family = `${mono ? mono + \", \" : \"\"}ui-monospace, SFMono-Regular, Menlo, Consolas, monospace`;\n    const base = Math.max(8, fontSize);\n    const specs = [\n      { size: base * 0.62, alpha: 0.28, rate: 0.55 },\n      { size: base * 0.82, alpha: 0.55, rate: 0.8 },\n      { size: base * 1.12, alpha: 1, rate: 1.15 },\n    ];\n\n    let seed = 0x51ed;\n    function rand() {\n      seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;\n      return seed / 4294967296;\n    }\n\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let layers: Layer[] = [];\n\n    function reset(L: Layer, i: number, rows: number, first: boolean) {\n      L.len[i] = 6 + Math.floor(rand() * Math.min(26, rows * 0.7));\n      L.vel[i] = (5 + rand() * 9) * L.rate;\n      L.head[i] = first ? rand() * (rows + L.len[i]) : -rand() * rows * 0.4;\n      L.wait[i] = first ? 0 : rand() * 2.5;\n    }\n\n    function setup() {\n      seed = 0x51ed;\n      layers = specs.map((s) => {\n        const pitch = s.size * 1.05;\n        const cols = Math.max(1, Math.floor((w / pitch) * Math.min(1, 0.55 * density)));\n        const L: Layer = {\n          size: s.size,\n          alpha: s.alpha,\n          rate: s.rate,\n          cols,\n          head: new Float32Array(cols),\n          vel: new Float32Array(cols),\n          len: new Uint8Array(cols),\n          wait: new Float32Array(cols),\n        };\n        const rows = Math.ceil(h / (s.size * 1.2));\n        for (let i = 0; i < cols; i++) reset(L, i, rows, true);\n        return L;\n      });\n    }\n\n    function render(dt: number) {\n      if (!w || !h) return;\n      g.clearRect(0, 0, w, h);\n      g.textAlign = \"center\";\n      g.textBaseline = \"top\";\n      const scan = ((t * 0.12) % 1.3) * h - h * 0.15;\n      for (let li = 0; li < layers.length; li++) {\n        const L = layers[li];\n        const lh = L.size * 1.2;\n        const rows = Math.ceil(h / lh);\n        const pitch = w / L.cols;\n        g.font = `500 ${L.size.toFixed(1)}px ${family}`;\n        for (let i = 0; i < L.cols; i++) {\n          if (L.wait[i] > 0) {\n            L.wait[i] -= dt;\n            continue;\n          }\n          L.head[i] += L.vel[i] * dt;\n          const len = L.len[i];\n          if (L.head[i] - len > rows) {\n            reset(L, i, rows, false);\n            continue;\n          }\n          const x = (i + 0.5) * pitch + ((hash(i * 31 + li) % 7) - 3) * 0.6;\n          const top = Math.floor(L.head[i]);\n          const tick = Math.floor(t * 9 + (hash(i + li * 977) % 50));\n          for (let j = 0; j < len; j++) {\n            const row = top - j;\n            if (row < 0 || row > rows) continue;\n            const y = row * lh;\n            const fl = j < 3 ? tick : Math.floor(tick / (4 + (j % 5)));\n            const glyph = glyphs[hash(row * 7919 + i * 104729 + li * 31 + fl) % glyphs.length];\n            let a = (1 - j / len) ** 1.6 * L.alpha;\n            const sd = Math.abs(y - scan);\n            if (sd < 30) a = Math.min(1, a + (1 - sd / 30) * 0.35 * L.alpha);\n            if (j === 0) {\n              g.globalAlpha = Math.min(1, L.alpha * 1.1);\n              g.fillStyle = `rgb(${ch[0]},${ch[1]},${ch[2]})`;\n            } else {\n              g.globalAlpha = a;\n              const m = j < 3 ? 0.45 : 0;\n              g.fillStyle = `rgb(${Math.round(cc[0] + (ch[0] - cc[0]) * m)},${Math.round(cc[1] + (ch[1] - cc[1]) * m)},${Math.round(cc[2] + (ch[2] - cc[2]) * m)})`;\n            }\n            g.fillText(glyph, x, y);\n          }\n        }\n      }\n      g.globalAlpha = 1;\n    }\n\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    function still() {\n      t = 1;\n      setup();\n      render(0);\n    }\n\n    function resize() {\n      const nw = el.clientWidth;\n      const nh = el.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\n      cv.width = Math.max(1, Math.round(w * dpr));\n      cv.height = Math.max(1, Math.round(h * dpr));\n      g.setTransform(dpr, 0, 0, dpr, 0, 0);\n      setup();\n      if (motion.matches) still();\n      else render(0);\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.05, (now - prev) / 1000) * speed;\n      prev = now;\n      t += dt;\n      render(dt);\n      raf = requestAnimationFrame(frame);\n    }\n\n    function sync() {\n      const run = visible && !document.hidden && !motion.matches;\n      if (run && !raf) {\n        prev = performance.now();\n        raf = requestAnimationFrame(frame);\n      } else if (!run && raf) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n      if (motion.matches) still();\n    }\n\n    const ro = new ResizeObserver(resize);\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1].isIntersecting;\n      sync();\n    });\n    ro.observe(el);\n    io.observe(el);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", sync);\n    resize();\n    sync();\n\n    return () => {\n      if (raf) cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", sync);\n      motion.removeEventListener(\"change\", sync);\n    };\n  }, [variant, chars, color, accent, background, fontSize, density, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-ve5 ${className}`} style={{ background, ...style }} {...rest}>\n      <style href=\"hb-bg-glyph-rain\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-ve5-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-ve5-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}