{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-silk-threads",
  "type": "registry:component",
  "title": "Silk Threads",
  "description": "Dozens of long, thin threads flowing across the area in offset waves, with a highlight that slides along each one. The pointer combs through them and pushes them apart.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "threads",
    "lines",
    "waves",
    "comb",
    "canvas"
  ],
  "docs": "https://hyperblur.dev/effects/bg-silk-threads",
  "files": [
    {
      "path": "registry/hyperblur/bg-silk-threads.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-silk-threads.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgSilkThreadsProps extends React.HTMLAttributes<HTMLDivElement> {\n  count?: number;\n  color?: string;\n  accent?: string;\n  amplitude?: number;\n  thickness?: number;\n  push?: number;\n  radius?: number;\n  speed?: number;\n}\n\nconst css = `\n.hb-qq3 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-qq3-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-qq3-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction qq3Hash(n: number) {\n  let x = Math.imul(n | 0, 2654435761);\n  x ^= x >>> 15;\n  x = Math.imul(x, 2246822519);\n  x ^= x >>> 13;\n  return (x >>> 0) / 4294967296;\n}\n\nexport function BgSilkThreads({\n  count = 40,\n  color = \"#d9e2ff\",\n  accent = \"#2f7bff\",\n  amplitude = 18,\n  thickness = 1,\n  push = 34,\n  radius = 90,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgSilkThreadsProps) {\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 n = Math.max(4, Math.min(160, Math.round(count)));\n    const ph1 = new Float32Array(n);\n    const ph2 = new Float32Array(n);\n    const k1 = new Float32Array(n);\n    const k2 = new Float32Array(n);\n    const sp1 = new Float32Array(n);\n    const sp2 = new Float32Array(n);\n    const amp = new Float32Array(n);\n    const depth = new Float32Array(n);\n    const shine = new Float32Array(n);\n    const jitter = new Float32Array(n);\n    for (let i = 0; i < n; i++) {\n      const b = i * 11 + 3;\n      ph1[i] = i * 0.21 + qq3Hash(b) * 0.6;\n      ph2[i] = qq3Hash(b + 1) * Math.PI * 2;\n      k1[i] = 0.006 + qq3Hash(b + 2) * 0.004;\n      k2[i] = 0.013 + qq3Hash(b + 3) * 0.008;\n      sp1[i] = 0.5 + qq3Hash(b + 4) * 0.35;\n      sp2[i] = 0.3 + qq3Hash(b + 5) * 0.5;\n      amp[i] = 0.7 + qq3Hash(b + 6) * 0.6;\n      depth[i] = qq3Hash(b + 7);\n      shine[i] = qq3Hash(b + 8);\n      jitter[i] = qq3Hash(b + 9) - 0.5;\n    }\n    let disp = new Float32Array(0);\n\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 act = 0;\n    let placed = false;\n    let step = 6;\n    let pts = 0;\n\n    function idleX(tt: number) {\n      return w * (0.5 + 0.38 * Math.sin(tt * 0.33 + 1.1));\n    }\n    function idleY(tt: number) {\n      return h * (0.5 + 0.3 * Math.sin(tt * 0.58 + 0.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 ? 10 : 2.2));\n      px += (gx - px) * k;\n      py += (gy - py) * k;\n      act += ((inside ? 1 : 0) - act) * (1 - Math.exp(-dt * 3));\n    }\n\n    function base(i: number, x: number) {\n      const top = h * 0.12;\n      const span = h * 0.76;\n      const y0 = top + ((i + 0.5 + jitter[i] * 0.6) / n) * span;\n      const a = amplitude * amp[i];\n      return (\n        y0 +\n        a * Math.sin(x * k1[i] + t * sp1[i] + ph1[i]) +\n        a * 0.45 * Math.sin(x * k2[i] - t * sp2[i] + ph2[i]) +\n        a * 0.6 * Math.sin(x * 0.0035 + t * 0.21)\n      );\n    }\n\n    function draw(dt: number) {\n      if (!ctx || !w || !h) return;\n      ctx.clearRect(0, 0, w, h);\n      ctx.globalCompositeOperation = \"lighter\";\n      ctx.lineCap = \"round\";\n      ctx.lineJoin = \"round\";\n      const R = Math.max(20, radius);\n      const sigma2 = 2 * R * R * 0.45;\n      const force = push * (0.45 + 0.55 * act);\n      const soft = R * 0.22;\n      const ease = dt > 0 ? 1 - Math.exp(-dt * 9) : 1;\n      for (let i = 0; i < n; i++) {\n        const d = depth[i];\n        const band = (((t * 0.045 * (0.6 + d)) + shine[i]) % 1.4) - 0.2;\n        const grad = ctx.createLinearGradient(0, 0, w, 0);\n        const lo = Math.max(0, Math.min(1, band - 0.18));\n        const mid = Math.max(0, Math.min(1, band));\n        const hi = Math.max(0, Math.min(1, band + 0.18));\n        const tone = i % 3 === 0 ? accent : color;\n        grad.addColorStop(0, tone);\n        grad.addColorStop(lo, tone);\n        grad.addColorStop(mid, \"#ffffff\");\n        grad.addColorStop(hi, tone);\n        grad.addColorStop(1, tone);\n        ctx.strokeStyle = grad;\n        ctx.globalAlpha = 0.14 + d * 0.32;\n        ctx.lineWidth = thickness * (0.5 + d * 0.9);\n        ctx.beginPath();\n        for (let p = 0; p < pts; p++) {\n          const x = p * step - step;\n          const y = base(i, x);\n          const dx = x - px;\n          const dy = y - py;\n          const g = Math.exp(-(dx * dx) / sigma2);\n          const target = (dy / Math.sqrt(dy * dy + soft * soft)) * force * g * Math.exp(-Math.abs(dy) / (R * 1.4));\n          const o = i * pts + p;\n          disp[o] += (target - disp[o]) * ease;\n          const yy = y + disp[o];\n          if (p === 0) ctx.moveTo(x, yy);\n          else ctx.lineTo(x, yy);\n        }\n        ctx.stroke();\n      }\n      ctx.globalAlpha = 1;\n      ctx.globalCompositeOperation = \"destination-out\";\n      const fade = ctx.createLinearGradient(0, 0, w, 0);\n      fade.addColorStop(0, \"rgba(0,0,0,1)\");\n      fade.addColorStop(0.1, \"rgba(0,0,0,0)\");\n      fade.addColorStop(0.9, \"rgba(0,0,0,0)\");\n      fade.addColorStop(1, \"rgba(0,0,0,1)\");\n      ctx.fillStyle = fade;\n      ctx.fillRect(0, 0, w, h);\n      ctx.globalCompositeOperation = \"source-over\";\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      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      step = Math.max(5, w / 140);\n      pts = Math.ceil(w / step) + 3;\n      disp = new Float32Array(n * pts);\n      if (motion.matches) still();\n      else {\n        track(0);\n        draw(0);\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 = 2.5;\n      inside = false;\n      placed = false;\n      act = 0;\n      track(0);\n      draw(0);\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(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    function move(e: PointerEvent) {\n      if (!wrap || motion.matches) return;\n      const rect = wrap.getBoundingClientRect();\n      const kx = rect.width ? wrap.clientWidth / rect.width : 1;\n      const ky = rect.height ? wrap.clientHeight / rect.height : 1;\n      tx = (e.clientX - rect.left) * kx;\n      ty = (e.clientY - rect.top) * ky;\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  }, [count, color, accent, amplitude, thickness, push, radius, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qq3 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-silk-threads\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qq3-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-qq3-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}