{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-faulty-terminal",
  "type": "registry:component",
  "title": "Faulty Terminal",
  "description": "A wall of scrolling terminal output that tears, glitches and flickers now and then. Glyphs near the pointer brighten and scramble.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "terminal",
    "glitch",
    "monospace",
    "canvas",
    "pointer"
  ],
  "docs": "https://hyperblur.dev/effects/bg-faulty-terminal",
  "files": [
    {
      "path": "registry/hyperblur/bg-faulty-terminal.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-faulty-terminal.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgFaultyTerminalProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  accent?: string;\n  background?: string;\n  fontSize?: number;\n  scroll?: number;\n  glitch?: number;\n  radius?: number;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-qp7 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-qp7-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-qp7-fx { position: absolute; inset: 0; background: repeating-linear-gradient(180deg, rgba(0,0,0,0) 0, rgba(0,0,0,0) 2px, rgba(0,0,0,0.22) 3px); }\n.hb-qp7-vg { position: absolute; inset: 0; background: radial-gradient(120% 90% at 50% 50%, rgba(0,0,0,0) 55%, rgba(0,0,0,0.7) 100%); }\n.hb-qp7-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nconst words = [\n  \"kernel\", \"mount\", \"socket\", \"cache\", \"route\", \"daemon\", \"fetch\", \"relay\", \"buffer\", \"shard\",\n  \"worker\", \"index\", \"vault\", \"proxy\", \"ledger\", \"beacon\", \"cipher\", \"packet\", \"thread\", \"queue\",\n];\nconst cmds = [\"sync\", \"deploy\", \"trace\", \"probe\", \"patch\", \"rollback\", \"scan\", \"attach\", \"flush\", \"tail\"];\nconst flags = [\"force\", \"depth=12\", \"region=eu-west\", \"dry-run\", \"verbose\", \"no-cache\", \"retries=5\", \"watch\"];\nconst states = [\"OK\", \"DONE\", \"PASS\", \"READY\", \"200\", \"ACK\"];\nconst noise = \"01#%&$@*+=<>/\\\\|{}[]ABCDEF0123456789:;\";\nconst blocks = \"█▓▒░\";\n\nfunction rnd(i: number) {\n  let x = Math.imul(i ^ 0x9e3779b9, 0x85ebca6b);\n  x ^= x >>> 13;\n  x = Math.imul(x, 0xc2b2ae35);\n  x ^= x >>> 16;\n  return (x >>> 0) / 4294967296;\n}\n\nfunction pick<T>(list: T[], seed: number) {\n  return list[Math.floor(rnd(seed) * list.length) % list.length];\n}\n\nfunction hex(n: number, len: number) {\n  return (n >>> 0).toString(16).padStart(len, \"0\").slice(-len);\n}\n\nfunction makeLine(i: number) {\n  const s = i * 41 + 7;\n  const kind = Math.floor(rnd(s) * 9);\n  if (kind === 0) {\n    const stamp = (i * 0.137 + 12).toFixed(3).padStart(9, \" \");\n    return { text: `[${stamp}] ${pick(words, s + 1)}: ${pick(words, s + 2)} ${pick(words, s + 3)} attached`, tone: 0.55 };\n  }\n  if (kind === 1) {\n    let bytes = \"\";\n    let ascii = \"\";\n    for (let b = 0; b < 8; b++) {\n      const v = Math.floor(rnd(s + 10 + b) * 256);\n      bytes += hex(v, 2) + \" \";\n      ascii += v > 32 && v < 127 ? String.fromCharCode(v) : \".\";\n    }\n    return { text: `0x${hex(i * 16, 8)}  ${bytes} ${ascii}`, tone: 0.4 };\n  }\n  if (kind === 2) {\n    return { text: `$ ${pick(cmds, s + 1)} ${pick(words, s + 2)} --${pick(flags, s + 3)}`, tone: 1 };\n  }\n  if (kind === 3) {\n    return { text: `WARN ${pick(words, s + 1)} latency ${Math.floor(rnd(s + 2) * 900 + 40)}ms on node-${Math.floor(rnd(s + 3) * 64)}`, tone: 0.8 };\n  }\n  if (kind === 4) {\n    const pct = Math.floor(rnd(s + 1) * 101);\n    const fill = Math.round(pct / 10);\n    return { text: `[${\"#\".repeat(fill)}${\".\".repeat(10 - fill)}] ${String(pct).padStart(3, \" \")}% ${pick(words, s + 2)}.tar`, tone: 0.65 };\n  }\n  if (kind === 5) {\n    return { text: `${pick(words, s + 1)}.${pick(cmds, s + 2)}(0x${hex(Math.floor(rnd(s + 3) * 65535), 4)}) -> ${pick(states, s + 4)}`, tone: 0.6 };\n  }\n  if (kind === 6) {\n    return { text: \"\", tone: 0 };\n  }\n  if (kind === 7) {\n    return { text: `  ${pick(words, s + 1)}/${pick(words, s + 2)}/${hex(Math.floor(rnd(s + 3) * 4294967295), 8)}  ${Math.floor(rnd(s + 4) * 9000 + 100)} kb`, tone: 0.45 };\n  }\n  return { text: `>> handshake ${hex(Math.floor(rnd(s + 1) * 4294967295), 8)} accepted by ${pick(words, s + 2)}`, tone: 0.75 };\n}\n\nexport function BgFaultyTerminal({\n  color = \"#33ff88\",\n  accent = \"#e8fff0\",\n  background = \"#020604\",\n  fontSize = 13,\n  scroll = 1.4,\n  glitch = 1,\n  radius = 90,\n  speed = 1,\n  children,\n  className = \"\",\n  style,\n  ...rest\n}: BgFaultyTerminalProps) {\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 host = wrap?.parentElement;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!wrap || !host || !canvas || !ctx) return;\n\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    const size = Math.max(8, fontSize);\n    const font = `${size}px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace`;\n    const lineH = Math.round(size * 1.5);\n    const R = Math.max(20, radius);\n    const cache = new Map<number, { text: string; tone: number }>();\n\n    let w = 0;\n    let h = 0;\n    let dpr = 1;\n    let charW = size * 0.6;\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 near = 0;\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    function line(i: number) {\n      let v = cache.get(i);\n      if (!v) {\n        if (cache.size > 600) cache.clear();\n        v = makeLine(i);\n        cache.set(i, v);\n      }\n      return v;\n    }\n\n    function idleX(tt: number) {\n      return w * (0.5 + 0.36 * Math.sin(tt * 0.37 + 0.9));\n    }\n    function idleY(tt: number) {\n      return h * (0.5 + 0.3 * Math.sin(tt * 0.59 + 2.4));\n    }\n\n    function track(dt: number) {\n      const gx = inside ? tx : idleX(t);\n      const gy = inside ? ty : idleY(t);\n      if (!placed || dt === 0) {\n        px = gx;\n        py = gy;\n        placed = true;\n        near = inside ? 1 : 0.6;\n        return;\n      }\n      const k = 1 - Math.exp(-dt * (inside ? 14 : 2.5));\n      px += (gx - px) * k;\n      py += (gy - py) * k;\n      near += ((inside ? 1 : 0.6) - near) * Math.min(1, dt * 4);\n    }\n\n    function draw() {\n      if (!w || !h) return;\n      ctx!.globalCompositeOperation = \"source-over\";\n      ctx!.globalAlpha = 1;\n      ctx!.fillStyle = background;\n      ctx!.fillRect(0, 0, w, h);\n      ctx!.font = font;\n      ctx!.textBaseline = \"middle\";\n\n      const bucket = Math.floor(t * 4);\n      const gOn = rnd(bucket * 31 + 7) < 0.18 * glitch;\n      const bandY = rnd(bucket * 31 + 8) * h;\n      const bandH = lineH * (1 + Math.floor(rnd(bucket * 31 + 9) * 4));\n      const shift = (rnd(bucket * 31 + 10) - 0.5) * size * 7;\n      const flickBucket = Math.floor(t * 12);\n      const flick = rnd(flickBucket * 13 + 5) < 0.04 * glitch ? 0.55 : 0.93 + 0.07 * Math.sin(t * 50);\n      const scrambleTick = Math.floor(t * 16);\n\n      const offset = t * scroll * lineH;\n      const first = Math.floor(offset / lineH);\n      const frac = offset - first * lineH;\n      const rows = Math.ceil(h / lineH) + 2;\n      const x0 = Math.max(8, size * 0.9);\n\n      for (let r = 0; r < rows; r++) {\n        const idx = first + r;\n        const ln = line(idx);\n        const y = r * lineH - frac + lineH * 0.5;\n        if (!ln.text) continue;\n        let text = ln.text;\n        const inBand = gOn && y > bandY - lineH * 0.5 && y < bandY + bandH;\n        if (inBand) {\n          const chars = text.split(\"\");\n          for (let c = 0; c < chars.length; c++) {\n            if (rnd(idx * 131 + c * 7 + bucket) < 0.18) chars[c] = blocks[Math.floor(rnd(idx + c + bucket * 3) * 4)];\n          }\n          text = chars.join(\"\");\n        }\n        const base = (0.32 + 0.6 * ln.tone) * flick;\n        const dy = y - py;\n        let c0 = -1;\n        let c1 = -2;\n        if (Math.abs(dy) < R) {\n          const half = Math.sqrt(R * R - dy * dy);\n          c0 = Math.max(0, Math.floor((px - half - x0) / charW));\n          c1 = Math.min(text.length - 1, Math.ceil((px + half - x0) / charW));\n        }\n        let plain = text;\n        if (c1 >= c0) plain = text.slice(0, c0) + \" \".repeat(c1 - c0 + 1) + text.slice(c1 + 1);\n\n        if (inBand) {\n          ctx!.globalCompositeOperation = \"lighter\";\n          ctx!.globalAlpha = 0.5 * base;\n          ctx!.fillStyle = \"#ff2a55\";\n          ctx!.fillText(plain, x0 - 2, y);\n          ctx!.fillStyle = \"#2ad4ff\";\n          ctx!.fillText(plain, x0 + 2, y);\n          ctx!.globalCompositeOperation = \"source-over\";\n        }\n        ctx!.globalAlpha = base;\n        ctx!.fillStyle = color;\n        ctx!.fillText(plain, x0, y);\n\n        if (c1 >= c0) {\n          const maxC = Math.min(c1, Math.ceil((px + R - x0) / charW));\n          for (let c = c0; c <= maxC; c++) {\n            const cx = x0 + c * charW + charW * 0.5;\n            const d = Math.hypot(cx - px, dy);\n            const k = Math.max(0, 1 - d / R) * near;\n            let ch = c < text.length ? text[c] : \" \";\n            const roll = rnd(idx * 977 + c * 31 + scrambleTick);\n            if (ch !== \" \" ? roll < k * 0.85 : roll < k * 0.25) {\n              ch = noise[Math.floor(rnd(idx * 59 + c * 13 + scrambleTick * 7) * noise.length) % noise.length];\n            }\n            if (ch === \" \") continue;\n            ctx!.globalAlpha = Math.min(1, base + (1 - base) * k * 1.4);\n            ctx!.fillStyle = k > 0.45 ? accent : color;\n            ctx!.fillText(ch, x0 + c * charW, y);\n          }\n        }\n      }\n\n      if (gOn) {\n        const sy = Math.max(0, Math.round(bandY * dpr));\n        const sh = Math.max(1, Math.min(canvas!.height - sy, Math.round(bandH * dpr)));\n        if (sh > 1) {\n          ctx!.globalAlpha = 1;\n          ctx!.drawImage(canvas!, 0, sy, canvas!.width, sh, shift, sy / dpr, w, sh / dpr);\n        }\n      }\n\n      const glow = ctx!.createRadialGradient(px, py, 0, px, py, R * 1.4);\n      glow.addColorStop(0, color);\n      glow.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx!.globalCompositeOperation = \"lighter\";\n      ctx!.globalAlpha = 0.09 * near;\n      ctx!.fillStyle = glow;\n      ctx!.fillRect(px - R * 1.4, py - R * 1.4, R * 2.8, R * 2.8);\n      ctx!.globalAlpha = 1;\n      ctx!.globalCompositeOperation = \"source-over\";\n    }\n\n    function still() {\n      t = 3.3;\n      inside = false;\n      placed = false;\n      track(0);\n      draw();\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      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      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.font = font;\n      charW = ctx.measureText(\"M\").width || size * 0.6;\n      if (motion.matches) still();\n      else {\n        track(0);\n        draw();\n      }\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.033, (now - prev) / 1000);\n      prev = now;\n      t += dt * speed;\n      track(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  }, [color, accent, background, fontSize, scroll, glitch, radius, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qp7 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-faulty-terminal\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qp7-c\" aria-hidden=\"true\" />\n      <div className=\"hb-qp7-fx\" aria-hidden=\"true\" />\n      <div className=\"hb-qp7-vg\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-qp7-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}