{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-glitch-letters",
  "type": "registry:component",
  "title": "Glitch Letters",
  "description": "A wall of monospaced characters that keep flickering into new glyphs and colors, with short glitch bursts along a row. Letters brighten around the pointer.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "letters",
    "glitch",
    "terminal",
    "canvas",
    "pointer"
  ],
  "docs": "https://hyperblur.dev/effects/bg-glitch-letters",
  "files": [
    {
      "path": "registry/hyperblur/bg-glitch-letters.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-glitch-letters.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgGlitchLettersProps extends React.HTMLAttributes<HTMLDivElement> {\n  characters?: string;\n  colors?: string[];\n  fontSize?: number;\n  fontFamily?: string;\n  rate?: number;\n  radius?: number;\n  dim?: number;\n  seed?: number;\n}\n\nconst css = `\n.hb-qj8 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }\n.hb-qj8-c { display: block; width: 100%; height: 100%; }\n`;\n\nconst defaultColors = [\"#ededed\", \"#2f7bff\", \"#7fb3ff\", \"#ff5c8a\"];\n\nfunction rnd(seed: number, i: number) {\n  let x = (Math.imul(i + 1, 0x9e3779b1) + Math.imul(seed | 0, 0x85ebca6b)) >>> 0;\n  x = Math.imul((x >>> 16) ^ x, 0x45d9f3b) >>> 0;\n  x = Math.imul((x >>> 16) ^ x, 0x45d9f3b) >>> 0;\n  x = ((x >>> 16) ^ x) >>> 0;\n  return x / 4294967296;\n}\n\nexport function BgGlitchLetters({\n  characters = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#$%&*+=<>/\",\n  colors = defaultColors,\n  fontSize = 15,\n  fontFamily = \"ui-monospace, SFMono-Regular, Menlo, Consolas, monospace\",\n  rate = 1,\n  radius = 120,\n  dim = 0.2,\n  seed = 9,\n  className = \"\",\n  style,\n  ...rest\n}: BgGlitchLettersProps) {\n  const wrapRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const palette = colors.length ? colors.join(\"|\") : defaultColors.join(\"|\");\n\n  React.useEffect(() => {\n    const wrap = wrapRef.current;\n    const host = wrap?.parentElement;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    const layer = document.createElement(\"canvas\");\n    const lctx = layer.getContext(\"2d\");\n    if (!wrap || !host || !canvas || !ctx || !lctx) return;\n\n    const glyphs = Array.from(characters.length ? characters : \"01\");\n    const tones = palette.split(\"|\");\n    const size = Math.max(8, fontSize);\n    const cw = Math.ceil(size * 0.66);\n    const ch = Math.ceil(size * 1.3);\n    const font = `${size}px ${fontFamily}`;\n    const R = Math.max(20, radius);\n    const tickLen = 1 / (16 * Math.max(0.05, rate));\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let inside = false;\n    let tx = 0;\n    let ty = 0;\n    let px = 0;\n    let py = 0;\n    let placed = false;\n    let cols = 0;\n    let rows = 0;\n    let n = 0;\n    let glyph = new Uint16Array(0);\n    let tone = new Uint8Array(0);\n    let flash = new Float32Array(0);\n    let active: number[] = [];\n    let tick = 0;\n    let clock = 0;\n    let burstClock = 0;\n    let bursts = 0;\n\n    function idleX(tt: number) {\n      return w * (0.5 + 0.34 * Math.sin(tt * 0.39 + 0.5));\n    }\n    function idleY(tt: number) {\n      return h * (0.5 + 0.3 * Math.sin(tt * 0.61 + 2));\n    }\n\n    function track(dt: number) {\n      const gx = inside ? tx : idleX(t);\n      const gy = inside ? ty : idleY(t);\n      if (!placed) {\n        px = gx;\n        py = gy;\n        placed = true;\n        return;\n      }\n      const k = 1 - Math.exp(-dt * (inside ? 12 : 2.4));\n      px += (gx - px) * k;\n      py += (gy - py) * k;\n    }\n\n    function pickTone(v: number) {\n      if (tones.length < 2 || v < 0.82) return 0;\n      return 1 + Math.floor(((v - 0.82) / 0.18) * (tones.length - 1)) % (tones.length - 1);\n    }\n\n    function paint(i: number) {\n      if (!lctx) return;\n      const c = i % cols;\n      const r = Math.floor(i / cols);\n      const x = c * cw;\n      const y = r * ch;\n      lctx.clearRect(x, y, cw, ch);\n      lctx.globalAlpha = tone[i] === 0 ? dim : Math.min(1, dim * 2.2);\n      lctx.fillStyle = tones[tone[i]];\n      lctx.fillText(glyphs[glyph[i]], x + cw / 2, y + ch / 2);\n    }\n\n    function layout() {\n      cols = Math.ceil(w / cw) + 1;\n      rows = Math.ceil(h / ch) + 1;\n      n = cols * rows;\n      glyph = new Uint16Array(n);\n      tone = new Uint8Array(n);\n      flash = new Float32Array(n);\n      active = [];\n      tick = 0;\n      bursts = 0;\n      for (let i = 0; i < n; i++) {\n        glyph[i] = Math.floor(rnd(seed, i * 3 + 1) * glyphs.length);\n        tone[i] = pickTone(rnd(seed, i * 3 + 2));\n      }\n      if (!lctx) return;\n      lctx.setTransform(1, 0, 0, 1, 0, 0);\n      lctx.clearRect(0, 0, layer.width, layer.height);\n      lctx.setTransform(layer.width / w, 0, 0, layer.height / h, 0, 0);\n      lctx.font = font;\n      lctx.textAlign = \"center\";\n      lctx.textBaseline = \"middle\";\n      for (let i = 0; i < n; i++) paint(i);\n    }\n\n    function change(i: number, k: number) {\n      glyph[i] = Math.floor(rnd(seed, k * 2 + 1) * glyphs.length);\n      tone[i] = pickTone(rnd(seed, k * 2 + 2));\n      if (flash[i] <= 0) active.push(i);\n      flash[i] = 1;\n      paint(i);\n    }\n\n    function step(dt: number) {\n      clock += dt;\n      burstClock += dt;\n      while (clock >= tickLen) {\n        clock -= tickLen;\n        tick++;\n        const amount = Math.max(1, Math.floor(n * 0.006));\n        for (let j = 0; j < amount; j++) {\n          const key = tick * 7919 + j;\n          change(Math.floor(rnd(seed + 17, key) * n), key);\n        }\n      }\n      if (burstClock > 2.4 / Math.max(0.05, rate)) {\n        burstClock = 0;\n        bursts++;\n        const row = Math.floor(rnd(seed + 31, bursts) * rows);\n        const start = Math.floor(rnd(seed + 37, bursts) * cols);\n        const len = 6 + Math.floor(rnd(seed + 41, bursts) * 10);\n        for (let c = start; c < Math.min(cols, start + len); c++) change(row * cols + c, 900000 + bursts * 64 + c);\n      }\n      const keep: number[] = [];\n      for (const i of active) {\n        flash[i] -= dt * 2.2;\n        if (flash[i] > 0) keep.push(i);\n        else flash[i] = 0;\n      }\n      active = keep;\n    }\n\n    function draw() {\n      if (!ctx || !w || !h) return;\n      ctx.clearRect(0, 0, w, h);\n      ctx.globalAlpha = 1;\n      ctx.drawImage(layer, 0, 0, w, h);\n      ctx.font = font;\n      ctx.textAlign = \"center\";\n      ctx.textBaseline = \"middle\";\n      for (const i of active) {\n        ctx.globalAlpha = flash[i] * 0.75;\n        ctx.fillStyle = tones[tone[i]];\n        ctx.fillText(glyphs[glyph[i]], (i % cols) * cw + cw / 2, Math.floor(i / cols) * ch + ch / 2);\n      }\n      const c0 = Math.max(0, Math.floor((px - R) / cw));\n      const c1 = Math.min(cols - 1, Math.ceil((px + R) / cw));\n      const r0 = Math.max(0, Math.floor((py - R) / ch));\n      const r1 = Math.min(rows - 1, Math.ceil((py + R) / ch));\n      for (let r = r0; r <= r1; r++) {\n        for (let c = c0; c <= c1; c++) {\n          const x = c * cw + cw / 2;\n          const y = r * ch + ch / 2;\n          const dx = x - px;\n          const dy = y - py;\n          const d = Math.sqrt(dx * dx + dy * dy);\n          if (d >= R) continue;\n          const k = 1 - d / R;\n          const i = r * cols + c;\n          ctx.globalAlpha = Math.min(1, k * k * 0.95);\n          ctx.fillStyle = tone[i] === 0 ? \"#ffffff\" : tones[tone[i]];\n          ctx.fillText(glyphs[glyph[i]], x, y);\n        }\n      }\n      ctx.globalAlpha = 1;\n    }\n\n    function resize() {\n      if (!wrap || !canvas || !ctx) return;\n      const nw = wrap.clientWidth;\n      const nh = wrap.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\n      canvas.width = Math.max(1, Math.round(w * dpr));\n      canvas.height = Math.max(1, Math.round(h * dpr));\n      layer.width = canvas.width;\n      layer.height = canvas.height;\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      layout();\n      if (motion.matches) still();\n      else {\n        track(0);\n        draw();\n      }\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.8;\n      inside = false;\n      placed = false;\n      track(0);\n      draw();\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.05, (now - prev) / 1000);\n      prev = now;\n      t += dt;\n      track(dt);\n      step(dt);\n      draw();\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    function move(e: PointerEvent) {\n      if (!host || motion.matches) return;\n      const rect = host.getBoundingClientRect();\n      const sx = rect.width ? host.clientWidth / rect.width : 1;\n      const sy = rect.height ? host.clientHeight / rect.height : 1;\n      tx = (e.clientX - rect.left) * sx;\n      ty = (e.clientY - rect.top) * sy;\n      inside = true;\n    }\n\n    function leave(e: PointerEvent) {\n      if (e.type === \"pointerup\" && e.pointerType === \"mouse\") return;\n      inside = false;\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(wrap);\n    io.observe(wrap);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", sync);\n    host.addEventListener(\"pointermove\", move, { passive: true });\n    host.addEventListener(\"pointerdown\", move, { passive: true });\n    host.addEventListener(\"pointerleave\", leave);\n    host.addEventListener(\"pointerup\", leave);\n    host.addEventListener(\"pointercancel\", leave);\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      host.removeEventListener(\"pointermove\", move);\n      host.removeEventListener(\"pointerdown\", move);\n      host.removeEventListener(\"pointerleave\", leave);\n      host.removeEventListener(\"pointerup\", leave);\n      host.removeEventListener(\"pointercancel\", leave);\n    };\n  }, [characters, palette, fontSize, fontFamily, rate, radius, dim, seed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qj8 ${className}`} style={style} aria-hidden=\"true\" {...rest}>\n      <style href=\"hb-bg-glitch-letters\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qj8-c\" />\n    </div>\n  );\n}\n"
    }
  ]
}