{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "swell",
  "type": "registry:ui",
  "title": "Swell",
  "description": "Sprung wave lattice: depth-graded rows of smooth lines ride an orbital noise field with woven coherence while a slow autonomous swell front rolls through every ~8s. Cursor swipes inject velocity impulses along the travel direction, proportional to cursor speed, then spring home on damped Hooke springs with a tanh soft-clip on stretch. Deep-sea steel default. Canvas 2D, seeded, offscreen-paused, DPR-clamped with device-pixel-aligned strokes, reduced-motion-safe (crest frozen mid-roll); idles on its own when untouched — a seeded ghost cursor swipes through.",
  "dependencies": [],
  "registryDependencies": [
    "https://crucibleui.com/r/use-visibility-pause.json",
    "https://crucibleui.com/r/use-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/default/backgrounds/swell/swell.tsx",
      "type": "registry:ui",
      "target": "components/crucible/swell.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nimport { cn } from \"@/lib/utils\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { useVisibilityPause } from \"@/hooks/use-visibility-pause\";\n\n/**\n * The default spring feel of the lattice, exported so you can reuse or riff on\n * it: `tension` is the Hooke pull-back stiffness (per s²), `friction` the\n * per-frame velocity retention at 60fps (applied frame-rate-independently),\n * `maxStretch` the CSS-px displacement the soft-clip saturates toward.\n */\nexport const DEFAULT_SPRING = {\n  tension: 150,\n  friction: 0.93,\n  maxStretch: 48,\n} as const;\n\nexport interface SwellProps extends React.ComponentPropsWithoutRef<\"div\"> {\n  /**\n   * `\"horizontal\"` — rows of swell lines, depth rising toward the bottom of the\n   * frame (an open-sea read). `\"vertical\"` — hanging curtains of line with\n   * seeded depth layers. @default \"horizontal\"\n   */\n  orientation?: \"horizontal\" | \"vertical\";\n  /**\n   * Lattice spacing in CSS px: `x` = horizontal spacing, `y` = vertical\n   * spacing. For horizontal orientation `y` sets the row gap and `x` the\n   * sample step along each row (vertical orientation swaps roles). Each axis\n   * clamped 8–200. @default { x: 28, y: 26 }\n   */\n  gap?: { x: number; y: number };\n  /** Idle wave amplitude in CSS px — how far points orbit their anchors. @default 16 */\n  amplitude?: number;\n  /** Animation-speed multiplier for drift and the swell front. 1 = default. @default 1 */\n  speed?: number;\n  /** Line brightness, 0–2. @default 1 */\n  intensity?: number;\n  /**\n   * Spring stiffness pulling displaced points home (per s²). Higher = faster,\n   * tighter snap-back. Clamped 1–600. @default 150\n   */\n  tension?: number;\n  /**\n   * Spring velocity retention per frame at 60fps, 0–0.999 (applied\n   * frame-rate-independently). Lower = deader, higher = wobblier. @default 0.93\n   */\n  friction?: number;\n  /**\n   * CSS-px displacement the cursor can stretch a point toward. Rendered\n   * through a tanh soft-clip, so stretch saturates smoothly — never a hard\n   * stop. @default 48\n   */\n  maxStretch?: number;\n  /**\n   * Period in seconds of the autonomous swell front — a slow pressure wave\n   * that rolls through the lattice so the field breathes with no cursor.\n   * @default 8\n   */\n  swellInterval?: number;\n  /**\n   * Two tones `[far, near]` graded across row depth. Defaults to deep-sea\n   * steel: dim slate far rows rising to cyan-tinged near rows. Pass warm tones\n   * for a dune-wind look. @default [\"#31404f\", \"#7dd3fc\"]\n   */\n  colors?: [string, string];\n  /**\n   * When true, cursor swipes inject velocity impulses along the travel\n   * direction, proportional to smoothed cursor speed, then spring back.\n   * @default true\n   */\n  interactive?: boolean;\n  /**\n   * Idle autoplay: after ~1.5s with no real pointer, a seeded ghost cursor\n   * sweeps slow figure-eights through the lattice (with an occasional faster\n   * swipe) so thumbnails and heroes never sit still; the real pointer takes\n   * over instantly. Requires `interactive`. Never runs under reduced motion.\n   * @default true\n   */\n  idle?: boolean;\n  /** Deterministic seed for depth layout and drift phases. @default 1 */\n  seed?: number;\n  /** devicePixelRatio ceiling. Clamped 1–2. @default 2 */\n  dpr?: number;\n  /** Freeze at a static frame with the swell front mid-roll (never flat). @default false */\n  paused?: boolean;\n}\n\nconst DEFAULT_COLORS: [string, string] = [\"#31404f\", \"#7dd3fc\"];\n/** Deep-sea near-black bed — cool navy, never flat #000. */\nconst BG: [number, number, number] = [4, 9, 15];\n\nconst TAU = Math.PI * 2;\n/** Spatial size of one noise feature in CSS px. */\nconst FEATURE_PX = 240;\n/** How many full turns the noise value winds the orbital angle through. */\nconst ANGLE_TURNS = 1.6;\n/** Swell-front contribution to the orbital angle (radians at crest). */\nconst SWELL_ANGLE = 1.05;\n/** Swell-front amplitude boost at crest (fraction of base amplitude). */\nconst SWELL_AMP = 0.45;\n/** Frozen noise time for the reduced-motion / paused frame. */\nconst NOISE_T_STATIC = 3.7;\n/** Cursor influence radius, CSS px. */\nconst POINTER_RADIUS = 130;\n/** Impulse gain: cursor speed → point velocity injection. */\nconst INJECT = 3.1;\n/** Smoothed cursor speed cap, CSS px/s. */\nconst MAX_POINTER_SPEED = 2600;\n/** Extra kick on the transverse axis — the component the eye reads on a polyline. */\nconst TRANSVERSE_BOOST = 1.35;\n/** Exponential time constants (seconds): enter/leave ease, velocity smoothing, velocity decay. */\nconst EASE_TAU = 0.22;\nconst VEL_TAU = 0.05;\nconst VEL_DECAY_TAU = 0.12;\n/** Hard cap on total lattice points; the along-line step grows past this. */\nconst MAX_POINTS = 4200;\n/** Ghost cursor takes over this long after the last real pointer movement. */\nconst GHOST_DELAY_MS = 1500;\n/** Ghost presence — a softer hand than a real swipe. */\nconst GHOST_PRESENCE = 0.85;\n\nfunction hexToRgb(hex: string): [number, number, number] {\n  let h = hex.replace(\"#\", \"\").trim();\n  if (h.length === 3) h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2];\n  const int = parseInt(h, 16);\n  if (Number.isNaN(int) || h.length !== 6) return [125, 211, 252];\n  return [(int >> 16) & 255, (int >> 8) & 255, int & 255];\n}\n\n/** Deterministic mulberry32 PRNG — same seed, same sea. */\nfunction mulberry32(seed: number): () => number {\n  let a = seed >>> 0;\n  return () => {\n    a |= 0;\n    a = (a + 0x6d2b79f5) | 0;\n    let t = Math.imul(a ^ (a >>> 15), 1 | a);\n    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;\n    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n  };\n}\n\n/** Integer hash → [0,1), stable across SSR intent and client. */\nfunction hash3(ix: number, iy: number, iz: number, seed: number): number {\n  let h = seed >>> 0;\n  h = Math.imul(h ^ (ix | 0), 0x27d4eb2f);\n  h = Math.imul(h ^ (iy | 0), 0x85ebca6b);\n  h = Math.imul(h ^ (iz | 0), 0xc2b2ae35);\n  h ^= h >>> 15;\n  return (h >>> 0) / 4294967296;\n}\n\nfunction fade(t: number): number {\n  return t * t * t * (t * (t * 6 - 15) + 10);\n}\n\nfunction lerp(a: number, b: number, t: number): number {\n  return a + (b - a) * t;\n}\n\n/** Trilinear value noise in 3D — smooth, seeded, morphs along z (time). */\nfunction vnoise3(x: number, y: number, z: number, seed: number): number {\n  const ix = Math.floor(x);\n  const iy = Math.floor(y);\n  const iz = Math.floor(z);\n  const ux = fade(x - ix);\n  const uy = fade(y - iy);\n  const uz = fade(z - iz);\n\n  const c000 = hash3(ix, iy, iz, seed);\n  const c100 = hash3(ix + 1, iy, iz, seed);\n  const c010 = hash3(ix, iy + 1, iz, seed);\n  const c110 = hash3(ix + 1, iy + 1, iz, seed);\n  const c001 = hash3(ix, iy, iz + 1, seed);\n  const c101 = hash3(ix + 1, iy, iz + 1, seed);\n  const c011 = hash3(ix, iy + 1, iz + 1, seed);\n  const c111 = hash3(ix + 1, iy + 1, iz + 1, seed);\n\n  const x00 = lerp(c000, c100, ux);\n  const x10 = lerp(c010, c110, ux);\n  const x01 = lerp(c001, c101, ux);\n  const x11 = lerp(c011, c111, ux);\n  return lerp(lerp(x00, x10, uy), lerp(x01, x11, uy), uz);\n}\n\n/** Two-octave value noise, ~[0,1] — one main eval plus a cheap detail term. */\nfunction fbm2(x: number, y: number, z: number, seed: number): number {\n  return (\n    vnoise3(x, y, z, seed) * 0.72 +\n    vnoise3(x * 2.13, y * 2.13, z * 1.7 + 9.2, seed + 101) * 0.28\n  );\n}\n\ninterface Line {\n  /** Index of the line's first point in the flat point arrays. */\n  start: number;\n  count: number;\n  /** Depth 0 (far) → 1 (near): drives opacity, width, drift rate, amplitude. */\n  z: number;\n  rgb: [number, number, number];\n  alphaBase: number;\n  width: number;\n  /** Per-row noise-drift rate — depth-graded so rows parallax. */\n  tRate: number;\n  tPhase: number;\n}\n\ninterface Tuning {\n  speed: number;\n  intensity: number;\n  amplitude: number;\n  tension: number;\n  friction: number;\n  maxStretch: number;\n  swellInterval: number;\n  interactive: boolean;\n  idle: boolean;\n}\n\ninterface Internals {\n  ctx: CanvasRenderingContext2D;\n  w: number;\n  h: number;\n  horizontal: boolean;\n  lines: Line[];\n  /** Anchor positions, CSS px. */\n  bx: Float32Array;\n  by: Float32Array;\n  /** Spring displacement + velocity per point. */\n  sx: Float32Array;\n  sy: Float32Array;\n  vx: Float32Array;\n  vy: Float32Array;\n  /** Per-line scratch for displaced positions. */\n  px: Float32Array;\n  py: Float32Array;\n  /** Swell-front wavelength (CSS px) and center projection for the static crest. */\n  lambda: number;\n  projC: number;\n  /** Per-instance seeded time offset — multiple instances desync deterministically. */\n  timeOffset: number;\n  noiseSeed: number;\n  springsIdle: boolean;\n  // Pointer state, CSS px / px-per-second.\n  pointerX: number;\n  pointerY: number;\n  prevX: number;\n  prevY: number;\n  velTX: number;\n  velTY: number;\n  velSX: number;\n  velSY: number;\n  ease: number;\n  easeTarget: number;\n  hasPointer: boolean;\n  lastMoveT: number;\n  lastElapsed: number;\n  /** Ghost cursor is currently driving the pointer state. */\n  ghostOn: boolean;\n  /** Ghost path clock (s) — advances only while the ghost drives. */\n  ghostT: number;\n  /** Ghost wall clock (s) that paces the swipe surges. */\n  ghostWall: number;\n}\n\n/**\n * Idle autoplay: when `want` (no real pointer for GHOST_DELAY_MS), a seeded\n * ghost drives the pointer state along a Lissajous figure-eight whose pace\n * surges every few seconds into a swipe, so the springs keep getting kicked.\n */\nfunction driveGhost(it: Internals, tn: Tuning, dt: number, want: boolean) {\n  if (!(want && tn.idle && tn.interactive)) {\n    if (it.ghostOn) {\n      it.ghostOn = false;\n      it.hasPointer = false;\n      it.easeTarget = 0;\n    }\n    return;\n  }\n  // Pace surges periodically: a lazy drift that every ~8s breaks into a swipe.\n  it.ghostWall += dt;\n  const surge = Math.pow(Math.max(Math.sin(it.ghostWall * 0.8 + it.timeOffset), 0), 4);\n  it.ghostT += dt * (0.6 + 3.4 * surge) * Math.max(tn.speed, 0.2);\n  const g = it.ghostT + it.timeOffset * 7;\n  const x = it.w * (0.5 + 0.4 * Math.sin(g * 0.61) + 0.06 * Math.sin(g * 1.7));\n  const y = it.h * (0.5 + 0.3 * Math.sin(g * 1.22 + 0.9));\n  if (!it.ghostOn) {\n    // Fade the ghost in from wherever its path is — never a streak from (0,0).\n    it.ghostOn = true;\n    it.prevX = it.pointerX = x;\n    it.prevY = it.pointerY = y;\n    it.velTX = it.velTY = 0;\n    it.hasPointer = true;\n    it.easeTarget = GHOST_PRESENCE;\n    return;\n  }\n  const inv = 1 / Math.max(dt, 1e-3);\n  it.velTX = it.velTX * 0.4 + (x - it.pointerX) * inv * 0.6;\n  it.velTY = it.velTY * 0.4 + (y - it.pointerY) * inv * 0.6;\n  it.pointerX = x;\n  it.pointerY = y;\n  it.hasPointer = true;\n  it.easeTarget = GHOST_PRESENCE;\n}\n\n/** Advance pointer easing + springs by dt seconds (frame-rate independent). */\nfunction physics(it: Internals, tn: Tuning, dt: number) {\n  // Eased 0→1 influence scalar on enter/leave; exponential time-constant blends.\n  it.ease += (it.easeTarget - it.ease) * (1 - Math.exp(-dt / EASE_TAU));\n  const velK = 1 - Math.exp(-dt / VEL_TAU);\n  it.velSX += (it.velTX - it.velSX) * velK;\n  it.velSY += (it.velTY - it.velSY) * velK;\n  // Target decays between move events so a parked cursor goes quiet.\n  const decay = Math.exp(-dt / VEL_DECAY_TAU);\n  it.velTX *= decay;\n  it.velTY *= decay;\n\n  const speedSm = Math.hypot(it.velSX, it.velSY);\n  const drive = tn.interactive ? it.ease * Math.min(speedSm, MAX_POINTER_SPEED) : 0;\n  const injecting = drive > 24 && it.hasPointer;\n\n  const { bx, by, sx, sy, vx, vy } = it;\n  const n = bx.length;\n\n  if (injecting) {\n    it.springsIdle = false;\n    // Impulse along the travel direction, ∝ smoothed cursor speed, over dt.\n    const invSpeed = 1 / (speedSm || 1);\n    let impX = it.velSX * invSpeed * drive * INJECT * dt;\n    let impY = it.velSY * invSpeed * drive * INJECT * dt;\n    if (it.horizontal) impY *= TRANSVERSE_BOOST;\n    else impX *= TRANSVERSE_BOOST;\n\n    // Kick every point near the pointer's travel *segment* this frame, so a\n    // fast swipe wakes the whole corridor it crossed — not just event samples.\n    const ax = it.prevX;\n    const ay = it.prevY;\n    const abx = it.pointerX - ax;\n    const aby = it.pointerY - ay;\n    const ab2 = abx * abx + aby * aby || 1;\n    const maxD2 = POINTER_RADIUS * POINTER_RADIUS * 2.9;\n    const sigma = POINTER_RADIUS * 0.55;\n    const invTwoSig2 = 1 / (2 * sigma * sigma);\n\n    for (let i = 0; i < n; i++) {\n      const pxp = bx[i] + sx[i];\n      const pyp = by[i] + sy[i];\n      let tSeg = ((pxp - ax) * abx + (pyp - ay) * aby) / ab2;\n      tSeg = tSeg < 0 ? 0 : tSeg > 1 ? 1 : tSeg;\n      const dx = pxp - (ax + abx * tSeg);\n      const dy = pyp - (ay + aby * tSeg);\n      const d2 = dx * dx + dy * dy;\n      if (d2 > maxD2) continue;\n      const wgt = Math.exp(-d2 * invTwoSig2);\n      vx[i] += impX * wgt;\n      vy[i] += impY * wgt;\n    }\n  }\n\n  if (!it.springsIdle) {\n    // Damped Hooke springs, semi-implicit Euler; friction is a per-frame\n    // retention at 60fps raised to dt·60 so the feel survives any frame rate.\n    const fr = Math.pow(Math.min(Math.max(tn.friction, 0), 0.999), dt * 60);\n    const k = Math.min(Math.max(tn.tension, 1), 600);\n    const cap = Math.max(tn.maxStretch, 1) * 2.5;\n    const cap2 = cap * cap;\n    let maxAbs = 0;\n    for (let i = 0; i < n; i++) {\n      let vxi = (vx[i] - sx[i] * k * dt) * fr;\n      let vyi = (vy[i] - sy[i] * k * dt) * fr;\n      let sxi = sx[i] + vxi * dt;\n      let syi = sy[i] + vyi * dt;\n      const r2 = sxi * sxi + syi * syi;\n      if (r2 > cap2) {\n        const s = cap / Math.sqrt(r2);\n        sxi *= s;\n        syi *= s;\n        vxi *= 0.7;\n        vyi *= 0.7;\n      }\n      vx[i] = vxi;\n      vy[i] = vyi;\n      sx[i] = sxi;\n      sy[i] = syi;\n      const a = Math.abs(sxi) + Math.abs(syi) + (Math.abs(vxi) + Math.abs(vyi)) * 0.05;\n      if (a > maxAbs) maxAbs = a;\n    }\n    // All quiet → zero the field and skip this loop entirely until re-kicked.\n    if (!injecting && maxAbs < 0.06) {\n      it.springsIdle = true;\n      sx.fill(0);\n      sy.fill(0);\n      vx.fill(0);\n      vy.fill(0);\n    }\n  }\n\n  it.prevX = it.pointerX;\n  it.prevY = it.pointerY;\n}\n\n/** One frame. `staticFrame` parks the swell crest mid-frame — visible curvature. */\nfunction draw(it: Internals, tn: Tuning, t: number, staticFrame: boolean) {\n  const { ctx, w, h, horizontal, lines, bx, by, sx, sy, px, py, lambda } = it;\n  ctx.fillStyle = `rgb(${BG[0]},${BG[1]},${BG[2]})`;\n  ctx.fillRect(0, 0, w, h);\n  ctx.lineJoin = \"round\";\n  ctx.lineCap = \"round\";\n\n  const baseT = t * Math.max(tn.speed, 0) + it.timeOffset;\n  const swellT = baseT / Math.max(tn.swellInterval, 0.5);\n  // Live: front travels with time. Static: phase chosen so the crest sits at\n  // the frame center — the frozen lattice always shows a full rolling swell.\n  const phase0 = staticFrame ? (TAU * it.projC) / lambda - Math.PI / 2 : TAU * swellT;\n  const springs = !staticFrame && !it.springsIdle;\n  const maxStretch = Math.max(tn.maxStretch, 1);\n  const invF = 1 / FEATURE_PX;\n  const amplitude = Math.max(tn.amplitude, 0);\n  const intensity = Math.max(tn.intensity, 0);\n  // Transverse-dominant orbits: lines sway across their own axis more than along it.\n  const axX = horizontal ? 0.6 : 1;\n  const axY = horizontal ? 1 : 0.6;\n\n  for (const line of lines) {\n    const alpha = Math.min(line.alphaBase * intensity, 1);\n    if (alpha < 0.01) continue;\n    const lt = staticFrame ? NOISE_T_STATIC + line.tPhase : baseT * line.tRate + line.tPhase;\n    const ampBase = amplitude * (0.55 + 0.45 * line.z);\n    const end = line.start + line.count;\n\n    let j = 0;\n    for (let i = line.start; i < end; i++, j++) {\n      const x = bx[i];\n      const y = by[i];\n      // Slightly diagonal front direction so the swell reads organic, not axial.\n      const proj = horizontal ? x + y * 0.28 : y + x * 0.28;\n      // Near rows lead the front by a small phase — depth parallax in time too.\n      const ph = (TAU * proj) / lambda - phase0 + line.z * 0.55;\n      const swell = Math.sin(ph);\n      // Noise sample used as an ANGLE through (cos, sin) — orbital, woven motion.\n      const angle =\n        fbm2(x * invF, y * invF, lt, it.noiseSeed) * TAU * ANGLE_TURNS + SWELL_ANGLE * swell;\n      const amp = ampBase * (1 + SWELL_AMP * swell);\n      let ox = Math.cos(angle) * amp * axX;\n      let oy = Math.sin(angle) * amp * axY;\n      if (springs) {\n        const sxi = sx[i];\n        const syi = sy[i];\n        const r = Math.hypot(sxi, syi);\n        if (r > 0.001) {\n          // tanh soft-clip: stretch saturates toward maxStretch, never hard-stops.\n          const soft = (maxStretch * Math.tanh(r / maxStretch)) / r;\n          ox += sxi * soft;\n          oy += syi * soft;\n        }\n      }\n      px[j] = x + ox;\n      py[j] = y + oy;\n    }\n\n    ctx.strokeStyle = `rgba(${line.rgb[0]},${line.rgb[1]},${line.rgb[2]},${alpha})`;\n    ctx.lineWidth = line.width;\n    ctx.beginPath();\n    ctx.moveTo(px[0], py[0]);\n    const cnt = line.count;\n    for (let m = 1; m < cnt - 1; m++) {\n      ctx.quadraticCurveTo(px[m], py[m], (px[m] + px[m + 1]) * 0.5, (py[m] + py[m + 1]) * 0.5);\n    }\n    ctx.lineTo(px[cnt - 1], py[cnt - 1]);\n    ctx.stroke();\n  }\n}\n\n/**\n * Swell — a sprung wave lattice. Rows of smooth lines ride an orbital noise\n * field (each point circles its anchor, so the mesh moves with woven, curtain-\n * like coherence) while a slow autonomous swell front rolls through every ~8s,\n * so the field breathes with no cursor. Swipe through it and points take\n * velocity impulses along your travel direction, proportional to cursor speed,\n * then spring home on damped Hooke springs — fast flicks send a wobble down\n * the lattice. Depth-graded rows (opacity, width, drift rate) give a parallax\n * curtain; deep-sea steel by default. Canvas 2D: seeded and SSR-safe, paused\n * offscreen and on hidden tabs, DPR-clamped with device-pixel-aligned strokes,\n * reduced-motion-safe (a static frame with the swell crest mid-roll). Left\n * untouched, a seeded ghost cursor takes over (`idle`). Drop it inside any\n * `relative` container.\n */\nexport function Swell({\n  orientation = \"horizontal\",\n  gap = { x: 28, y: 26 },\n  amplitude = 16,\n  speed = 1,\n  intensity = 1,\n  tension = DEFAULT_SPRING.tension,\n  friction = DEFAULT_SPRING.friction,\n  maxStretch = DEFAULT_SPRING.maxStretch,\n  swellInterval = 8,\n  colors = DEFAULT_COLORS,\n  interactive = true,\n  idle = true,\n  seed = 1,\n  dpr = 2,\n  paused = false,\n  className,\n  style,\n  ...props\n}: SwellProps) {\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const internalsRef = React.useRef<Internals | null>(null);\n  const reducedMotion = useReducedMotion();\n\n  const frozen = paused || reducedMotion;\n  const frozenRef = React.useRef(frozen);\n  frozenRef.current = frozen;\n  // performance.now() of the last real pointer move; -Infinity = never.\n  const lastRealRef = React.useRef(-Infinity);\n\n  // Tuning knobs mutate in place — no lattice rebuild on feel changes.\n  const tuningRef = React.useRef<Tuning>({\n    speed,\n    intensity,\n    amplitude,\n    tension,\n    friction,\n    maxStretch,\n    swellInterval,\n    interactive,\n    idle,\n  });\n  tuningRef.current = {\n    speed,\n    intensity,\n    amplitude,\n    tension,\n    friction,\n    maxStretch,\n    swellInterval,\n    interactive,\n    idle,\n  };\n\n  const gapKey = `${gap.x},${gap.y}`;\n  const colorsKey = colors.join(\",\");\n\n  const tick = React.useCallback((elapsed: number) => {\n    const it = internalsRef.current;\n    if (!it) return;\n    const tn = tuningRef.current;\n    const dt = Math.min(Math.max(elapsed - it.lastElapsed, 1 / 240), 1 / 30);\n    it.lastElapsed = elapsed;\n    driveGhost(it, tn, dt, performance.now() - lastRealRef.current > GHOST_DELAY_MS);\n    physics(it, tn, dt);\n    draw(it, tn, elapsed, false);\n  }, []);\n\n  const containerRef = useVisibilityPause<HTMLDivElement>(tick, { paused: frozen });\n\n  // Build the lattice; rebuild on size / orientation / spacing / palette / seed.\n  React.useEffect(() => {\n    const canvas = canvasRef.current;\n    const container = containerRef.current;\n    if (!canvas || !container) return;\n\n    const ctx = canvas.getContext(\"2d\", { alpha: false });\n    if (!ctx) return;\n\n    const dprScale = Math.min(\n      typeof window !== \"undefined\" ? window.devicePixelRatio || 1 : 1,\n      Math.min(Math.max(dpr, 1), 2)\n    );\n    const farRgb = hexToRgb(colors[0]);\n    const nearRgb = hexToRgb(colors[1]);\n    const horizontal = orientation !== \"vertical\";\n    const gapX = Math.min(Math.max(gap.x, 8), 200);\n    const gapY = Math.min(Math.max(gap.y, 8), 200);\n    /** Half-pixel alignment in DEVICE px so idle 1px strokes sit crisp on any DPR. */\n    const alignHalf = (v: number) => (Math.round(v * dprScale - 0.5) + 0.5) / dprScale;\n\n    const onContextLost = (e: Event) => e.preventDefault();\n    canvas.addEventListener(\"contextlost\", onContextLost);\n\n    const build = () => {\n      const w = Math.max(container.clientWidth, 1);\n      const h = Math.max(container.clientHeight, 1);\n      canvas.width = Math.round(w * dprScale);\n      canvas.height = Math.round(h * dprScale);\n      canvas.style.width = \"100%\";\n      canvas.style.height = \"100%\";\n      ctx.setTransform(dprScale, 0, 0, dprScale, 0, 0);\n\n      const rand = mulberry32((Math.round(seed || 1) * 0x9e3779b1) >>> 0 || 1);\n\n      // Row decimation on short stages: skip every other row below 480px height.\n      const dec = h < 480 ? 2 : 1;\n      const lineGap = horizontal ? gapY * dec : gapX;\n      let step = horizontal ? gapX : gapY * dec;\n      const span = horizontal ? h : w; // across the lines\n      const length = horizontal ? w : h; // along the lines\n      const lineCount = Math.max(2, Math.floor(span / lineGap));\n      let ptsPerLine = Math.max(4, Math.floor(length / step) + 3);\n      while (lineCount * ptsPerLine > MAX_POINTS) {\n        step *= 1.5;\n        ptsPerLine = Math.max(4, Math.floor(length / step) + 3);\n      }\n      const n = lineCount * ptsPerLine;\n\n      const bx = new Float32Array(n);\n      const by = new Float32Array(n);\n      const lines: Line[] = [];\n      for (let li = 0; li < lineCount; li++) {\n        const cross = alignHalf((li + 0.5) * lineGap);\n        // Horizontal rows: depth rises toward the bottom (sea toward the viewer).\n        // Vertical curtains: seeded random depth layers.\n        const z = horizontal\n          ? Math.min(1, Math.max(0, 0.78 * (cross / h) + 0.22 * rand()))\n          : rand();\n        const start = li * ptsPerLine;\n        for (let j = 0; j < ptsPerLine; j++) {\n          const along = (j - 1) * step; // one point of overhang each side\n          bx[start + j] = horizontal ? along : cross;\n          by[start + j] = horizontal ? cross : along;\n        }\n        const zz = Math.pow(z, 1.15);\n        lines.push({\n          start,\n          count: ptsPerLine,\n          z,\n          rgb: [\n            Math.round(farRgb[0] + (nearRgb[0] - farRgb[0]) * zz),\n            Math.round(farRgb[1] + (nearRgb[1] - farRgb[1]) * zz),\n            Math.round(farRgb[2] + (nearRgb[2] - farRgb[2]) * zz),\n          ],\n          alphaBase: 0.34 + 0.54 * zz,\n          width: 0.75 + 0.9 * z,\n          tRate: 0.55 + 0.6 * z,\n          tPhase: rand() * 61,\n        });\n      }\n      // Far rows first so near rows draw on top.\n      lines.sort((a, b) => a.z - b.z);\n\n      const it: Internals = {\n        ctx,\n        w,\n        h,\n        horizontal,\n        lines,\n        bx,\n        by,\n        sx: new Float32Array(n),\n        sy: new Float32Array(n),\n        vx: new Float32Array(n),\n        vy: new Float32Array(n),\n        px: new Float32Array(ptsPerLine),\n        py: new Float32Array(ptsPerLine),\n        lambda: Math.max(w, h) * 0.8,\n        projC: horizontal ? w / 2 + h * 0.14 : h / 2 + w * 0.14,\n        timeOffset: rand() * 100,\n        noiseSeed: (Math.round(seed || 1) * 0x51ab7) >>> 0 || 1,\n        springsIdle: true,\n        pointerX: 0,\n        pointerY: 0,\n        prevX: 0,\n        prevY: 0,\n        velTX: 0,\n        velTY: 0,\n        velSX: 0,\n        velSY: 0,\n        ease: 0,\n        easeTarget: 0,\n        hasPointer: false,\n        lastMoveT: 0,\n        lastElapsed: 0,\n        ghostOn: false,\n        ghostT: 0,\n        ghostWall: 0,\n      };\n      internalsRef.current = it;\n      // Never flash empty — paint one frame immediately.\n      draw(it, tuningRef.current, 0, frozenRef.current);\n    };\n\n    const onPointerMove = (e: PointerEvent) => {\n      const it = internalsRef.current;\n      if (!it || frozenRef.current || !tuningRef.current.interactive) return;\n      const rect = container.getBoundingClientRect();\n      const x = e.clientX - rect.left;\n      const y = e.clientY - rect.top;\n      const now = performance.now();\n      lastRealRef.current = now;\n      if (it.ghostOn) {\n        // Real pointer takes over instantly: drop the ghost's velocity so the\n        // jump from its position to the cursor never registers as a swipe.\n        it.ghostOn = false;\n        it.hasPointer = false;\n        it.velTX = it.velTY = 0;\n      }\n      if (it.hasPointer) {\n        const dtE = Math.min(Math.max((now - it.lastMoveT) / 1000, 0.004), 0.05);\n        // Blend event velocity into the target to tame event-timing jitter.\n        it.velTX = it.velTX * 0.4 + ((x - it.pointerX) / dtE) * 0.6;\n        it.velTY = it.velTY * 0.4 + ((y - it.pointerY) / dtE) * 0.6;\n      } else {\n        it.prevX = x;\n        it.prevY = y;\n      }\n      it.pointerX = x;\n      it.pointerY = y;\n      it.lastMoveT = now;\n      it.hasPointer = true;\n      it.easeTarget = 1;\n    };\n\n    const onPointerLeave = () => {\n      const it = internalsRef.current;\n      if (!it) return;\n      it.easeTarget = 0;\n      it.hasPointer = false;\n      // The ghost resumes after the usual delay rather than snapping in.\n      lastRealRef.current = performance.now();\n    };\n\n    container.addEventListener(\"pointermove\", onPointerMove);\n    container.addEventListener(\"pointerleave\", onPointerLeave);\n    container.addEventListener(\"pointercancel\", onPointerLeave);\n\n    const ro = new ResizeObserver(build);\n    ro.observe(container);\n    build();\n\n    return () => {\n      ro.disconnect();\n      container.removeEventListener(\"pointermove\", onPointerMove);\n      container.removeEventListener(\"pointerleave\", onPointerLeave);\n      container.removeEventListener(\"pointercancel\", onPointerLeave);\n      canvas.removeEventListener(\"contextlost\", onContextLost);\n      internalsRef.current = null;\n    };\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [orientation, gapKey, colorsKey, seed, dpr]);\n\n  // Render the designed static frame in place when paused / reduced-motion\n  // toggles on; springs are zeroed so unfreezing never jumps.\n  React.useEffect(() => {\n    const it = internalsRef.current;\n    if (!it || !frozen) return;\n    it.springsIdle = true;\n    it.sx.fill(0);\n    it.sy.fill(0);\n    it.vx.fill(0);\n    it.vy.fill(0);\n    it.ease = 0;\n    it.easeTarget = 0;\n    it.velTX = 0;\n    it.velTY = 0;\n    it.velSX = 0;\n    it.velSY = 0;\n    it.hasPointer = false;\n    it.ghostOn = false;\n    draw(it, tuningRef.current, 0, true);\n    // Tuning deps: knob changes while frozen must repaint the still frame.\n  }, [frozen, speed, intensity, amplitude, swellInterval]);\n\n  return (\n    <div\n      ref={containerRef}\n      aria-hidden\n      data-crucible=\"swell\"\n      className={cn(\"absolute inset-0 -z-10 overflow-hidden\", className)}\n      style={style}\n      {...props}\n    >\n      <canvas ref={canvasRef} className=\"block h-full w-full\" />\n      {/* Soft vignette so the lattice sinks into the frame edges. */}\n      <div\n        className=\"pointer-events-none absolute inset-0\"\n        style={{\n          background:\n            \"radial-gradient(120% 120% at 50% 42%, transparent 55%, rgba(0,0,0,0.5) 100%)\",\n        }}\n      />\n    </div>\n  );\n}\n"
    }
  ]
}