{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-helix-threads",
  "type": "registry:component",
  "title": "Helix Threads",
  "description": "Double-helix threads cross the frame at different depths, twisting continuously with base-pair rungs between the strands and depth shading on every segment.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "helix",
    "dna",
    "science",
    "canvas",
    "threads"
  ],
  "docs": "https://hyperblur.dev/effects/bg-helix-threads",
  "files": [
    {
      "path": "registry/hyperblur/bg-helix-threads.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-helix-threads.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgHelixThreadsProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  accent?: string;\n  background?: string;\n  threads?: number;\n  angle?: number;\n  twist?: number;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-ve15 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-ve15-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-ve15-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction rgbOf(value: string, fallback: [number, number, number]): [number, number, number] {\n  const raw = value.trim().replace(\"#\", \"\");\n  const hex = raw.length === 3 ? raw.split(\"\").map((c) => c + c).join(\"\") : raw;\n  const n = parseInt(hex.slice(0, 6), 16);\n  if (hex.length < 6 || Number.isNaN(n)) return fallback;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\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\nexport function BgHelixThreads({\n  color = \"#7fb3ff\",\n  accent = \"#ff7ad9\",\n  background = \"#05060b\",\n  threads = 3,\n  angle = -14,\n  twist = 1,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgHelixThreadsProps) {\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 canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!wrap || !canvas || !ctx) return;\n    const g: CanvasRenderingContext2D = ctx;\n    const el: HTMLDivElement = wrap;\n    const cv: HTMLCanvasElement = canvas;\n\n    const ca = rgbOf(color, [127, 179, 255]);\n    const cb = rgbOf(accent, [255, 122, 217]);\n    const n = Math.max(1, Math.min(8, Math.round(threads)));\n    const helices = Array.from({ length: n }, (_, i) => {\n      const z = n === 1 ? 1 : 0.45 + (i / (n - 1)) * 0.55;\n      return {\n        z,\n        off: n === 1 ? 0 : ((i + 0.5) / n - 0.5) * 1.2 + (rnd(i * 3 + 1) - 0.5) * 0.18,\n        tilt: (rnd(i * 3 + 2) - 0.5) * 0.22,\n        ph: rnd(i * 3 + 3) * 6.283,\n        dir: i % 2 ? -1 : 1,\n      };\n    });\n\n    let w = 0;\n    let h = 0;\n    let t = 0;\n\n    function rgba(c: [number, number, number], a: number) {\n      return `rgba(${c[0]},${c[1]},${c[2]},${Math.max(0, Math.min(1, a)).toFixed(3)})`;\n    }\n\n    function drawHelix(hx: (typeof helices)[number]) {\n      const th = (angle * Math.PI) / 180 + hx.tilt;\n      const ux = Math.cos(th);\n      const uy = Math.sin(th);\n      const nx = -uy;\n      const ny = ux;\n      const diag = Math.hypot(w, h);\n      const cx = w / 2 + nx * hx.off * h * 0.5;\n      const cy = h / 2 + ny * hx.off * h * 0.5;\n      const amp = Math.min(h * 0.11, 70) * hx.z;\n      const period = Math.max(120, Math.min(w, 900) * 0.42) * hx.z;\n      const k = (6.2832 / period) * Math.max(0.2, twist);\n      const flow = t * 0.9 * hx.dir + hx.ph;\n      const step = 5;\n      const half = diag / 2 + 40;\n      const count = Math.ceil((half * 2) / step);\n      const ax: number[] = [];\n      const ay: number[] = [];\n      const ad: number[] = [];\n      for (let i = 0; i <= count; i++) {\n        const s = -half + i * step;\n        const p = s * k + flow;\n        const sway = Math.sin(s * 0.004 + t * 0.3 + hx.ph) * amp * 0.5;\n        const bx = cx + ux * s + nx * sway;\n        const by = cy + uy * s + ny * sway;\n        ax.push(bx);\n        ay.push(by);\n        ad.push(p);\n      }\n      const baseA = 0.25 + hx.z * 0.75;\n      const lw = 0.8 + hx.z * 1.6;\n      const pt = (i: number, sgn: number): [number, number, number] => {\n        const sp = Math.sin(ad[i]) * sgn;\n        const dp = Math.cos(ad[i]) * sgn;\n        return [ax[i] + nx * sp * amp, ay[i] + ny * sp * amp, dp];\n      };\n      g.lineCap = \"round\";\n      for (const front of [false, true]) {\n        for (const sgn of [1, -1]) {\n          const col = sgn > 0 ? ca : cb;\n          let prev = pt(0, sgn);\n          for (let i = 1; i <= count; i++) {\n            const cur = pt(i, sgn);\n            const d = (prev[2] + cur[2]) / 2;\n            if (d > 0 === front) {\n              g.strokeStyle = rgba(col, baseA * (0.22 + 0.78 * (d * 0.5 + 0.5)));\n              g.lineWidth = lw * (0.6 + 0.6 * (d * 0.5 + 0.5));\n              g.beginPath();\n              g.moveTo(prev[0], prev[1]);\n              g.lineTo(cur[0], cur[1]);\n              g.stroke();\n            }\n            prev = cur;\n          }\n        }\n        if (!front) {\n          const rungEvery = 6.2832 / 10;\n          for (let i = 0; i <= count; i++) {\n            const p0 = ad[i] - flow;\n            const q = Math.floor(p0 / rungEvery);\n            const p1 = i < count ? ad[i + 1] - flow : p0;\n            if (Math.floor(p1 / rungEvery) === q) continue;\n            const a = pt(i, 1);\n            const b = pt(i, -1);\n            const depth = Math.abs(Math.sin(ad[i]));\n            const alpha = baseA * (0.18 + 0.4 * depth);\n            const grad = g.createLinearGradient(a[0], a[1], b[0], b[1]);\n            grad.addColorStop(0, rgba(ca, alpha));\n            grad.addColorStop(0.5, rgba([255, 255, 255], alpha * 0.35));\n            grad.addColorStop(1, rgba(cb, alpha));\n            g.strokeStyle = grad;\n            g.lineWidth = lw * 0.6;\n            g.beginPath();\n            g.moveTo(a[0], a[1]);\n            g.lineTo(b[0], b[1]);\n            g.stroke();\n            for (const e of [a, b]) {\n              const fr = e[2] * 0.5 + 0.5;\n              g.fillStyle = rgba(e === a ? ca : cb, baseA * (0.3 + fr * 0.7));\n              g.beginPath();\n              g.arc(e[0], e[1], (1 + fr * 1.6) * (0.6 + hx.z * 0.6), 0, 6.2832);\n              g.fill();\n            }\n          }\n        }\n      }\n    }\n\n    function render() {\n      if (!w || !h) return;\n      g.fillStyle = background;\n      g.fillRect(0, 0, w, h);\n      const glow = g.createRadialGradient(w * 0.5, h * 0.5, 0, w * 0.5, h * 0.5, Math.max(w, h) * 0.6);\n      glow.addColorStop(0, rgba(ca, 0.07));\n      glow.addColorStop(1, \"rgba(0,0,0,0)\");\n      g.fillStyle = glow;\n      g.fillRect(0, 0, w, h);\n      for (const hx of helices) drawHelix(hx);\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.5;\n      render();\n    }\n\n    function resize() {\n      const nw = el.clientWidth;\n      const nh = el.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\n      cv.width = Math.max(1, Math.round(w * dpr));\n      cv.height = Math.max(1, Math.round(h * dpr));\n      g.setTransform(dpr, 0, 0, dpr, 0, 0);\n      if (motion.matches) still();\n      else render();\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.05, (now - prev) / 1000);\n      prev = now;\n      t += dt * speed;\n      render();\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    const ro = new ResizeObserver(resize);\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1].isIntersecting;\n      sync();\n    });\n    ro.observe(el);\n    io.observe(el);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", sync);\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    };\n  }, [color, accent, background, threads, angle, twist, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-ve15 ${className}`} style={{ background, ...style }} {...rest}>\n      <style href=\"hb-bg-helix-threads\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-ve15-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-ve15-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}