{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-ghost-fibers",
  "type": "registry:component",
  "title": "Ghost Fibers",
  "description": "Translucent strands that drift on smooth noise and leave faint trails behind them. Strands thicken and glow where the pointer passes.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "fibers",
    "noise",
    "trails",
    "pointer",
    "canvas"
  ],
  "docs": "https://hyperblur.dev/effects/bg-ghost-fibers",
  "files": [
    {
      "path": "registry/hyperblur/bg-ghost-fibers.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-ghost-fibers.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgGhostFibersProps extends React.HTMLAttributes<HTMLDivElement> {\n  count?: number;\n  color?: string;\n  accent?: string;\n  thickness?: number;\n  amplitude?: number;\n  trail?: number;\n  radius?: number;\n  seed?: number;\n  speed?: number;\n}\n\nconst css = `\n.hb-qo3 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-qo3-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-qo3-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction hash2(x: number, y: number, s: number) {\n  let n = (Math.imul(x, 374761393) + Math.imul(y, 668265263) + Math.imul(s, 1442695041)) | 0;\n  n = Math.imul(n ^ (n >>> 13), 1274126177);\n  return ((n ^ (n >>> 16)) >>> 0) / 4294967295;\n}\n\nfunction noise2(x: number, y: number, s: number) {\n  const xi = Math.floor(x);\n  const yi = Math.floor(y);\n  const xf = x - xi;\n  const yf = y - yi;\n  const u = xf * xf * (3 - 2 * xf);\n  const v = yf * yf * (3 - 2 * yf);\n  const a = hash2(xi, yi, s);\n  const b = hash2(xi + 1, yi, s);\n  const c = hash2(xi, yi + 1, s);\n  const d = hash2(xi + 1, yi + 1, s);\n  return a + (b - a) * u + (c - a) * v + (a - b - c + d) * u * v;\n}\n\nexport function BgGhostFibers({\n  count = 22,\n  color = \"rgba(200,220,255,0.55)\",\n  accent = \"#7fb3ff\",\n  thickness = 1,\n  amplitude = 0.28,\n  trail = 3,\n  radius = 120,\n  seed = 7,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgGhostFibersProps) {\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 context = canvas?.getContext(\"2d\");\n    if (!wrap || !host || !canvas || !context) return;\n    const g = context;\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n\n    const N = Math.max(1, Math.min(80, Math.round(count)));\n    const R = Math.max(20, radius);\n    const ghosts = Math.max(0, Math.min(6, Math.round(trail)));\n    const s0 = Math.round(seed) | 0;\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 hover = 0;\n    let placed = false;\n    let M = 40;\n    let xs = new Float32Array(0);\n    let ys = new Float32Array(0);\n    let gs = new Float32Array(0);\n\n    const idleX = (tt: number) => w * (0.5 + 0.36 * Math.sin(tt * 0.31 + 2.2));\n    const idleY = (tt: number) => h * (0.5 + 0.3 * Math.sin(tt * 0.53 + 0.8));\n\n    const 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      } else {\n        const e = 1 - Math.exp(-dt * (inside ? 7 : 1.8));\n        px += (gx - px) * e;\n        py += (gy - py) * e;\n      }\n      hover += ((inside ? 1 : 0) - hover) * Math.min(1, dt * 4);\n    };\n\n    const layout = () => {\n      M = Math.max(24, Math.min(110, Math.round(w / 12)));\n      xs = new Float32Array(M);\n      ys = new Float32Array(M);\n      gs = new Float32Array(M);\n    };\n\n    const shape = (f: number, T: number, live: boolean) => {\n      const base = h * (0.04 + (0.92 * (f + 0.5)) / N + (hash2(f, 3, s0) - 0.5) * (0.6 / N));\n      const amp = h * amplitude;\n      const inv = 1 / (R * R);\n      const pull = 0.6 + 0.4 * hover;\n      for (let j = 0; j < M; j++) {\n        const x0 = -0.06 * w + (j * 1.12 * w) / (M - 1);\n        const nx = x0 * 0.0045 - T * 0.05 + f * 7.31;\n        const ny = T * 0.11 + f * 1.73;\n        const n1 = noise2(nx, ny, s0) * 2 - 1;\n        const n2 = noise2(nx * 2.3 + 4.1, ny * 1.4 + 9.2, s0 + 11) * 2 - 1;\n        let x = x0 + n2 * 10;\n        let y = base + amp * (0.75 * n1 + 0.25 * n2);\n        let gp = 0;\n        if (live) {\n          const dx = px - x;\n          const dy = py - y;\n          gp = Math.exp(-(dx * dx + dy * dy) * inv) * pull;\n          x += dx * gp * 0.12;\n          y += dy * gp * 0.3;\n        }\n        xs[j] = x;\n        ys[j] = y;\n        gs[j] = gp;\n      }\n    };\n\n    const path = (a: number, b: number) => {\n      g.beginPath();\n      g.moveTo(xs[a], ys[a]);\n      for (let j = a + 1; j < b; j++) {\n        const mx = (xs[j] + xs[j + 1]) / 2;\n        const my = (ys[j] + ys[j + 1]) / 2;\n        g.quadraticCurveTo(xs[j], ys[j], mx, my);\n      }\n      g.lineTo(xs[b], ys[b]);\n    };\n\n    const draw = () => {\n      if (!w || !h) return;\n      g.clearRect(0, 0, w, h);\n      g.globalCompositeOperation = \"lighter\";\n      g.lineCap = \"round\";\n      g.lineJoin = \"round\";\n      for (let f = 0; f < N; f++) {\n        const tint = hash2(f, 1, s0) < 0.22;\n        const alpha = (0.22 + 0.5 * hash2(f, 2, s0)) * (0.65 + 0.35 * Math.sin(t * 0.5 + f * 1.3));\n        g.strokeStyle = tint ? accent : color;\n        g.lineWidth = thickness;\n        for (let k = ghosts; k >= 1; k--) {\n          shape(f, t - k * 0.55, false);\n          path(0, M - 1);\n          g.globalAlpha = alpha * 0.32 * Math.pow(0.6, k - 1);\n          g.stroke();\n        }\n        shape(f, t, true);\n        path(0, M - 1);\n        g.globalAlpha = alpha;\n        g.stroke();\n        g.strokeStyle = accent;\n        for (let j = 0; j < M - 1; j++) {\n          const gp = (gs[j] + gs[j + 1]) / 2;\n          if (gp < 0.03) continue;\n          g.beginPath();\n          g.moveTo(xs[j], ys[j]);\n          g.lineTo(xs[j + 1], ys[j + 1]);\n          g.lineWidth = thickness * (1 + 4.5 * gp);\n          g.globalAlpha = Math.min(1, gp * (0.45 + alpha));\n          g.stroke();\n        }\n      }\n      g.globalAlpha = 1;\n      g.globalCompositeOperation = \"source-over\";\n    };\n\n    const resize = () => {\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      g.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    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    const still = () => {\n      t = 4.2;\n      inside = false;\n      placed = false;\n      hover = 0;\n      track(0);\n      draw();\n    };\n\n    const 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    const 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 move = (e: PointerEvent) => {\n      if (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    const 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  }, [count, color, accent, thickness, amplitude, trail, radius, seed, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qo3 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-ghost-fibers\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qo3-c\" aria-hidden=\"true\" />\n      {children != null && <div className=\"hb-qo3-in\">{children}</div>}\n    </div>\n  );\n}\n"
    }
  ]
}