{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"twine","type":"registry:ui","title":"Twine","description":"A bundle of thin glowing threads twisting like strings in the wind, bending toward the cursor.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/twine.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface TwineProps {\n  /** Number of threads. */\n  count?: number;\n  /** Color of the first thread. */\n  color?: string;\n  /** Color of the last thread; the rest blend between. */\n  accentColor?: string;\n  /** Wave height multiplier. */\n  amplitude?: number;\n  /** Wind speed multiplier; 0 freezes the motion. */\n  speed?: number;\n  /** How strongly threads bend toward the cursor, 0 to disable. */\n  attraction?: number;\n  /** Stroke width in px. */\n  thickness?: number;\n  className?: string;\n}\n\nfunction rgb(hex: string) {\n  let h = (hex || \"#000\").replace(\"#\", \"\");\n  if (h.length === 3) h = h.replace(/./g, \"$&$&\");\n  const n = parseInt(h.padEnd(6, \"0\").slice(0, 6), 16) || 0;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nexport function Twine({\n  count = 32,\n  color = \"#22d3ee\",\n  accentColor = \"#c084fc\",\n  amplitude = 1,\n  speed = 1,\n  attraction = 1,\n  thickness = 1,\n  className,\n}: TwineProps) {\n  const ref = useRef<HTMLCanvasElement>(null);\n  const opts = useRef({ count, color, accentColor, amplitude, speed, attraction, thickness });\n  const redraw = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = { count, color, accentColor, amplitude, speed, attraction, thickness };\n    redraw.current?.();\n  }, [count, color, accentColor, amplitude, speed, attraction, thickness]);\n\n  useEffect(() => {\n    const canvas = ref.current!;\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    // Smoothed pointer; `a` fades the pull in and out as the cursor enters and leaves.\n    const m = { x: 0, y: 0, tx: 0, ty: 0, a: 0, ta: 0 };\n    let w = 0;\n    let h = 0;\n    let raf = 0;\n    let visible = true;\n    let time = 12;\n    let last = 0;\n\n    const draw = () => {\n      const o = opts.current;\n      const n = Math.max(1, Math.round(o.count));\n      const a = rgb(o.color);\n      const b = rgb(o.accentColor);\n      const step = 6;\n      const radius = Math.max(w, h) * 0.18;\n      const pull = o.attraction * m.a * 0.6;\n      ctx.clearRect(0, 0, w, h);\n      ctx.lineWidth = o.thickness;\n      ctx.globalCompositeOperation = \"lighter\";\n      for (let i = 0; i < n; i++) {\n        const k = n === 1 ? 0.5 : i / (n - 1);\n        const phase = k * Math.PI * 2;\n        const c = a.map((v, j) => Math.round(v + (b[j] - v) * k));\n        const g = ctx.createLinearGradient(0, 0, w, 0);\n        g.addColorStop(0, `rgba(${c},0)`);\n        g.addColorStop(0.2, `rgba(${c},0.55)`);\n        g.addColorStop(0.5, `rgba(${c},0.9)`);\n        g.addColorStop(0.8, `rgba(${c},0.55)`);\n        g.addColorStop(1, `rgba(${c},0)`);\n        ctx.strokeStyle = g;\n        ctx.beginPath();\n        for (let x = -step; x <= w + step; x += step) {\n          const u = x / Math.max(w, 1);\n          // A loose twisted bundle: each thread orbits the shared wave with its own phase.\n          const wave =\n            Math.sin(u * 4.2 + time * 0.55) * 0.5 +\n            Math.sin(u * 7.3 - time * 0.4 + 1.3) * 0.25 +\n            Math.sin(u * 2.1 + time * 0.23) * 0.35;\n          const spread = 0.35 + 0.65 * (0.5 + 0.5 * Math.sin(u * 2.6 - time * 0.3));\n          const orbit = Math.sin(phase + u * 5.5 + time * 0.45) * spread;\n          const flutter = Math.sin(u * 13 + time * 1.7 + i * 0.9) * 0.08 * spread;\n          let y = h * 0.5 + h * 0.13 * o.amplitude * (wave + orbit * 0.9 + flutter);\n          if (pull > 0) {\n            const dx = (x - m.x) / radius;\n            const dy = (y - m.y) / (radius * 1.8);\n            y += (m.y - y) * pull * Math.exp(-dx * dx - dy * dy);\n          }\n          if (x === -step) ctx.moveTo(x, y);\n          else ctx.lineTo(x, y);\n        }\n        ctx.stroke();\n      }\n      ctx.globalCompositeOperation = \"source-over\";\n    };\n\n    const loop = (now: number) => {\n      time += (Math.min(now - last, 50) / 1000) * opts.current.speed;\n      last = now;\n      m.x += (m.tx - m.x) * 0.08;\n      m.y += (m.ty - m.y) * 0.08;\n      m.a += (m.ta - m.a) * 0.05;\n      draw();\n      raf = requestAnimationFrame(loop);\n    };\n    const play = () => {\n      if (raf || !visible || reduced) return;\n      last = performance.now();\n      raf = requestAnimationFrame(loop);\n    };\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      w = canvas.offsetWidth;\n      h = canvas.offsetHeight;\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      draw();\n    };\n    const onMove = (e: PointerEvent) => {\n      const r = canvas.getBoundingClientRect();\n      m.tx = e.clientX - r.left;\n      m.ty = e.clientY - r.top;\n      if (m.ta === 0) {\n        m.x = m.tx;\n        m.y = m.ty;\n      }\n      m.ta = 1;\n    };\n    const onLeave = () => {\n      m.ta = 0;\n    };\n\n    const target = canvas.parentElement ?? canvas;\n    target.addEventListener(\"pointermove\", onMove, { passive: true });\n    target.addEventListener(\"pointerleave\", onLeave);\n    const ro = new ResizeObserver(resize);\n    ro.observe(canvas);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) play();\n      else {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(canvas);\n    redraw.current = draw;\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      target.removeEventListener(\"pointermove\", onMove);\n      target.removeEventListener(\"pointerleave\", onLeave);\n      redraw.current = null;\n    };\n  }, []);\n\n  return (\n    <canvas\n      ref={ref}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, width: \"100%\", height: \"100%\", pointerEvents: \"none\" }}\n    />\n  );\n}\n"}]}