{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"cloth-wire","type":"registry:ui","title":"Cloth Wire","description":"A wireframe mesh that stretches around the cursor like cloth and springs back; click to send a shockwave.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/cloth-wire.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface ClothWireProps {\n  /** Px between mesh points. */\n  spacing?: number;\n  /** Px radius the cursor bends. */\n  radius?: number;\n  /** Cursor force multiplier. */\n  strength?: number;\n  /** Pull back toward rest, 0–1. Higher = snappier. */\n  stiffness?: number;\n  /** Velocity kept per frame, 0–1. Higher = wobblier. */\n  damping?: number;\n  /** Push the mesh away from the cursor or pull it in. */\n  mode?: \"push\" | \"pull\";\n  /** Idle line color (6-digit hex). */\n  lineColor?: string;\n  /** Color of stretched lines (6-digit hex). */\n  activeColor?: string;\n  className?: string;\n}\n\nconst rgb = (hex: string) => {\n  const n = parseInt(hex.replace(\"#\", \"\").padEnd(6, \"0\").slice(0, 6), 16);\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n};\nconst BUCKETS = 8;\n\nexport function ClothWire({\n  spacing = 34,\n  radius = 170,\n  strength = 1,\n  stiffness = 0.05,\n  damping = 0.88,\n  mode = \"push\",\n  lineColor = \"#34343c\",\n  activeColor = \"#c6ff3d\",\n  className,\n}: ClothWireProps) {\n  const ref = useRef<HTMLCanvasElement>(null);\n\n  useEffect(() => {\n    const canvas = ref.current!;\n    const host = canvas.parentElement!;\n    const ctx = canvas.getContext(\"2d\")!;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const a = rgb(lineColor);\n    const b = rgb(activeColor);\n    const colors = Array.from({ length: BUCKETS }, (_, k) => {\n      const t = k / (BUCKETS - 1);\n      return `rgb(${a.map((v, i) => Math.round(v + (b[i] - v) * t)).join(\",\")})`;\n    });\n    const mouse = { x: -1e4, y: -1e4, in: false };\n    const sign = mode === \"pull\" ? -1 : 1;\n    let cols = 0;\n    let rows = 0;\n    let w = 0;\n    let h = 0;\n    // Flat arrays: rest position, offset from rest, velocity.\n    let rx = new Float32Array(0);\n    let ry = rx;\n    let ox = rx;\n    let oy = rx;\n    let vx = rx;\n    let vy = rx;\n    let raf = 0;\n    let visible = true;\n\n    const step = () => {\n      const r2 = radius * radius;\n      let energy = 0;\n      for (let j = 0; j < rows; j++) {\n        for (let i = 0; i < cols; i++) {\n          const k = j * cols + i;\n          const x = rx[k] + ox[k];\n          const y = ry[k] + oy[k];\n          // Couple to neighbours so disturbances ripple through the mesh like cloth.\n          const l = i > 0 ? k - 1 : k;\n          const rt = i < cols - 1 ? k + 1 : k;\n          const u = j > 0 ? k - cols : k;\n          const dn = j < rows - 1 ? k + cols : k;\n          const nx = (ox[l] + ox[rt] + ox[u] + ox[dn]) / 4;\n          const ny = (oy[l] + oy[rt] + oy[u] + oy[dn]) / 4;\n          let ax = -ox[k] * stiffness + (nx - ox[k]) * 0.12;\n          let ay = -oy[k] * stiffness + (ny - oy[k]) * 0.12;\n          if (mouse.in) {\n            const dx = x - mouse.x;\n            const dy = y - mouse.y;\n            const d2 = dx * dx + dy * dy;\n            if (d2 < r2 && d2 > 0.01) {\n              const d = Math.sqrt(d2);\n              const f = (1 - d / radius) ** 2 * strength * 2.4 * sign;\n              ax += (dx / d) * f;\n              ay += (dy / d) * f;\n            }\n          }\n          vx[k] = (vx[k] + ax) * damping;\n          vy[k] = (vy[k] + ay) * damping;\n          energy = Math.max(energy, Math.abs(vx[k]) + Math.abs(vy[k]) + (Math.abs(ox[k]) + Math.abs(oy[k])) * 0.05);\n        }\n      }\n      for (let k = 0; k < ox.length; k++) {\n        ox[k] += vx[k];\n        oy[k] += vy[k];\n      }\n      return energy;\n    };\n\n    const draw = () => {\n      ctx.clearRect(0, 0, w, h);\n      ctx.lineWidth = 1;\n      const paths = Array.from({ length: BUCKETS }, () => new Path2D());\n      const seg = (p: number, q: number) => {\n        const stretch = Math.hypot(ox[p] + ox[q], oy[p] + oy[q]) / 2;\n        const bucket = Math.min(BUCKETS - 1, Math.floor((stretch / (spacing * 0.6)) * BUCKETS));\n        paths[bucket].moveTo(rx[p] + ox[p], ry[p] + oy[p]);\n        paths[bucket].lineTo(rx[q] + ox[q], ry[q] + oy[q]);\n      };\n      for (let j = 0; j < rows; j++) {\n        for (let i = 0; i < cols; i++) {\n          const k = j * cols + i;\n          if (i < cols - 1) seg(k, k + 1);\n          if (j < rows - 1) seg(k, k + cols);\n        }\n      }\n      paths.forEach((p, i) => {\n        ctx.strokeStyle = colors[i];\n        ctx.globalAlpha = 0.8 + (i / BUCKETS) * 0.2;\n        ctx.stroke(p);\n      });\n      ctx.globalAlpha = 1;\n    };\n\n    const loop = () => {\n      const energy = step();\n      draw();\n      // Sleep once the mesh has settled and nothing is touching it.\n      raf = visible && (mouse.in || energy > 0.01) ? requestAnimationFrame(loop) : 0;\n    };\n    const wake = () => {\n      if (!raf && visible && !reduced) raf = requestAnimationFrame(loop);\n    };\n\n    const ro = new ResizeObserver(() => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      w = canvas.offsetWidth;\n      h = canvas.offsetHeight;\n      canvas.width = w * dpr;\n      canvas.height = h * dpr;\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      // One extra ring of points past each edge so the border never shows.\n      cols = Math.ceil(w / spacing) + 3;\n      rows = Math.ceil(h / spacing) + 3;\n      const n = cols * rows;\n      rx = new Float32Array(n);\n      ry = new Float32Array(n);\n      ox = new Float32Array(n);\n      oy = new Float32Array(n);\n      vx = new Float32Array(n);\n      vy = new Float32Array(n);\n      const x0 = (w - (cols - 1) * spacing) / 2;\n      const y0 = (h - (rows - 1) * spacing) / 2;\n      for (let k = 0; k < n; k++) {\n        rx[k] = x0 + (k % cols) * spacing;\n        ry[k] = y0 + Math.floor(k / cols) * spacing;\n      }\n      draw();\n    });\n    ro.observe(canvas);\n\n    const pos = (e: PointerEvent) => {\n      const r = canvas.getBoundingClientRect();\n      mouse.x = e.clientX - r.left;\n      mouse.y = e.clientY - r.top;\n    };\n    const onMove = (e: PointerEvent) => {\n      pos(e);\n      mouse.in = true;\n      wake();\n    };\n    const onLeave = () => {\n      mouse.in = false;\n    };\n    const onDown = (e: PointerEvent) => {\n      pos(e);\n      const reach = radius * 2;\n      for (let k = 0; k < ox.length; k++) {\n        const dx = rx[k] + ox[k] - mouse.x;\n        const dy = ry[k] + oy[k] - mouse.y;\n        const d = Math.hypot(dx, dy) || 1;\n        if (d < reach) {\n          const f = (1 - d / reach) * 16 * strength * sign;\n          vx[k] += (dx / d) * f;\n          vy[k] += (dy / d) * f;\n        }\n      }\n      wake();\n    };\n\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) wake();\n    });\n    io.observe(canvas);\n    host.addEventListener(\"pointermove\", onMove, { passive: true });\n    host.addEventListener(\"pointerleave\", onLeave);\n    host.addEventListener(\"pointerdown\", onDown);\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      host.removeEventListener(\"pointermove\", onMove);\n      host.removeEventListener(\"pointerleave\", onLeave);\n      host.removeEventListener(\"pointerdown\", onDown);\n    };\n  }, [spacing, radius, strength, stiffness, damping, mode, lineColor, activeColor]);\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"}]}