{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-layered-waves",
  "type": "registry:component",
  "title": "Layered Waves",
  "description": "Stacked translucent waves in depth with gradient fills and soft crest lines, rolling gently. The wave closest to the pointer rises toward it.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "waves",
    "layers",
    "ocean",
    "gradient",
    "canvas"
  ],
  "docs": "https://hyperblur.dev/effects/bg-layered-waves",
  "files": [
    {
      "path": "registry/hyperblur/bg-layered-waves.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-layered-waves.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgLayeredWavesProps extends React.HTMLAttributes<HTMLDivElement> {\n  layers?: number;\n  accent?: string;\n  accentTo?: string;\n  amplitude?: number;\n  lift?: number;\n  crest?: boolean;\n  speed?: number;\n}\n\nconst css = `\n.hb-qq4 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-qq4-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-qq4-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction qq4Rgb(color: string, fallback: [number, number, number]): [number, number, number] {\n  const m = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(color.trim());\n  if (!m) return fallback;\n  let s = m[1];\n  if (s.length === 3) s = s[0] + s[0] + s[1] + s[1] + s[2] + s[2];\n  const v = parseInt(s, 16);\n  return [(v >> 16) & 255, (v >> 8) & 255, v & 255];\n}\n\nexport function BgLayeredWaves({\n  layers = 5,\n  accent = \"#2f7bff\",\n  accentTo = \"#a855f7\",\n  amplitude = 14,\n  lift = 34,\n  crest = true,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgLayeredWavesProps) {\n  const wrapRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n\n  React.useEffect(() => {\n    const wrap = wrapRef.current;\n    const host = wrap?.parentElement;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!wrap || !host || !canvas || !ctx) return;\n\n    const n = Math.max(2, Math.min(9, Math.round(layers)));\n    const a0 = qq4Rgb(accent, [47, 123, 255]);\n    const a1 = qq4Rgb(accentTo, [168, 85, 247]);\n    const tones: string[] = [];\n    for (let i = 0; i < n; i++) {\n      const f = n === 1 ? 0 : i / (n - 1);\n      const r = Math.round(a1[0] + (a0[0] - a1[0]) * f);\n      const g = Math.round(a1[1] + (a0[1] - a1[1]) * f);\n      const b = Math.round(a1[2] + (a0[2] - a1[2]) * f);\n      tones.push(`${r},${g},${b}`);\n    }\n    const mod = new Float32Array(n);\n\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let inside = false;\n    let tx = 0;\n    let ty = 0;\n    let px = 0;\n    let py = 0;\n    let act = 0;\n    let placed = false;\n\n    function idleX(tt: number) {\n      return w * (0.5 + 0.4 * Math.sin(tt * 0.29 + 0.8));\n    }\n    function idleY(tt: number) {\n      return h * (0.58 + 0.24 * Math.sin(tt * 0.19 + 2.4));\n    }\n\n    function track(dt: number) {\n      const gx = inside ? tx : idleX(t);\n      const gy = inside ? ty : idleY(t);\n      if (!placed) {\n        px = gx;\n        py = gy;\n        placed = true;\n        return;\n      }\n      const k = 1 - Math.exp(-dt * (inside ? 8 : 2));\n      px += (gx - px) * k;\n      py += (gy - py) * k;\n      act += ((inside ? 1 : 0) - act) * (1 - Math.exp(-dt * 3));\n    }\n\n    function baseY(i: number) {\n      return h * (0.3 + (0.62 * (i + 0.5)) / n);\n    }\n\n    function wave(i: number, x: number) {\n      const depth = (i + 1) / n;\n      const a = amplitude * (0.55 + depth * 0.8);\n      const k = (0.011 - depth * 0.004) * (1 + (i % 2) * 0.25);\n      const s = 0.35 + depth * 0.45;\n      return (\n        baseY(i) +\n        a * Math.sin(x * k + t * s + i * 1.7) * 0.65 +\n        a * 0.4 * Math.sin(x * k * 2.3 - t * s * 0.8 + i * 0.9) +\n        a * 0.25 * Math.sin(x * k * 0.45 + t * 0.3 + i)\n      );\n    }\n\n    function draw(dt: number) {\n      if (!ctx || !w || !h) return;\n      ctx.clearRect(0, 0, w, h);\n      let near = 0;\n      let best = Infinity;\n      for (let i = 0; i < n; i++) {\n        const d = Math.abs(wave(i, px) - py);\n        if (d < best) {\n          best = d;\n          near = i;\n        }\n      }\n      const ease = dt > 0 ? 1 - Math.exp(-dt * 4) : 1;\n      for (let i = 0; i < n; i++) {\n        const target = i === near ? 0.45 + 0.55 * act : 0;\n        mod[i] += (target - mod[i]) * ease;\n      }\n      const step = Math.max(4, w / 120);\n      const sigma = Math.max(50, w * 0.16);\n      const s2 = 2 * sigma * sigma;\n      const breathe = 1 + 0.18 * Math.sin(t * 2.2);\n      for (let i = 0; i < n; i++) {\n        const depth = (i + 1) / n;\n        const tone = tones[i];\n        const m = mod[i];\n        const reach = Math.max(-lift * 1.6, Math.min(lift * 0.6, py - baseY(i)));\n        const bump = (reach - lift * 0.55) * m * breathe;\n        ctx.beginPath();\n        let minY = h;\n        const ys: number[] = [];\n        for (let x = -step; x <= w + step; x += step) {\n          const dx = x - px;\n          const y = wave(i, x) + bump * Math.exp(-(dx * dx) / s2);\n          ys.push(y);\n          if (y < minY) minY = y;\n        }\n        let xi = -step;\n        for (let p = 0; p < ys.length; p++) {\n          if (p === 0) ctx.moveTo(xi, ys[p]);\n          else ctx.lineTo(xi, ys[p]);\n          xi += step;\n        }\n        ctx.lineTo(w + step, h + 2);\n        ctx.lineTo(-step, h + 2);\n        ctx.closePath();\n        const g = ctx.createLinearGradient(0, minY, 0, h);\n        g.addColorStop(0, `rgba(${tone},${(0.22 + depth * 0.4 + m * 0.12).toFixed(3)})`);\n        g.addColorStop(0.55, `rgba(${tone},${(0.06 + depth * 0.14).toFixed(3)})`);\n        g.addColorStop(1, `rgba(6,7,12,${(0.25 + depth * 0.55).toFixed(3)})`);\n        ctx.fillStyle = g;\n        ctx.fill();\n        if (crest) {\n          ctx.beginPath();\n          xi = -step;\n          for (let p = 0; p < ys.length; p++) {\n            if (p === 0) ctx.moveTo(xi, ys[p]);\n            else ctx.lineTo(xi, ys[p]);\n            xi += step;\n          }\n          ctx.strokeStyle = `rgba(255,255,255,${(0.08 + depth * 0.14 + m * 0.25).toFixed(3)})`;\n          ctx.lineWidth = 1 + m * 0.6;\n          ctx.stroke();\n        }\n      }\n    }\n\n    function resize() {\n      if (!wrap || !canvas || !ctx) return;\n      const nw = wrap.clientWidth;\n      const nh = wrap.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\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      if (motion.matches) still();\n      else {\n        track(0);\n        draw(0);\n      }\n    }\n\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    function still() {\n      t = 1.2;\n      inside = false;\n      placed = false;\n      act = 0;\n      mod.fill(0);\n      track(0);\n      draw(0);\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.033, (now - prev) / 1000);\n      prev = now;\n      t += dt * speed;\n      track(dt);\n      draw(dt);\n      raf = requestAnimationFrame(frame);\n    }\n\n    function sync() {\n      const run = visible && !document.hidden && !motion.matches;\n      if (run && !raf) {\n        prev = performance.now();\n        raf = requestAnimationFrame(frame);\n      } else if (!run && raf) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n      if (motion.matches) still();\n    }\n\n    function move(e: PointerEvent) {\n      if (!wrap || motion.matches) return;\n      const rect = wrap.getBoundingClientRect();\n      const kx = rect.width ? wrap.clientWidth / rect.width : 1;\n      const ky = rect.height ? wrap.clientHeight / rect.height : 1;\n      tx = (e.clientX - rect.left) * kx;\n      ty = (e.clientY - rect.top) * ky;\n      inside = true;\n    }\n\n    function leave(e: PointerEvent) {\n      if (e.type === \"pointerup\" && e.pointerType === \"mouse\") return;\n      inside = false;\n    }\n\n    const ro = new ResizeObserver(resize);\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1].isIntersecting;\n      sync();\n    });\n    ro.observe(wrap);\n    io.observe(wrap);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", sync);\n    host.addEventListener(\"pointermove\", move, { passive: true });\n    host.addEventListener(\"pointerdown\", move, { passive: true });\n    host.addEventListener(\"pointerleave\", leave);\n    host.addEventListener(\"pointerup\", leave);\n    host.addEventListener(\"pointercancel\", leave);\n    resize();\n    sync();\n\n    return () => {\n      if (raf) cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", sync);\n      motion.removeEventListener(\"change\", sync);\n      host.removeEventListener(\"pointermove\", move);\n      host.removeEventListener(\"pointerdown\", move);\n      host.removeEventListener(\"pointerleave\", leave);\n      host.removeEventListener(\"pointerup\", leave);\n      host.removeEventListener(\"pointercancel\", leave);\n    };\n  }, [layers, accent, accentTo, amplitude, lift, crest, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qq4 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-layered-waves\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qq4-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-qq4-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}