{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-web-threads",
  "type": "registry:component",
  "title": "Web Threads",
  "description": "A spider web strung to the edges with a knot in the middle. Moving the pointer across a thread plucks it, and the vibration travels through the web before it settles.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "web",
    "spring",
    "physics",
    "pointer",
    "canvas"
  ],
  "docs": "https://hyperblur.dev/effects/bg-web-threads",
  "files": [
    {
      "path": "registry/hyperblur/bg-web-threads.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-web-threads.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgWebThreadsProps extends React.HTMLAttributes<HTMLDivElement> {\n  spokes?: number;\n  rings?: number;\n  color?: string;\n  accent?: string;\n  pluck?: number;\n  damping?: number;\n  breeze?: number;\n  seed?: number;\n  speed?: number;\n}\n\nconst css = `\n.hb-qo7 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-qo7-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-qo7-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction hashU(n: number, s: number) {\n  let x = (Math.imul(n, 2654435761) ^ Math.imul(s + 1, 1597334677)) | 0;\n  x = Math.imul(x ^ (x >>> 15), 2246822519);\n  x = Math.imul(x ^ (x >>> 13), 3266489917);\n  return ((x ^ (x >>> 16)) >>> 0) / 4294967295;\n}\n\nexport function BgWebThreads({\n  spokes = 14,\n  rings = 8,\n  color = \"rgba(255,255,255,0.22)\",\n  accent = \"#7fb3ff\",\n  pluck = 0.8,\n  damping = 2.5,\n  breeze = 1,\n  seed = 4,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgWebThreadsProps) {\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 S = Math.max(5, Math.min(32, Math.round(spokes)));\n    const K = Math.max(2, Math.min(20, Math.round(rings)));\n    const s0 = Math.round(seed) | 0;\n    const N = 1 + S * K;\n    const stiff = 18;\n    const couple = 120;\n    const maxD = 28;\n    const rx = new Float32Array(N);\n    const ry = new Float32Array(N);\n    const dx = new Float32Array(N);\n    const dy = new Float32Array(N);\n    const vx = new Float32Array(N);\n    const vy = new Float32Array(N);\n    const ax = new Float32Array(S);\n    const ay = new Float32Array(S);\n    const nbr: number[][] = [];\n    const segs: [number, number][] = [];\n    const node = (s: number, r: number) => 1 + (((s % S) + S) % S) * K + (r - 1);\n    for (let i = 0; i < N; i++) nbr.push([]);\n    for (let s = 0; s < S; s++) {\n      nbr[0].push(node(s, 1));\n      segs.push([0, node(s, 1)]);\n      for (let r = 1; r <= K; r++) {\n        const n = node(s, r);\n        nbr[n].push(r === 1 ? 0 : node(s, r - 1));\n        nbr[n].push(r === K ? -1 - s : node(s, r + 1));\n        nbr[n].push(node(s - 1, r));\n        nbr[n].push(node(s + 1, r));\n        segs.push([n, r === K ? -1 - s : node(s, r + 1)]);\n        segs.push([n, node(s + 1, r)]);\n      }\n    }\n\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let cx = 0;\n    let cy = 0;\n    let inside = false;\n    let tx = 0;\n    let ty = 0;\n    let p0x = 0;\n    let p0y = 0;\n    let fresh = true;\n\n    const idleX = (tt: number) => w * (0.5 + 0.44 * Math.sin(tt * 0.5 + 0.3));\n    const idleY = (tt: number) => h * (0.5 + 0.4 * Math.sin(tt * 0.83 + 1.1));\n\n    const layout = () => {\n      cx = w * 0.5;\n      cy = h * 0.47;\n      rx[0] = cx;\n      ry[0] = cy;\n      for (let s = 0; s < S; s++) {\n        const a = (s / S) * Math.PI * 2 + (hashU(s, s0) - 0.5) * (2.4 / S);\n        const ux = Math.cos(a);\n        const uy = Math.sin(a);\n        let tm = Infinity;\n        if (ux > 1e-6) tm = Math.min(tm, (w + 2 - cx) / ux);\n        if (ux < -1e-6) tm = Math.min(tm, (-2 - cx) / ux);\n        if (uy > 1e-6) tm = Math.min(tm, (h + 2 - cy) / uy);\n        if (uy < -1e-6) tm = Math.min(tm, (-2 - cy) / uy);\n        ax[s] = cx + ux * tm;\n        ay[s] = cy + uy * tm;\n        for (let r = 1; r <= K; r++) {\n          const f = 0.06 + 0.84 * Math.pow(r / K, 1.15) + (hashU(s * 97 + r, s0 + 1) - 0.5) * (0.5 / K);\n          const n = node(s, r);\n          rx[n] = cx + ux * tm * f;\n          ry[n] = cy + uy * tm * f;\n        }\n      }\n      dx.fill(0);\n      dy.fill(0);\n      vx.fill(0);\n      vy.fill(0);\n      fresh = true;\n    };\n\n    const px = (i: number) => (i < 0 ? ax[-1 - i] : rx[i] + dx[i]);\n    const py = (i: number) => (i < 0 ? ay[-1 - i] : ry[i] + dy[i]);\n\n    const kick = (i: number, jx: number, jy: number) => {\n      if (i < 0) return;\n      const m = i === 0 ? 0.35 : 1;\n      vx[i] += jx * m;\n      vy[i] += jy * m;\n    };\n\n    const cross = (x0: number, y0: number, x1: number, y1: number, dt: number, boost: number) => {\n      const mx = x1 - x0;\n      const my = y1 - y0;\n      if (mx * mx + my * my < 0.01 || dt <= 0) return;\n      let jx = (mx / dt) * boost;\n      let jy = (my / dt) * boost;\n      const len = Math.hypot(jx, jy);\n      if (len > 1400) {\n        jx *= 1400 / len;\n        jy *= 1400 / len;\n      }\n      jx *= pluck * 0.35;\n      jy *= pluck * 0.35;\n      for (let k = 0; k < segs.length; k++) {\n        const [a, b] = segs[k];\n        const sx0 = px(a);\n        const sy0 = py(a);\n        const ex = px(b) - sx0;\n        const ey = py(b) - sy0;\n        const den = mx * ey - my * ex;\n        if (Math.abs(den) < 1e-9) continue;\n        const qx = sx0 - x0;\n        const qy = sy0 - y0;\n        const tp = (qx * ey - qy * ex) / den;\n        const u = (qx * my - qy * mx) / den;\n        if (tp < 0 || tp > 1 || u < 0 || u > 1) continue;\n        kick(a, jx * (1 - u), jy * (1 - u));\n        kick(b, jx * u, jy * u);\n      }\n    };\n\n    const simulate = (dt: number) => {\n      const steps = Math.max(1, Math.ceil(dt / (1 / 120)));\n      const h1 = dt / steps;\n      for (let st = 0; st < steps; st++) {\n        for (let i = 0; i < N; i++) {\n          let sx = 0;\n          let sy = 0;\n          const list = nbr[i];\n          for (let k = 0; k < list.length; k++) {\n            const n = list[k];\n            if (n >= 0) {\n              sx += dx[n];\n              sy += dy[n];\n            }\n          }\n          const deg = list.length;\n          const wind = breeze * 9 * Math.sin(t * 0.7 + ry[i] * 0.012 + rx[i] * 0.004);\n          vx[i] += (-stiff * dx[i] - damping * vx[i] + couple * (sx - deg * dx[i]) * (i === 0 ? 0.25 : 1) + wind) * h1;\n          vy[i] += (-stiff * dy[i] - damping * vy[i] + couple * (sy - deg * dy[i]) * (i === 0 ? 0.25 : 1) + wind * 0.6) * h1;\n        }\n        for (let i = 0; i < N; i++) {\n          dx[i] = Math.max(-maxD, Math.min(maxD, dx[i] + vx[i] * h1));\n          dy[i] = Math.max(-maxD, Math.min(maxD, dy[i] + vy[i] * h1));\n        }\n      }\n    };\n\n    const pointer = (dt: number) => {\n      const gx = inside ? tx : idleX(t);\n      const gy = inside ? ty : idleY(t);\n      if (fresh) {\n        p0x = gx;\n        p0y = gy;\n        fresh = false;\n        return;\n      }\n      cross(p0x, p0y, gx, gy, dt, inside ? 1 : 6);\n      p0x = gx;\n      p0y = gy;\n    };\n\n    const segPath = (a: number, b: number) => {\n      const x0 = px(a);\n      const y0 = py(a);\n      const x1 = px(b);\n      const y1 = py(b);\n      g.moveTo(x0, y0);\n      const ringSeg = a > 0 && b > 0 && (a - 1) % K === (b - 1) % K;\n      if (ringSeg) {\n        const mx = (x0 + x1) / 2;\n        const my = (y0 + y1) / 2;\n        g.quadraticCurveTo(mx + (px(0) - mx) * 0.07, my + (py(0) - my) * 0.07, x1, y1);\n      } else g.lineTo(x1, y1);\n    };\n\n    const energy = (i: number) => (i < 0 ? 0 : Math.hypot(vx[i], vy[i]));\n\n    const draw = () => {\n      if (!w || !h) return;\n      g.clearRect(0, 0, w, h);\n      g.lineCap = \"round\";\n      g.strokeStyle = color;\n      g.lineWidth = 1;\n      g.beginPath();\n      for (let k = 0; k < segs.length; k++) segPath(segs[k][0], segs[k][1]);\n      g.stroke();\n      g.strokeStyle = accent;\n      for (let b = 1; b <= 4; b++) {\n        let any = false;\n        g.beginPath();\n        for (let k = 0; k < segs.length; k++) {\n          const [p, q] = segs[k];\n          const e = Math.min(1, (energy(p) + energy(q)) / 2 / 140);\n          if (e < 0.06 || Math.min(4, Math.ceil(e * 4)) !== b) continue;\n          segPath(p, q);\n          any = true;\n        }\n        if (any) {\n          g.globalAlpha = 0.2 + b * 0.2;\n          g.lineWidth = 1 + b * 0.25;\n          g.stroke();\n        }\n      }\n      g.globalAlpha = 1;\n      for (let i = 1; i < N; i++) {\n        if (hashU(i, s0 + 5) > 0.16) continue;\n        const tw = 0.35 + 0.35 * Math.sin(t * 1.7 + i * 2.3);\n        g.globalAlpha = tw;\n        g.fillStyle = \"#ffffff\";\n        g.beginPath();\n        g.arc(px(i), py(i), 1.2, 0, Math.PI * 2);\n        g.fill();\n      }\n      const ke = Math.min(1, energy(0) / 120);\n      const kx = px(0);\n      const ky = py(0);\n      const glow = g.createRadialGradient(kx, ky, 0, kx, ky, 18 + ke * 14);\n      glow.addColorStop(0, accent);\n      glow.addColorStop(1, \"rgba(0,0,0,0)\");\n      g.globalAlpha = 0.35 + ke * 0.4;\n      g.fillStyle = glow;\n      g.fillRect(kx - 40, ky - 40, 80, 80);\n      g.globalAlpha = 1;\n      g.fillStyle = accent;\n      g.beginPath();\n      g.arc(kx, ky, 2.8 + ke * 1.6, 0, Math.PI * 2);\n      g.fill();\n      g.globalAlpha = 1;\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 draw();\n    };\n\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    const still = () => {\n      inside = false;\n      dx.fill(0);\n      dy.fill(0);\n      vx.fill(0);\n      vy.fill(0);\n      t = 0;\n      fresh = true;\n      for (let s = 0; s < 90; s++) {\n        t += 1 / 60;\n        pointer(1 / 60);\n        simulate(1 / 60);\n      }\n      vx.fill(0);\n      vy.fill(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      pointer(dt);\n      simulate(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        fresh = true;\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      if (!inside) fresh = true;\n      inside = true;\n    };\n\n    const leave = (e: PointerEvent) => {\n      if (e.type === \"pointerup\" && e.pointerType === \"mouse\") return;\n      inside = false;\n      fresh = true;\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  }, [spokes, rings, color, accent, pluck, damping, breeze, seed, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qo7 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-web-threads\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qo7-c\" aria-hidden=\"true\" />\n      {children != null && <div className=\"hb-qo7-in\">{children}</div>}\n    </div>\n  );\n}\n"
    }
  ]
}