{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"magnet-lines","type":"registry:ui","title":"Magnet Lines","description":"A grid of short lines that all turn to face the pointer, and sway in a slow wave when it is away.","dependencies":["motion"],"registryDependencies":["https://ui.mekaigames.com/r/theme.json"],"files":[{"path":"components/ui/magnet-lines.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useReducedMotion } from \"motion/react\";\nimport { useEffect, useRef } from \"react\";\n\ntype MagnetLinesProps = {\n  rows?: number;\n  columns?: number;\n  /** Line color as a hex value. */\n  color?: string;\n  /** Line thickness in pixels. */\n  lineWidth?: number;\n  /** Line length in pixels. */\n  lineHeight?: number;\n  className?: string;\n};\n\nfunction hexToRgb(hex: string) {\n  let h = hex.replace(\"#\", \"\");\n  if (h.length === 3) h = h.replace(/./g, (c) => c + c);\n  const n = parseInt(h, 16) || 0;\n  return `${(n >> 16) & 255},${(n >> 8) & 255},${n & 255}`;\n}\n\n/** Shortest turn between two line angles. A line looks the same after half a turn, so this works modulo pi. */\nfunction turn(from: number, to: number) {\n  let d = (to - from) % Math.PI;\n  if (d > Math.PI / 2) d -= Math.PI;\n  if (d < -Math.PI / 2) d += Math.PI;\n  return d;\n}\n\n/** A grid of short lines that all turn to face the pointer, and sway in a slow wave when it is away. */\nexport function MagnetLines({\n  rows = 9,\n  columns = 14,\n  color = \"#ff784f\",\n  lineWidth = 2,\n  lineHeight = 22,\n  className = \"\",\n}: MagnetLinesProps) {\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const reduce = useReducedMotion();\n\n  useEffect(() => {\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!canvas || !ctx) return;\n\n    const rgb = hexToRgb(color);\n    const nr = Math.max(1, Math.round(rows));\n    const nc = Math.max(1, Math.round(columns));\n    const angles = new Float32Array(nr * nc).fill(-Math.PI / 4);\n    const glow = new Float32Array(nr * nc);\n    const pointer = { x: 0, y: 0, inside: false };\n    let width = 0;\n    let height = 0;\n    let frame = 0;\n    let visible = true;\n    let last = 0;\n\n    const draw = (t: number) => {\n      const dt = last ? Math.min(50, t - last) : 16;\n      last = t;\n      const cellW = width / nc;\n      const cellH = height / nr;\n      const reach = Math.max(cellW, cellH) * 3.5;\n      const ease = reduce ? 1 : Math.min(1, dt * 0.009);\n\n      ctx.clearRect(0, 0, width, height);\n      ctx.lineCap = \"round\";\n      ctx.lineWidth = Math.max(0.5, lineWidth);\n      for (let r = 0; r < nr; r++) {\n        for (let c = 0; c < nc; c++) {\n          const i = r * nc + c;\n          const x = (c + 0.5) * cellW;\n          const y = (r + 0.5) * cellH;\n          let target: number;\n          let near = 0;\n          if (pointer.inside) {\n            target = Math.atan2(pointer.y - y, pointer.x - x);\n            near = Math.max(0, 1 - Math.hypot(pointer.x - x, pointer.y - y) / reach);\n          } else {\n            // Idle: a slow diagonal wave rolls across the grid.\n            target = -Math.PI / 4 + Math.sin(t * 0.0009 + c * 0.42 + r * 0.3) * 0.75;\n          }\n          angles[i] += turn(angles[i], target) * ease;\n          glow[i] += (near - glow[i]) * ease;\n          const len = lineHeight * (1 + glow[i] * 0.25);\n          const dx = (Math.cos(angles[i]) * len) / 2;\n          const dy = (Math.sin(angles[i]) * len) / 2;\n          ctx.strokeStyle = `rgba(${rgb},${0.38 + glow[i] * 0.62})`;\n          ctx.beginPath();\n          ctx.moveTo(x - dx, y - dy);\n          ctx.lineTo(x + dx, y + dy);\n          ctx.stroke();\n        }\n      }\n\n      if (!reduce && visible) frame = requestAnimationFrame(draw);\n      else frame = 0;\n    };\n\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      width = canvas.clientWidth;\n      height = canvas.clientHeight;\n      canvas.width = Math.round(width * dpr);\n      canvas.height = Math.round(height * dpr);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      if (reduce) draw(0);\n    };\n\n    const onMove = (e: PointerEvent) => {\n      const rect = canvas.getBoundingClientRect();\n      pointer.x = e.clientX - rect.left;\n      pointer.y = e.clientY - rect.top;\n      pointer.inside = pointer.x >= 0 && pointer.y >= 0 && pointer.x <= rect.width && pointer.y <= rect.height;\n      // With reduced motion the lines snap to the pointer instead of animating.\n      if (reduce) draw(performance.now());\n    };\n    const onOut = (e: PointerEvent) => {\n      if (!e.relatedTarget) {\n        pointer.inside = false;\n        if (reduce) draw(performance.now());\n      }\n    };\n\n    resize();\n    const observer = new ResizeObserver(resize);\n    observer.observe(canvas);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && !reduce && !frame) {\n        last = 0;\n        frame = requestAnimationFrame(draw);\n      }\n    });\n    io.observe(canvas);\n    window.addEventListener(\"pointermove\", onMove, { passive: true });\n    document.addEventListener(\"pointerout\", onOut);\n    if (!reduce) frame = requestAnimationFrame(draw);\n\n    return () => {\n      cancelAnimationFrame(frame);\n      observer.disconnect();\n      io.disconnect();\n      window.removeEventListener(\"pointermove\", onMove);\n      document.removeEventListener(\"pointerout\", onOut);\n    };\n  }, [rows, columns, color, lineWidth, lineHeight, reduce]);\n\n  return (\n    <canvas\n      ref={canvasRef}\n      className={`pointer-events-none absolute inset-0 block size-full ${className}`}\n      aria-hidden=\"true\"\n    />\n  );\n}\n"}]}