{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "water-ripples",
  "type": "registry:ui",
  "title": "Water Ripples",
  "author": "Rishabh Bose <https://theb0se.vercel.app/>",
  "description": "Interactive water surface ripple simulation combining a real-time 2D finite-difference wave-equation height-map buffer with expanding specular caustic refraction rings triggered by cursor wakes, clicks, and ambient droplets.",
  "dependencies": [
    "lucide-react",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "components/ui/water-ripples.tsx",
      "content": "\"use client\";\n\nimport React, { useEffect, useRef, useState } from \"react\";\nimport { Radio, CloudRain, MousePointerClick } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface WaterRipplesProps {\n  /** Wave energy retention factor per frame (0.90 to 0.995). Default: 0.97 */\n  damping?: number;\n  /** Maximum expansion radius of spawned concentric ripple rings in px. Default: 140 */\n  rippleRadius?: number;\n  /** Automatically spawn periodic rain/ambient droplets across the surface. Default: true */\n  ambientDrops?: boolean;\n  /** Primary specular caustic highlight hex color. Default: \"#38BDF8\" */\n  color?: string;\n  /** Spawn ripples on pointer move and click. Default: true */\n  interactive?: boolean;\n  /** Optional foreground content */\n  children?: React.ReactNode;\n  className?: string;\n}\n\nfunction hexToRgb(hex: string): [number, number, number] {\n  const clean = hex.replace(\"#\", \"\").trim();\n  if (clean.length === 3) {\n    return [\n      parseInt(clean[0] + clean[0], 16) || 56,\n      parseInt(clean[1] + clean[1], 16) || 189,\n      parseInt(clean[2] + clean[2], 16) || 248,\n    ];\n  }\n  const num = parseInt(clean, 16);\n  if (Number.isNaN(num)) return [56, 189, 248];\n  return [(num >> 16) & 255, (num >> 8) & 255, num & 255];\n}\n\nconst RIPPLE_PRESETS = [\n  { name: \"Lagoon Pool\", color: \"#38BDF8\", damping: 0.975, radius: 140 },\n  { name: \"Jade Sanctuary\", color: \"#34D399\", damping: 0.982, radius: 175 },\n  { name: \"Sapphire Basin\", color: \"#818CF8\", damping: 0.962, radius: 120 },\n];\n\nexport function WaterRipples({\n  damping = 0.97,\n  rippleRadius = 140,\n  ambientDrops = true,\n  color = \"#38BDF8\",\n  interactive = true,\n  children,\n  className,\n}: WaterRipplesProps) {\n  const containerRef = useRef<HTMLDivElement>(null);\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n\n  const [liveDamping, setLiveDamping] = useState(damping);\n  const [liveRadius, setLiveRadius] = useState(rippleRadius);\n  const [liveAmbient, setLiveAmbient] = useState(ambientDrops);\n  const [liveColor, setLiveColor] = useState(color);\n  const [dropCounter, setDropCounter] = useState(0);\n\n  useEffect(() => setLiveDamping(damping), [damping]);\n  useEffect(() => setLiveRadius(rippleRadius), [rippleRadius]);\n  useEffect(() => setLiveAmbient(ambientDrops), [ambientDrops]);\n  useEffect(() => setLiveColor(color), [color]);\n\n  const paramsRef = useRef({\n    damping: liveDamping,\n    radius: liveRadius,\n    ambient: liveAmbient,\n    color: liveColor,\n  });\n\n  useEffect(() => {\n    paramsRef.current = {\n      damping: liveDamping,\n      radius: liveRadius,\n      ambient: liveAmbient,\n      color: liveColor,\n    };\n  }, [liveDamping, liveRadius, liveAmbient, liveColor]);\n\n  // Imperative disturb callback ref so pointer handlers trigger ripples without re-mounting\n  const disturbRef = useRef<\n    (normX: number, normY: number, strength: number) => void\n  >(() => {});\n\n  useEffect(() => {\n    const container = containerRef.current;\n    const canvas = canvasRef.current;\n    if (!container || !canvas) return;\n\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n\n    let simW = 280;\n    let simH = 180;\n    let bufPrev = new Float32Array(simW * simH);\n    let bufCurr = new Float32Array(simW * simH);\n    // Precomputed procedural submerged mosaic/pebble & static caustic texture buffer\n    let bedTex = new Float32Array(simW * simH);\n\n    const offscreen = document.createElement(\"canvas\");\n    offscreen.width = simW;\n    offscreen.height = simH;\n    const offCtx = offscreen.getContext(\"2d\");\n    let imgData = offCtx ? offCtx.createImageData(simW, simH) : null;\n\n    const buildSubmergedBed = (w: number, h: number) => {\n      bedTex = new Float32Array(w * h);\n      for (let y = 0; y < h; y++) {\n        const ny = y / h;\n        for (let x = 0; x < w; x++) {\n          const nx = x / w;\n          // Subtle submerged stone tile grid + organic pebble/sand harmonic texture\n          const tileX = Math.abs(Math.sin(nx * Math.PI * 14));\n          const tileY = Math.abs(Math.sin(ny * Math.PI * 9));\n          const grout = Math.pow(Math.min(tileX, tileY), 0.18);\n\n          const pebble =\n            0.5 +\n            0.25 * Math.sin(x * 0.23 + Math.cos(y * 0.19) * 2.5) +\n            0.25 * Math.cos(y * 0.27 - Math.sin(x * 0.21) * 2.5);\n\n          bedTex[y * w + x] = grout * (0.72 + 0.28 * pebble);\n        }\n      }\n    };\n\n    let viewW = 1;\n    let viewH = 1;\n    let dpr = 1;\n\n    const resize = () => {\n      const rect = container.getBoundingClientRect();\n      viewW = Math.max(1, rect.width);\n      viewH = Math.max(1, rect.height);\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = Math.floor(viewW * dpr);\n      canvas.height = Math.floor(viewH * dpr);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n\n      // 0.45x simulation grid resolution for silky 60fps per-pixel 3D normal shading\n      const nextSimW = Math.max(160, Math.min(340, Math.floor(viewW * 0.42)));\n      const nextSimH = Math.max(110, Math.min(230, Math.floor(viewH * 0.42)));\n      if (nextSimW !== simW || nextSimH !== simH || bedTex.length === 0) {\n        simW = nextSimW;\n        simH = nextSimH;\n        bufPrev = new Float32Array(simW * simH);\n        bufCurr = new Float32Array(simW * simH);\n        buildSubmergedBed(simW, simH);\n        offscreen.width = simW;\n        offscreen.height = simH;\n        imgData = offCtx ? offCtx.createImageData(simW, simH) : null;\n      }\n    };\n\n    resize();\n    const ro = new ResizeObserver(resize);\n    ro.observe(container);\n\n    const triggerRipple = (normX: number, normY: number, strength: number) => {\n      const gx = Math.floor(normX * simW);\n      const gy = Math.floor(normY * simH);\n      const rad = Math.max(\n        3,\n        Math.min(12, Math.round((paramsRef.current.radius / 140) * 6))\n      );\n\n      for (let dy = -rad; dy <= rad; dy++) {\n        const y = gy + dy;\n        if (y <= 2 || y >= simH - 2) continue;\n        for (let dx = -rad; dx <= rad; dx++) {\n          const x = gx + dx;\n          if (x <= 2 || x >= simW - 2) continue;\n          const dist = Math.sqrt(dx * dx + dy * dy);\n          if (dist <= rad) {\n            // Smooth raised ring profile so each drop launches a realistic capillary wave packet\n            const phase = (dist / rad) * Math.PI;\n            const profile = Math.cos(phase);\n            bufPrev[y * simW + x] -= strength * profile;\n          }\n        }\n      }\n    };\n\n    disturbRef.current = triggerRipple;\n\n    // Seed initial welcoming droplets\n    triggerRipple(0.35, 0.45, 220);\n    triggerRipple(0.66, 0.54, 190);\n\n    let rafId = 0;\n    let frame = 0;\n\n    // Normalized 3D directional light vector L = normalize(-0.45, -0.55, 0.70)\n    // and Halfway vector H = normalize(L + V) where V = (0, 0, 1)\n    const lx = -0.45;\n    const ly = -0.55;\n    const lz = 0.703;\n    const hLen = Math.hypot(lx, ly, lz + 1.0);\n    const hx = lx / hLen;\n    const hy = ly / hLen;\n    const hz = (lz + 1.0) / hLen;\n\n    const render = () => {\n      frame++;\n      const {\n        damping: curDamping,\n        ambient: curAmbient,\n        color: curColor,\n      } = paramsRef.current;\n\n      const [tintR, tintG, tintB] = hexToRgb(curColor);\n      const damp = Math.max(0.9, Math.min(0.994, curDamping));\n\n      // Periodic ambient rain droplets\n      if (curAmbient && frame % 24 === 0) {\n        triggerRipple(\n          0.1 + Math.random() * 0.8,\n          0.12 + Math.random() * 0.76,\n          140 + Math.random() * 120\n        );\n      }\n\n      // 1. Step 2D Wave Equation with 8-neighbor isotropic Laplacian kernel for circular waves\n      for (let y = 1; y < simH - 1; y++) {\n        const row = y * simW;\n        for (let x = 1; x < simW - 1; x++) {\n          const idx = row + x;\n          const cardinal =\n            bufPrev[idx - 1] +\n            bufPrev[idx + 1] +\n            bufPrev[idx - simW] +\n            bufPrev[idx + simW];\n          const diagonal =\n            bufPrev[idx - simW - 1] +\n            bufPrev[idx - simW + 1] +\n            bufPrev[idx + simW - 1] +\n            bufPrev[idx + simW + 1];\n\n          const avg = cardinal * 0.4 + diagonal * 0.1;\n          bufCurr[idx] = (avg - bufCurr[idx]) * damp;\n        }\n      }\n\n      // 2. Per-pixel 3D Surface Normal Reconstruction, Snell Refraction, Caustic Focusing & Blinn-Phong Specular\n      if (offCtx && imgData) {\n        const data = imgData.data;\n        const timeShift = frame * 0.025;\n\n        for (let y = 1; y < simH - 1; y++) {\n          const row = y * simW;\n          const vNorm = y / simH;\n\n          for (let x = 1; x < simW - 1; x++) {\n            const idx = row + x;\n\n            // Central-difference 2D height-field gradient (dh/dx, dh/dy)\n            const dhdx = (bufCurr[idx + 1] - bufCurr[idx - 1]) * 0.032;\n            const dhdy = (bufCurr[idx + simW] - bufCurr[idx - simW]) * 0.032;\n\n            // 3D Surface Normal N = normalize(-dhdx, -dhdy, 1.0)\n            const invLen = 1.0 / Math.sqrt(dhdx * dhdx + dhdy * dhdy + 1.0);\n            const nx = -dhdx * invLen;\n            const ny = -dhdy * invLen;\n            const nz = invLen;\n\n            // Refracted floor lookup coordinates shifted by surface normal\n            const refX = Math.max(\n              0,\n              Math.min(simW - 1, Math.round(x + nx * 26))\n            );\n            const refY = Math.max(\n              0,\n              Math.min(simH - 1, Math.round(y + ny * 26))\n            );\n            const floorPattern = bedTex[refY * simW + refX];\n\n            // Ambient refracted caustic wave network on the pool floor\n            const cx = refX * 0.11 + timeShift;\n            const cy = refY * 0.11 - timeShift * 0.7;\n            const ambientCaustic = Math.pow(\n              0.5 + 0.5 * Math.sin(cx + Math.cos(cy * 1.3)) * Math.cos(cy - Math.sin(cx * 1.2)),\n              3.0\n            );\n\n            // Wave curvature convergence (Laplacian) creates bright caustic focal rings on the bed\n            const curvature = Math.max(\n              0,\n              bufCurr[idx] * 2.0 -\n                (bufCurr[idx - 1] + bufCurr[idx + 1]) * 0.5\n            );\n            const rippleCaustic = Math.min(1.4, curvature * 0.018);\n\n            // Schlick Fresnel reflection factor: grazing angles reflect more bright sky light\n            const fresnel = Math.pow(1.0 - nz, 3.0);\n\n            // Diffuse lighting + sharp Blinn-Phong specular highlight pow(max(dot(N, H), 0.0), 80.0)\n            const ndotl = Math.max(0.0, nx * lx + ny * ly + nz * lz);\n            const ndoth = Math.max(0.0, nx * hx + ny * hy + nz * hz);\n            const specular = Math.pow(ndoth, 80.0) * 1.85 + Math.pow(ndoth, 18.0) * 0.35;\n\n            // Combine deep water absorption + refracted floor + caustics + Fresnel sky + specular glint\n            const depthShade = 0.42 + 0.58 * (1.0 - vNorm * 0.45);\n            const floorLight =\n              floorPattern * depthShade * (0.55 + 0.45 * ndotl) +\n              ambientCaustic * 0.24 +\n              rippleCaustic * 0.55;\n\n            const rOut =\n              4 +\n              floorLight * tintR * 0.78 +\n              fresnel * 145 +\n              specular * 255;\n            const gOut =\n              14 +\n              floorLight * tintG * 0.92 +\n              fresnel * 215 +\n              specular * 252;\n            const bOut =\n              30 +\n              floorLight * tintB * 1.05 +\n              fresnel * 255 +\n              specular * 255;\n\n            const p = idx * 4;\n            data[p] = rOut > 255 ? 255 : rOut < 0 ? 0 : rOut;\n            data[p + 1] = gOut > 255 ? 255 : gOut < 0 ? 0 : gOut;\n            data[p + 2] = bOut > 255 ? 255 : bOut < 0 ? 0 : bOut;\n            data[p + 3] = 255;\n          }\n        }\n\n        offCtx.putImageData(imgData, 0, 0);\n\n        // Upscale smoothly onto full-resolution display canvas\n        ctx.imageSmoothingEnabled = true;\n        ctx.imageSmoothingQuality = \"high\";\n        ctx.drawImage(offscreen, 0, 0, viewW, viewH);\n      }\n\n      // Swap wave equation buffers\n      const temp = bufPrev;\n      bufPrev = bufCurr;\n      bufCurr = temp;\n\n      rafId = requestAnimationFrame(render);\n    };\n\n    rafId = requestAnimationFrame(render);\n\n    return () => {\n      cancelAnimationFrame(rafId);\n      ro.disconnect();\n    };\n  }, []);\n\n  const handlePointerMove = (e: React.PointerEvent<HTMLDivElement>) => {\n    if (!interactive || !containerRef.current) return;\n    const rect = containerRef.current.getBoundingClientRect();\n    const nx = (e.clientX - rect.left) / Math.max(1, rect.width);\n    const ny = (e.clientY - rect.top) / Math.max(1, rect.height);\n    disturbRef.current(nx, ny, 95);\n  };\n\n  const handlePointerDown = (e: React.PointerEvent<HTMLDivElement>) => {\n    if (!interactive || !containerRef.current) return;\n    const rect = containerRef.current.getBoundingClientRect();\n    const nx = (e.clientX - rect.left) / Math.max(1, rect.width);\n    const ny = (e.clientY - rect.top) / Math.max(1, rect.height);\n    disturbRef.current(nx, ny, 280);\n    setDropCounter((c) => c + 1);\n  };\n\n  return (\n    <div\n      ref={containerRef}\n      data-fullscreen-scroll\n      onPointerMove={handlePointerMove}\n      onPointerDown={handlePointerDown}\n      className={cn(\n        \"relative flex min-h-[520px] w-full flex-col justify-between overflow-hidden rounded-2xl border border-white/10 bg-[#030A18] p-6 text-white shadow-2xl select-none\",\n        interactive && \"cursor-crosshair\",\n        className\n      )}\n    >\n      <canvas\n        ref={canvasRef}\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-0 h-full w-full\"\n      />\n\n      {children && (\n        <div className=\"relative z-10 flex min-h-[520px] w-full flex-col\">\n          {children}\n        </div>\n      )}\n    </div>\n  );\n}\n\nexport default WaterRipples;\n",
      "type": "registry:ui",
      "target": "components/ui/water-ripples.tsx"
    },
    {
      "path": "lib/utils.ts",
      "content": "import { type ClassValue, clsx } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport async function copyToClipboard(text: string): Promise<boolean> {\n  if (typeof window === \"undefined\") return false;\n\n  try {\n    if (navigator.clipboard && window.isSecureContext) {\n      await navigator.clipboard.writeText(text);\n      return true;\n    }\n\n    const textArea = document.createElement(\"textarea\");\n    textArea.value = text;\n    textArea.style.position = \"fixed\";\n    textArea.style.left = \"-999999px\";\n    textArea.style.top = \"-999999px\";\n    document.body.appendChild(textArea);\n    textArea.focus();\n    textArea.select();\n    const succeeded = document.execCommand(\"copy\");\n    textArea.remove();\n    return succeeded;\n  } catch {\n    return false;\n  }\n}\n",
      "type": "registry:lib",
      "target": "lib/utils.ts"
    }
  ]
}
