{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"filings","type":"registry:ui","title":"Filings","description":"A field of short lines that swing to point at the cursor like iron filings, glowing and stretching as it nears.","dependencies":[],"registryDependencies":["utils"],"files":[{"path":"registry/ui/filings.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface FilingsProps {\n  /** Px between line centers. */\n  gap?: number;\n  /** Line length in px. */\n  lineLength?: number;\n  /** Line thickness in px. */\n  lineWidth?: number;\n  /** Idle line color. */\n  color?: string;\n  /** Color of lines close to the cursor. */\n  activeColor?: string;\n  /** Px radius in which lines light up and stretch. */\n  radius?: number;\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};\n\nexport function Filings({\n  gap = 30,\n  lineLength = 16,\n  lineWidth = 2,\n  color = \"#3f3f46\",\n  activeColor = \"#c6ff3d\",\n  radius = 220,\n  className,\n}: FilingsProps) {\n  const ref = useRef<HTMLCanvasElement>(null);\n\n  useEffect(() => {\n    const canvas = ref.current!;\n    const ctx = canvas.getContext(\"2d\")!;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const base = rgb(color);\n    const hot = rgb(activeColor);\n    // Pre-build a 32-step color ramp so the frame loop never formats strings per line.\n    const ramp = Array.from({ length: 33 }, (_, i) => `rgb(${base.map((b, j) => Math.round(b + (hot[j] - b) * (i / 32))).join(\",\")})`);\n    let w = 0;\n    let h = 0;\n    let cols = 0;\n    let rows = 0;\n    let ox = 0;\n    let oy = 0;\n    let ang = new Float32Array(0);\n    let heat = new Float32Array(0);\n    const m = { x: 0, y: 0, on: false };\n    let raf = 0;\n    let visible = true;\n\n    const frame = (time: number) => {\n      ctx.clearRect(0, 0, w, h);\n      ctx.lineCap = \"round\";\n      ctx.lineWidth = lineWidth;\n      // With no cursor, lines follow a slow orbiting attractor so the grid never looks dead.\n      const t = time / 1000;\n      const tx = m.on ? m.x : w / 2 + Math.cos(t * 0.5) * w * 0.3;\n      const ty = m.on ? m.y : h / 2 + Math.sin(t * 0.7) * h * 0.3;\n      const k = reduced ? 1 : 0.14;\n      for (let r = 0; r < rows; r++) {\n        for (let c = 0; c < cols; c++) {\n          const i = r * cols + c;\n          const x = ox + c * gap;\n          const y = oy + r * gap;\n          const dx = tx - x;\n          const dy = ty - y;\n          const target = Math.atan2(dy, dx);\n          let d = target - ang[i];\n          d = Math.atan2(Math.sin(d), Math.cos(d)); // shortest way round\n          ang[i] += d * k;\n          const near = Math.max(0, 1 - Math.hypot(dx, dy) / radius);\n          heat[i] += ((m.on ? near : near * 0.6) - heat[i]) * k;\n          const len = (lineLength / 2) * (1 + heat[i] * 0.6);\n          const cx = Math.cos(ang[i]) * len;\n          const cy = Math.sin(ang[i]) * len;\n          ctx.strokeStyle = ramp[Math.round(heat[i] * 32)];\n          ctx.beginPath();\n          ctx.moveTo(x - cx, y - cy);\n          ctx.lineTo(x + cx, y + cy);\n          ctx.stroke();\n        }\n      }\n    };\n\n    const loop = (time: number) => {\n      frame(time);\n      raf = visible && !reduced ? requestAnimationFrame(loop) : 0;\n    };\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.round(w * dpr);\n      canvas.height = Math.round(h * dpr);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      cols = Math.max(1, Math.floor(w / gap));\n      rows = Math.max(1, Math.floor(h / gap));\n      ox = (w - (cols - 1) * gap) / 2;\n      oy = (h - (rows - 1) * gap) / 2;\n      ang = new Float32Array(cols * rows).fill(-Math.PI / 4);\n      heat = new Float32Array(cols * rows);\n      frame(performance.now());\n    };\n\n    const onMove = (e: PointerEvent) => {\n      const r = canvas.getBoundingClientRect();\n      m.x = e.clientX - r.left;\n      m.y = e.clientY - r.top;\n      m.on = true;\n      if (reduced) frame(0);\n    };\n    const onLeave = () => (m.on = false);\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(canvas);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && !raf && !reduced) raf = requestAnimationFrame(loop);\n    });\n    io.observe(canvas);\n    const host = canvas.parentElement ?? canvas;\n    host.addEventListener(\"pointermove\", onMove, { passive: true });\n    host.addEventListener(\"pointerleave\", onLeave);\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      host.removeEventListener(\"pointermove\", onMove);\n      host.removeEventListener(\"pointerleave\", onLeave);\n    };\n  }, [gap, lineLength, lineWidth, color, activeColor, radius]);\n\n  return (\n    <canvas\n      ref={ref}\n      aria-hidden\n      className={cn(\"pointer-events-none\", className)}\n      style={{ position: \"absolute\", inset: 0, width: \"100%\", height: \"100%\" }}\n    />\n  );\n}\n"}]}