{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"tide-lines","type":"registry:ui","title":"Tide Lines","description":"Stacked lines flowing like a noise-driven sea; the cursor drags through them and they spring back.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/tide-lines.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface TideLinesProps {\n  /** Color of the top lines. */\n  color?: string;\n  /** Color of the bottom lines; the rest blend between. */\n  accentColor?: string;\n  /** Px between lines. */\n  gap?: number;\n  /** Wave height multiplier. */\n  amplitude?: number;\n  /** Flow speed multiplier; 0 freezes the motion. */\n  speed?: number;\n  /** Stroke width in px. */\n  lineWidth?: number;\n  /** How far the cursor drags the lines, 0 to disable. */\n  cursorForce?: 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\n/** Seeded 2D gradient noise in roughly [-1, 1]. */\nfunction makeNoise(seed: number) {\n  const perm = new Uint8Array(512);\n  const p = Array.from({ length: 256 }, (_, i) => i);\n  let s = seed;\n  for (let i = 255; i > 0; i--) {\n    s = (s * 16807) % 2147483647;\n    const j = s % (i + 1);\n    [p[i], p[j]] = [p[j], p[i]];\n  }\n  for (let i = 0; i < 512; i++) perm[i] = p[i & 255];\n  const grad = (h: number, x: number, y: number) => {\n    const a = (h & 7) * 0.7853981634;\n    return Math.cos(a) * x + Math.sin(a) * y;\n  };\n  const fade = (t: number) => t * t * t * (t * (t * 6 - 15) + 10);\n  return (x: number, y: number) => {\n    const xi = Math.floor(x);\n    const yi = Math.floor(y);\n    const xf = x - xi;\n    const yf = y - yi;\n    const X = xi & 255;\n    const Y = yi & 255;\n    const u = fade(xf);\n    const v = fade(yf);\n    const aa = perm[perm[X] + Y];\n    const ab = perm[perm[X] + Y + 1];\n    const ba = perm[perm[X + 1] + Y];\n    const bb = perm[perm[X + 1] + Y + 1];\n    const x1 = grad(aa, xf, yf) + (grad(ba, xf - 1, yf) - grad(aa, xf, yf)) * u;\n    const x2 = grad(ab, xf, yf - 1) + (grad(bb, xf - 1, yf - 1) - grad(ab, xf, yf - 1)) * u;\n    return (x1 + (x2 - x1) * v) * 1.4;\n  };\n}\n\nexport function TideLines({\n  color = \"#38bdf8\",\n  accentColor = \"#a78bfa\",\n  gap = 14,\n  amplitude = 1,\n  speed = 1,\n  lineWidth = 1,\n  cursorForce = 1,\n  className,\n}: TideLinesProps) {\n  const ref = useRef<HTMLCanvasElement>(null);\n  const opts = useRef({ color, accentColor, gap, amplitude, speed, lineWidth, cursorForce });\n  const redraw = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = { color, accentColor, gap, amplitude, speed, lineWidth, cursorForce };\n    redraw.current?.();\n  }, [color, accentColor, gap, amplitude, speed, lineWidth, cursorForce]);\n\n  useEffect(() => {\n    const canvas = ref.current!;\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n    const noise = makeNoise(1337);\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const step = 10;\n    const m = { x: -9999, y: -9999, vx: 0, vy: 0, px: -9999, py: -9999 };\n    let w = 0;\n    let h = 0;\n    let rows = 0;\n    let cols = 0;\n    let builtGap = 0;\n    // Per-point spring offsets the cursor pushes around.\n    let ox = new Float32Array(0);\n    let oy = new Float32Array(0);\n    let vx = new Float32Array(0);\n    let vy = new Float32Array(0);\n    let raf = 0;\n    let visible = true;\n    let time = 0;\n    let last = 0;\n\n    const build = () => {\n      builtGap = Math.max(4, opts.current.gap);\n      rows = Math.ceil(h / builtGap) + 6;\n      cols = Math.ceil(w / step) + 3;\n      const n = rows * cols;\n      ox = new Float32Array(n);\n      oy = new Float32Array(n);\n      vx = new Float32Array(n);\n      vy = new Float32Array(n);\n    };\n\n    const physics = () => {\n      const f = opts.current.cursorForce;\n      const r = 150;\n      const g = builtGap;\n      for (let j = 0; j < rows; j++) {\n        const y0 = (j - 3) * g;\n        if (Math.abs(y0 - m.y) > r + 60) {\n          // Still settle rows the cursor left behind.\n          for (let i = 0, k = j * cols; i < cols; i++, k++) {\n            vx[k] = (vx[k] - ox[k] * 0.03) * 0.9;\n            vy[k] = (vy[k] - oy[k] * 0.03) * 0.9;\n            ox[k] += vx[k];\n            oy[k] += vy[k];\n          }\n          continue;\n        }\n        for (let i = 0, k = j * cols; i < cols; i++, k++) {\n          const x0 = (i - 1) * step;\n          const dx = x0 + ox[k] - m.x;\n          const dy = y0 + oy[k] - m.y;\n          const d2 = dx * dx + dy * dy;\n          if (d2 < r * r && f > 0) {\n            const t = 1 - Math.sqrt(d2) / r;\n            const s = t * t * f;\n            vx[k] += m.vx * s * 0.12;\n            vy[k] += m.vy * s * 0.12;\n          }\n          vx[k] = (vx[k] - ox[k] * 0.03) * 0.9;\n          vy[k] = (vy[k] - oy[k] * 0.03) * 0.9;\n          ox[k] += vx[k];\n          oy[k] += vy[k];\n        }\n      }\n    };\n\n    const draw = () => {\n      const o = opts.current;\n      if (Math.max(4, o.gap) !== builtGap) build();\n      const a = rgb(o.color);\n      const b = rgb(o.accentColor);\n      const g = builtGap;\n      const amp = 34 * o.amplitude;\n      ctx.clearRect(0, 0, w, h);\n      ctx.lineWidth = o.lineWidth;\n      ctx.lineJoin = \"round\";\n      for (let j = 0; j < rows; j++) {\n        const y0 = (j - 3) * g;\n        const k = Math.min(1, Math.max(0, y0 / Math.max(h, 1)));\n        // Lines fade toward the top and bottom edges.\n        const alpha = 0.18 + 0.5 * Math.sin(Math.PI * k);\n        const c = a.map((v, i) => Math.round(v + (b[i] - v) * k));\n        ctx.strokeStyle = `rgba(${c},${alpha.toFixed(3)})`;\n        ctx.beginPath();\n        for (let i = 0; i < cols; i++) {\n          const idx = j * cols + i;\n          const x0 = (i - 1) * step;\n          const n1 = noise(x0 * 0.0021 + time * 0.09, y0 * 0.0035 + time * 0.05);\n          const n2 = noise(x0 * 0.006 - time * 0.12 + 40, y0 * 0.006 + 17);\n          const x = x0 + ox[idx];\n          const y = y0 + oy[idx] + n1 * amp + n2 * amp * 0.25;\n          if (i === 0) ctx.moveTo(x, y);\n          else ctx.lineTo(x, y);\n        }\n        ctx.stroke();\n      }\n    };\n\n    const loop = (now: number) => {\n      time += (Math.min(now - last, 50) / 1000) * opts.current.speed;\n      last = now;\n      if (m.px > -9000) {\n        m.vx = m.x - m.px;\n        m.vy = m.y - m.py;\n      }\n      m.px = m.x;\n      m.py = m.y;\n      physics();\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      build();\n      draw();\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    };\n    const onLeave = () => {\n      m.x = m.y = m.px = m.py = -9999;\n      m.vx = m.vy = 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"}]}