# Integrate `Submerge` from Motion Garden

You are helping me add the **Submerge** component to an existing project.
Everything you need is below — full source, dependencies, props and the
behavioural contract. Read it all before writing anything.

- **Component:** Submerge
- **Category:** Power-User Systems
- **Summary:** Live content held under water. A slow current bends it continuously, a press drops a stone and a drag leaves a wake that trails behind the cursor. The text underneath stays selectable, the button still answers and the link is still a link, because the water bends the real tree rather than a picture of it.
- **Reference:** https://motion-garden.netlify.app/components/submerge
- **Full catalog (for you, the agent):** https://motion-garden.netlify.app/llms.txt

## Stack this source assumes

- React 19 with the Next.js App Router (never the `pages/` directory)
- TypeScript, strict
- Tailwind CSS v4, CSS-first config (utilities are inline; no `tailwind.config` needed)

If my project differs, adapt the integration — but do not rewrite the animation math to suit a different stack. Ask me first.

## Dependencies

```bash
# Submerge needs no npm packages
```

## Files to create

2 files — all of them are required. Paths are as shipped; the component may live anywhere in my project, but keep every peer file at the exact path shown so the import aliases resolve.

### 1. `components/registry/submerge/submerge.tsx`

```tsx
"use client";

import { memo, useEffect, useId, useRef, useState, type ReactNode } from "react";
import { useAnimationLoop, type Metrics } from "@/hooks/use-animation-loop";

export interface SubmergeProps {
  children?: ReactNode;
  depth?: number;
  flow?: number;
  turbulence?: number;
  rippleForce?: number;
  rippleSpread?: number;
  settle?: number;
  edgeHold?: number;
  waterColor?: string;
  deepColor?: string;
  paused?: boolean;
  reducedMotion?: boolean;
  className?: string;
}

const RIPPLES = 10;
const TILE = 320;
const FLOW_CELLS = 3;
const SPAWN_FRACTION = 0.09;
const SPRITE_RES = 160;
const WAKE_ANGLES = 16;

const NOSE = 0.45;
const TAIL = 1;
const SIDE = 0.62;
const PEAK_AT = 0.62;
const PEAK_WIDTH = 0.34;

const FLOW_AMP = 100;
const DISPLACE_BASE = 46;
const MAX_WEIGHT = 0.68;

const DEFAULT_GAP = 0.12;
const WAKE_LIFE_MAX = 0.85;
const WAKE_LIFE_MIN = 0.18;

const ringGrowth = (p: number) => (1 - Math.exp(-2.8 * p)) / (1 - Math.exp(-2.8));

const smoothstep = (a: number, b: number, x: number) => {
  const t = Math.min(1, Math.max(0, (x - a) / (b - a || 1e-6)));
  return t * t * (3 - 2 * t);
};

function makeNoise(cellsX: number, cellsY: number, seed: number) {
  const hash = (ix: number, iy: number) => {
    const x = ((ix % cellsX) + cellsX) % cellsX;
    const y = ((iy % cellsY) + cellsY) % cellsY;
    const n = Math.sin(x * 127.1 + y * 311.7 + seed * 74.7) * 43758.5453;
    return n - Math.floor(n);
  };
  return (u: number, v: number) => {
    const fx = u * cellsX;
    const fy = v * cellsY;
    const ix = Math.floor(fx);
    const iy = Math.floor(fy);
    const tx = fx - ix;
    const ty = fy - iy;
    const sx = tx * tx * (3 - 2 * tx);
    const sy = ty * ty * (3 - 2 * ty);
    const a = hash(ix, iy);
    const b = hash(ix + 1, iy);
    const c = hash(ix, iy + 1);
    const d = hash(ix + 1, iy + 1);
    return (a + (b - a) * sx) * (1 - sy) + (c + (d - c) * sx) * sy;
  };
}

function buildFlowTile(): HTMLCanvasElement | null {
  const canvas = document.createElement("canvas");
  canvas.width = TILE;
  canvas.height = TILE;
  const ctx = canvas.getContext("2d");
  if (!ctx) return null;

  const nx1 = makeNoise(FLOW_CELLS, FLOW_CELLS, 1);
  const ny1 = makeNoise(FLOW_CELLS, FLOW_CELLS, 2);
  const nx2 = makeNoise(FLOW_CELLS * 2, FLOW_CELLS * 2, 3);
  const ny2 = makeNoise(FLOW_CELLS * 2, FLOW_CELLS * 2, 4);

  const img = ctx.createImageData(TILE, TILE);
  const data = img.data;
  for (let y = 0; y < TILE; y++) {
    const v = y / TILE;
    for (let x = 0; x < TILE; x++) {
      const u = x / TILE;
      const dx = nx1(u, v) - 0.5 + (nx2(u, v) - 0.5) * 0.5;
      const dy = ny1(u, v) - 0.5 + (ny2(u, v) - 0.5) * 0.5;
      const i = (y * TILE + x) * 4;
      data[i] = Math.max(0, Math.min(255, 128 + dx * FLOW_AMP));
      data[i + 1] = Math.max(0, Math.min(255, 128 + dy * FLOW_AMP));
      data[i + 2] = Math.max(0, Math.min(255, 128 + (dx + dy) * FLOW_AMP * 0.43));
      data[i + 3] = 255;
    }
  }
  ctx.putImageData(img, 0, 0);
  return canvas;
}

let FLOW_TILE: HTMLCanvasElement | null | undefined;
function flowTile() {
  if (FLOW_TILE === undefined) FLOW_TILE = buildFlowTile();
  return FLOW_TILE;
}

function buildFlowMap(w: number, h: number): string {
  const tile = flowTile();
  if (!tile) return "";
  const canvas = document.createElement("canvas");
  canvas.width = Math.max(1, Math.round(w));
  canvas.height = Math.max(1, Math.round(h));
  const ctx = canvas.getContext("2d");
  if (!ctx) return "";
  const pattern = ctx.createPattern(tile, "repeat");
  if (!pattern) return "";
  ctx.fillStyle = pattern;
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  return canvas.toDataURL();
}

function buildDisturbance(heading: number): string {
  const canvas = document.createElement("canvas");
  canvas.width = SPRITE_RES;
  canvas.height = SPRITE_RES;
  const ctx = canvas.getContext("2d");
  if (!ctx) return "";
  const img = ctx.createImageData(SPRITE_RES, SPRITE_RES);
  const data = img.data;
  const half = SPRITE_RES / 2;
  const directional = heading >= 0;
  const ch = Math.cos(heading);
  const sh = Math.sin(heading);

  for (let y = 0; y < SPRITE_RES; y++) {
    for (let x = 0; x < SPRITE_RES; x++) {
      const dx = (x - half) / half;
      const dy = (y - half) / half;
      const i = (y * SPRITE_RES + x) * 4;

      const s = directional ? dx * ch + dy * sh : dx;
      const t = directional ? -dx * sh + dy * ch : dy;
      const rs = directional ? (s > 0 ? s / NOSE : s / TAIL) : s;
      const rt = directional ? t / SIDE : t;
      const r = Math.hypot(rs, rt);

      const q = (r - PEAK_AT) / PEAK_WIDTH;
      let push = 0;
      if (q > -1 && q < 1) {
        const win = Math.cos(q * Math.PI * 0.5);
        push = Math.sin(q * Math.PI) * win * win;
        if (directional) {
          const m = Math.hypot(s, t);
          const c = m > 1e-5 ? s / m : 0;
          push *= 0.05 + 0.95 * Math.pow(0.5 - 0.5 * c, 1.35);
        }
      }

      const mag = Math.abs(push);
      if (mag < 1e-3) {
        data[i] = 128;
        data[i + 1] = 128;
        data[i + 2] = 128;
        data[i + 3] = 0;
        continue;
      }
      const sgn = push > 0 ? 1 : -1;
      const len = Math.hypot(dx, dy) || 1;
      data[i] = Math.round(128 + (dx / len) * sgn * 127);
      data[i + 1] = Math.round(128 + (dy / len) * sgn * 127);
      data[i + 2] = Math.round(128 + sgn * 110);
      data[i + 3] = Math.round(Math.min(1, mag) * 255);
    }
  }
  ctx.putImageData(img, 0, 0);
  return canvas.toDataURL();
}

let SHAPES: { ring: string; wakes: string[] } | null = null;
function shapes() {
  if (!SHAPES) {
    SHAPES = {
      ring: buildDisturbance(-1),
      wakes: Array.from({ length: WAKE_ANGLES }, (_, i) =>
        buildDisturbance((i / WAKE_ANGLES) * Math.PI * 2),
      ),
    };
  }
  return SHAPES;
}

function buildTaper(w: number, h: number, hold: number): string {
  const canvas = document.createElement("canvas");
  canvas.width = Math.max(1, Math.round(w));
  canvas.height = Math.max(1, Math.round(h));
  const ctx = canvas.getContext("2d");
  if (!ctx) return "";
  const band = Math.max(
    1,
    Math.min(canvas.width, canvas.height) * 0.5 * Math.min(Math.max(hold, 0), 1),
  );
  const img = ctx.createImageData(canvas.width, canvas.height);
  const data = img.data;
  for (let y = 0; y < canvas.height; y++) {
    for (let x = 0; x < canvas.width; x++) {
      const d = Math.min(x, y, canvas.width - 1 - x, canvas.height - 1 - y);
      const t = band <= 1 ? 1 : smoothstep(0, band, d);
      const c = Math.round(t * 255);
      const i = (y * canvas.width + x) * 4;
      data[i] = c;
      data[i + 1] = c;
      data[i + 2] = c;
      data[i + 3] = 255;
    }
  }
  ctx.putImageData(img, 0, 0);
  return canvas.toDataURL();
}

const hexToRgb01 = (hex: string): [number, number, number] => {
  const h = hex.replace("#", "");
  const full = h.length === 3 ? h.split("").map((c) => c + c).join("") : h;
  const n = parseInt(full, 16);
  if (Number.isNaN(n) || full.length !== 6) return [1, 1, 1];
  return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];
};

interface Ripple {
  x: number;
  y: number;
  age: number;
  alive: boolean;
  href: string;
  r0: number;
  r1: number;
  life: number;
  gain: number;
}

const Submerge = memo(
  ({
    children,
    depth = 0.45,
    flow = 1.25,
    turbulence = 0.5,
    rippleForce = 0.8,
    rippleSpread = 1,
    settle = 1.6,
    edgeHold = 0.35,
    waterColor = "#bacffe",
    deepColor = "#d9e7f3",
    paused = false,
    reducedMotion = false,
    className,
  }: SubmergeProps) => {
    const containerRef = useRef<HTMLDivElement>(null);
    const flowRef = useRef<SVGFEImageElement>(null);
    const dispRef = useRef<SVGFEDisplacementMapElement>(null);
    const spriteRefs = useRef<(SVGFEImageElement | null)[]>([]);
    const gateRefs = useRef<(SVGFEComponentTransferElement | null)[]>([]);
    const weightRefs = useRef<(SVGFEFuncAElement | null)[]>([]);

    const rawId = useId();
    const filterId = `sg-submerge-${rawId.replace(/[^a-zA-Z0-9_-]/g, "")}`;

    const box = useRef({ w: 0, h: 0 });
    const pointer = useRef({
      lastX: NaN,
      lastY: NaN,
      travel: 0,
      lastSpawn: 0,
      gap: DEFAULT_GAP,
    });
    const ripples = useRef<Ripple[]>(
      Array.from({ length: RIPPLES }, () => ({
        x: 0.5, y: 0.5, age: 0, alive: false, href: "",
        r0: 0.04, r1: 0.44, life: 1, gain: 0.7,
      })),
    );
    const nextSlot = useRef(0);
    const heading = useRef({ x: 1, y: 0 });

    const [maps, setMaps] = useState({ flow: "", taper: "", w: 0, h: 0 });

    const live = useRef({
      depth, flow, turbulence, rippleForce, rippleSpread, settle,
      edgeHold, waterColor, deepColor, reducedMotion,
    });
    live.current = {
      depth, flow, turbulence, rippleForce, rippleSpread, settle,
      edgeHold, waterColor, deepColor, reducedMotion,
    };

    const rebuild = useRef<(() => void) | null>(null);
    rebuild.current = () => {
      const { w, h } = box.current;
      if (w < 2 || h < 2) return;
      shapes();
      setMaps({
        flow: buildFlowMap(w + TILE, h + TILE),
        taper: buildTaper(w, h, live.current.edgeHold),
        w,
        h,
      });
    };

    const loop = useAnimationLoop({
      target: containerRef,
      halted: paused || reducedMotion,
      onResize: (m: Metrics) => {
        const changed =
          Math.abs(m.width - box.current.w) > 1 ||
          Math.abs(m.height - box.current.h) > 1;
        box.current = { w: m.width, h: m.height };
        if (changed) rebuild.current?.();
      },
      onFrame: ({ dt, elapsed }) => {
        const l = live.current;
        const { w, h } = box.current;
        if (w < 2 || h < 2) return;

        const flowEl = flowRef.current;
        if (flowEl) {
          const t = elapsed * l.flow;
          const ox = -(((t * 19) % TILE) + TILE) % TILE;
          const oy = -(((t * 13) % TILE) + TILE) % TILE;
          flowEl.setAttribute("x", ox.toFixed(2));
          flowEl.setAttribute("y", oy.toFixed(2));
        }

        const long = Math.max(w, h);
        for (let i = 0; i < RIPPLES; i++) {
          const r = ripples.current[i];
          const img = spriteRefs.current[i];
          const gate = gateRefs.current[i];
          const weight = weightRefs.current[i];

          const lifetime = Math.max(l.settle * r.life, 0.05);
          if (r.alive) {
            r.age += dt;
            if (r.age >= lifetime) r.alive = false;
          }

          if (!r.alive) {
            if (weight) weight.setAttribute("slope", "0");
            continue;
          }

          const p = r.age / lifetime;
          const radius =
            (r.r0 + (r.r1 - r.r0) * ringGrowth(p)) * long * l.rippleSpread;
          const strength = Math.min(
            MAX_WEIGHT,
            Math.min(1, p / 0.12) * (1 - p) * (1 - p) * l.rippleForce * r.gain,
          );

          const bx = (r.x * w - radius).toFixed(2);
          const by = (r.y * h - radius).toFixed(2);
          const bs = (radius * 2).toFixed(2);

          if (img) {
            if (img.getAttribute("href") !== r.href) {
              img.setAttribute("href", r.href);
            }
            img.setAttribute("x", bx);
            img.setAttribute("y", by);
            img.setAttribute("width", bs);
            img.setAttribute("height", bs);
          }
          if (gate) {
            gate.setAttribute("x", bx);
            gate.setAttribute("y", by);
            gate.setAttribute("width", bs);
            gate.setAttribute("height", bs);
          }
          if (weight) weight.setAttribute("slope", strength.toFixed(4));
        }

        const disp = dispRef.current;
        if (disp) {
          const push = l.turbulence * DISPLACE_BASE * (0.55 + l.depth * 0.9);
          disp.setAttribute("scale", push.toFixed(2));
        }
      },
      deps: [],
    });

    useEffect(() => {
      rebuild.current?.();
    }, [edgeHold]);

    useEffect(() => {
      loop.paint();
    }, [
      depth, flow, turbulence, rippleForce, rippleSpread, settle,
      waterColor, deepColor, maps, loop,
    ]);

    const spawn = (x: number, y: number, dirX: number, dirY: number) => {
      const slot = nextSlot.current;
      const r = ripples.current[slot];
      const sh = shapes();
      if (dirX !== 0 || dirY !== 0) {
        const angle = Math.atan2(dirY, dirX);
        const bucket =
          ((Math.round((angle / (Math.PI * 2)) * WAKE_ANGLES) % WAKE_ANGLES) +
            WAKE_ANGLES) %
          WAKE_ANGLES;
        r.href = sh.wakes[bucket];
        r.r0 = 0.075;
        r.r1 = 0.18;
        r.life = Math.max(
          WAKE_LIFE_MIN,
          Math.min(
            WAKE_LIFE_MAX,
            ((RIPPLES - 1) * pointer.current.gap) / Math.max(live.current.settle, 0.05),
          ),
        );
        r.gain = 0.62;
      } else {
        r.href = sh.ring;
        r.r0 = 0.04;
        r.r1 = 0.44;
        r.life = 1;
        r.gain = 0.85;
      }
      r.x = x;
      r.y = y;
      r.age = 0;
      r.alive = true;
      nextSlot.current = (slot + 1) % RIPPLES;
    };

    const track = (e: React.PointerEvent<HTMLDivElement>) => {
      if (live.current.reducedMotion) return;
      const rect = e.currentTarget.getBoundingClientRect();
      if (rect.width === 0 || rect.height === 0) return;
      const px = e.clientX - rect.left;
      const py = e.clientY - rect.top;
      const p = pointer.current;

      if (Number.isNaN(p.lastX)) {
        p.lastX = px;
        p.lastY = py;
        p.travel = 0;
        return;
      }
      const dx = px - p.lastX;
      const dy = py - p.lastY;
      p.travel += Math.hypot(dx, dy);
      p.lastX = px;
      p.lastY = py;
      const step = Math.max(10, Math.min(rect.width, rect.height) * SPAWN_FRACTION);
      if (p.travel < step) return;
      p.travel = 0;

      const now = e.timeStamp;
      if (p.lastSpawn > 0) {
        const dtSpawn = Math.min(0.5, Math.max(0.008, (now - p.lastSpawn) / 1000));
        p.gap += (dtSpawn - p.gap) * 0.4;
      }
      p.lastSpawn = now;

      const len = Math.hypot(dx, dy);
      if (len > 1e-5) {
        const hd = heading.current;
        hd.x += (dx / len - hd.x) * 0.35;
        hd.y += (dy / len - hd.y) * 0.35;
        const hl = Math.hypot(hd.x, hd.y) || 1;
        hd.x /= hl;
        hd.y /= hl;
      }
      const hd = heading.current;
      spawn(
        (px - hd.x * step * 0.8) / rect.width,
        (py - hd.y * step * 0.8) / rect.height,
        hd.x,
        hd.y,
      );
      loop.start();
    };

    const press = (e: React.PointerEvent<HTMLDivElement>) => {
      if (live.current.reducedMotion) return;
      const rect = e.currentTarget.getBoundingClientRect();
      if (rect.width === 0 || rect.height === 0) return;
      spawn(
        (e.clientX - rect.left) / rect.width,
        (e.clientY - rect.top) / rect.height,
        0,
        0,
      );
      loop.start();
    };

    const release = () => {
      pointer.current.lastX = NaN;
      pointer.current.lastY = NaN;
      pointer.current.lastSpawn = 0;
    };

    const ready = maps.flow !== "" && maps.taper !== "";
    const deep = hexToRgb01(deepColor);
    const deepCss = `rgb(${Math.round(deep[0] * 255)} ${Math.round(deep[1] * 255)} ${Math.round(deep[2] * 255)})`;

    return (
      <div
        ref={containerRef}
        className={
          className ?? "relative isolate touch-none overflow-hidden select-none"
        }
        onPointerMove={track}
        onPointerDown={press}
        onPointerLeave={release}
        onPointerCancel={release}
      >
        <svg aria-hidden className="absolute h-0 w-0" focusable="false">
          <defs>
            <filter
              id={filterId}
              colorInterpolationFilters="sRGB"
              x="0"
              y="0"
              width="100%"
              height="100%"
            >
              {ready ? (
                <>
                  <feFlood
                    floodColor="rgb(128,128,128)"
                    floodOpacity="1"
                    result="acc0"
                  />
                  {Array.from({ length: RIPPLES }, (_, i) => (
                    <feImage
                      key={`spr-${i}`}
                      ref={(el) => {
                        spriteRefs.current[i] = el;
                      }}
                      x="0"
                      y="0"
                      width="0"
                      height="0"
                      preserveAspectRatio="none"
                      result={`spr${i}`}
                    />
                  ))}
                  {Array.from({ length: RIPPLES }, (_, i) => (
                    <feComponentTransfer
                      key={`gate-${i}`}
                      ref={(el) => {
                        gateRefs.current[i] = el;
                      }}
                      in={`spr${i}`}
                      x="0"
                      y="0"
                      width="0"
                      height="0"
                      result={`gate${i}`}
                    >
                      <feFuncA
                        ref={(el) => {
                          weightRefs.current[i] = el;
                        }}
                        type="linear"
                        slope="0"
                        intercept="0"
                      />
                    </feComponentTransfer>
                  ))}
                  {Array.from({ length: RIPPLES }, (_, i) => (
                    <feComposite
                      key={`mix-${i}`}
                      in={`gate${i}`}
                      in2={`acc${i}`}
                      operator="over"
                      result={`acc${i + 1}`}
                    />
                  ))}

                  <feColorMatrix
                    in={`acc${RIPPLES}`}
                    type="matrix"
                    values="0 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 2.78 0 -1.395"
                    result="crestMask"
                  />
                  <feFlood
                    floodColor={waterColor}
                    floodOpacity="1"
                    result="crestTint"
                  />
                  <feComposite
                    in="crestTint"
                    in2="crestMask"
                    operator="in"
                    result="crestRaw"
                  />
                  <feComponentTransfer in="crestRaw" result="crest">
                    <feFuncA type="linear" slope="0.85" intercept="0" />
                  </feComponentTransfer>

                  <feImage
                    ref={flowRef}
                    href={maps.flow}
                    x="0"
                    y="0"
                    width={maps.w + TILE}
                    height={maps.h + TILE}
                    preserveAspectRatio="none"
                    result="current"
                  />
                  <feComposite
                    in={`acc${RIPPLES}`}
                    in2="current"
                    operator="arithmetic"
                    k1="0"
                    k2="1"
                    k3="1"
                    k4="-0.5"
                    result="mixed"
                  />
                  <feImage
                    href={maps.taper}
                    x="0"
                    y="0"
                    width={maps.w}
                    height={maps.h}
                    preserveAspectRatio="none"
                    result="taper"
                  />
                  <feComposite
                    in="mixed"
                    in2="taper"
                    operator="arithmetic"
                    k1="1"
                    k2="0"
                    k3="-0.5"
                    k4="0.5"
                    result="surface"
                  />
                  <feDisplacementMap
                    ref={dispRef}
                    in="SourceGraphic"
                    in2="surface"
                    scale="0"
                    xChannelSelector="R"
                    yChannelSelector="G"
                    result="bent"
                  />
                  <feBlend in="crest" in2="bent" mode="screen" />
                </>
              ) : null}
            </filter>
          </defs>
        </svg>

        <div
          className="relative h-full w-full"
          style={{
            filter: ready && !reducedMotion ? `url(#${filterId})` : undefined,
          }}
        >
          {children}
          <div
            aria-hidden
            className="pointer-events-none absolute inset-0"
            style={{
              background: deepCss,
              mixBlendMode: "multiply",
              opacity: Math.min(0.85, depth * 0.8),
            }}
          />
        </div>
      </div>
    );
  },
);

Submerge.displayName = "Submerge";

export default Submerge;
```

### 2. `hooks/use-animation-loop.ts` — peer hook, required

```tsx
"use client";

import { useCallback, useEffect, useLayoutEffect, useMemo, useRef } from "react";

export interface Metrics {
  width: number;
  height: number;
  dpr: number;
  bufferWidth: number;
  bufferHeight: number;
}

export interface FrameInfo {
  now: number;
  dt: number;
  elapsed: number;
  frame: number;
}

export interface AnimationLoopHandle {
  start(): void;
  stop(): void;
  paint(): void;
  resize(): void;
  readonly running: boolean;
}

export interface AnimationLoopOptions {
  target: React.RefObject<HTMLElement | null>;
  halted?: boolean;
  dpr?: number | "auto";
  onResize?(metrics: Metrics): void;
  onFrame?(info: FrameInfo): void | false;
  paintWhenHalted?: boolean;
  resizeDebounceMs?: number;
  gl?(): WebGLRenderingContext | WebGL2RenderingContext | null | undefined;
  onDispose?(): void;
  deps?: unknown[];
}

const MAX_DT = 1 / 15;

export function useAnimationLoop(
  options: AnimationLoopOptions,
): AnimationLoopHandle {
  const opts = useRef(options);
  useLayoutEffect(() => {
    opts.current = options;
  });

  const raf = useRef<number | null>(null);
  const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
  const running = useRef(false);
  const disposed = useRef(false);
  const startedAt = useRef(0);
  const last = useRef(0);
  const frame = useRef(0);

  const measure = useCallback((): Metrics | null => {
    const el = opts.current.target.current;
    if (!el) return null;
    const rect = el.getBoundingClientRect();
    const cap = opts.current.dpr ?? "auto";
    const limit = cap === "auto" ? 2 : cap;
    const dpr = Math.min(
      typeof window === "undefined" ? 1 : window.devicePixelRatio || 1,
      limit,
    );
    return {
      width: rect.width,
      height: rect.height,
      dpr,
      bufferWidth: Math.max(1, Math.round(rect.width * dpr)),
      bufferHeight: Math.max(1, Math.round(rect.height * dpr)),
    };
  }, []);

  const tick = useCallback(function tick(now: number) {
    if (disposed.current) return;

    if (startedAt.current === 0) startedAt.current = now;
    const dt = last.current === 0 ? 0 : Math.min((now - last.current) / 1000, MAX_DT);
    last.current = now;

    const info: FrameInfo = {
      now,
      dt,
      elapsed: (now - startedAt.current) / 1000,
      frame: frame.current++,
    };

    const verdict = opts.current.onFrame?.(info);

    if (disposed.current) return;

    if (verdict === false || opts.current.halted) {
      running.current = false;
      raf.current = null;
      return;
    }
    raf.current = requestAnimationFrame(tick);
  }, []);

  const start = useCallback(() => {
    if (disposed.current || running.current) return;
    running.current = true;
    last.current = 0;
    raf.current = requestAnimationFrame(tick);
  }, [tick]);

  const stop = useCallback(() => {
    running.current = false;
    if (raf.current !== null) {
      cancelAnimationFrame(raf.current);
      raf.current = null;
    }
  }, []);

  const paint = useCallback(() => start(), [start]);

  const resize = useCallback(() => {
    const metrics = measure();
    if (!metrics) return;
    opts.current.onResize?.(metrics);
    if (opts.current.paintWhenHalted !== false) paint();
    else if (!opts.current.halted) start();
  }, [measure, paint, start]);

  const deps = options.deps ?? [];

  useEffect(() => {
    disposed.current = false;
    const el = opts.current.target.current;
    if (!el) return;

    const run = () => {
      if (disposed.current) return;
      resize();
    };

    const observer = new ResizeObserver(() => {
      const wait = opts.current.resizeDebounceMs ?? 0;
      if (wait <= 0) {
        run();
        return;
      }
      if (timer.current !== null) clearTimeout(timer.current);
      timer.current = setTimeout(() => {
        timer.current = null;
        run();
      }, wait);
    });
    observer.observe(el);

    resize();
    if (!opts.current.halted) start();

    return () => {
      disposed.current = true;
      running.current = false;
      if (raf.current !== null) {
        cancelAnimationFrame(raf.current);
        raf.current = null;
      }
      if (timer.current !== null) {
        clearTimeout(timer.current);
        timer.current = null;
      }
      observer.disconnect();
      opts.current.onDispose?.();
      const context = opts.current.gl?.();
      context?.getExtension("WEBGL_lose_context")?.loseContext();
      startedAt.current = 0;
      last.current = 0;
      frame.current = 0;
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, deps);

  const halted = options.halted ?? false;
  useEffect(() => {
    if (!halted) start();
  }, [halted, start]);

  return useMemo<AnimationLoopHandle>(
    () => ({
      start,
      stop,
      paint,
      resize,
      get running() {
        return running.current;
      },
    }),
    [start, stop, paint, resize],
  );
}
```


## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `depth` | `number` | `0.45` | How deep the content sits. It drives the colour wash and adds to the bend, because the further under a surface something lies the further the same slope carries the light across it. Past about 0.8 the water starts winning and the content stops being the subject. Range 0–1. |
| `flow` | `number` | `1.25` | Speed of the ambient current. The field is rasterised once as a seamless tile and scrolled rather than regenerated — regenerating costs a PNG encode per frame, which is why so many browser water effects settle for a static distortion instead. Range 0–2 ×. |
| `turbulence` | `number` | `0.5` | How hard the current bends the content. This is genuine per-pixel displacement of the live tree, not a transform — a transform can only move the whole box, which is the reason skewed 'underwater' text never convinces. Range 0–2. |
| `rippleForce` | `number` | `0.8` | Strength of the disturbance the pointer leaves. A press radiates an isotropic ring; a drag leaves a wake — the same wave packet stretched along travel and weighted to the rear, so the water is disturbed where the pointer has been rather than around where it is. Sources are spaced by distance travelled rather than by pointer event, so the same gesture leaves the same wake at 60Hz or 120Hz. Range 0–2 ×. |
| `rippleSpread` | `number` | `1` | How far a disturbance travels before it dies. Growth is an exponential approach rather than a square root: both decelerate, but a square root has infinite slope at zero, so every ring is born already moving and the birth itself snaps. A drag wake spreads slower than a press ring, because a trail lingers where it was left. Range 0.3–3 ×. |
| `settle` | `number` | `1.6` | Seconds a disturbance takes to die away. Twelve run at once and a thirteenth reuses the oldest slot, so a fast drag always shows its newest wake rather than queueing behind stale ones — and twelve is enough that eviction rarely cuts one off mid-life, which is the pop that reads as jumpy. Range 0.4–5 s. |
| `edgeHold` | `number` | `0.35` | How much of the element's own silhouette is protected. Displacing evenly moves the border too, so a rounded card dissolves into an amoeba and the one edge a reader uses to locate it is the first thing destroyed. At zero the outline shreds; past about half the water is confined to the middle. Range 0–1. |
| `waterColor` | `string (hex)` | `"#bacffe"` | Shallow water — the colour every ring crest carries. |
| `deepColor` | `string (hex)` | `"#d9e7f3"` | Deep water. Applied as a multiply rather than an overlay, so the content keeps its own contrast instead of flattening behind a sheet of colour. |

## Usage

```tsx
import Submerge from "@/components/registry/submerge/submerge";

<Submerge
  depth={0.45}
  flow={1.25}
  turbulence={0.5}
  rippleForce={0.8}
  rippleSpread={1}
  settle={1.6}
  edgeHold={0.35}
  waterColor="#bacffe"
  deepColor="#d9e7f3"
/>
```

The import path matches the file path above; change it if you move the component. Every value shown is the documented default — pass only what you actually want to change. The snippet lists the tunable props only: if the component's props type in the source requires others (content, handlers, data), pass those too.

## Integration rules

- Keep the `"use client"` directive. This is a client component and will not render as a Server Component.
- It fills its parent. Give the wrapper an explicit height (or a definite grid area) — against a height-auto parent it collapses to nothing.
- It takes a `reducedMotion` prop and quiesces when true. Wire it to a `prefers-reduced-motion` listener rather than hardcoding `false`.
- It runs a free-running animation loop. Halt it when off-screen or backgrounded if you care about battery.
- The `touch-action: none` on the hit target is load-bearing — without it a finger drag scrolls the page instead of driving the effect. Do not strip it as a stray utility class.
- The peer hook is required, not optional. Copy it to `hooks/use-animation-loop.ts` so the component's `@/hooks/use-animation-loop` import resolves.
- It owns a GPU/canvas context and releases it on unmount. Preserve the cleanup return — leaking contexts kills the tab after a few navigations.

## Your task

1. Tell me where in my project this should live and why.
2. Create the files above verbatim.
3. Wire the component into the place I point you at, with sensible props for that context.
4. Flag anything in my setup that conflicts with the stack assumptions above.

Do not restyle the component or alter the animation math unless I ask.
