{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"pool-water-ripple-background-bending-tile-lines-as-drops-land","type":"registry:block","title":"Pool Water Ripples","description":"","dependencies":[],"files":[{"path":"components/we-inc/pool-water-ripple-background-bending-tile-lines-as-drops-land.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// The values worth changing.\nconst SETTINGS = {\n  tile: 46, // tile size in pixels\n  water: [\"#b9e4e2\", \"#7fc6cc\"], // shallow and deep water over the tiles\n  grout: \"rgba(255, 255, 255, 0.75)\", // the lines between tiles\n  drops: 1.4, // drops falling each second; 0 leaves only the ones you tap\n  bend: 16, // how strongly the ripples bend the tile lines\n  calm: 0.986, // how long rings last; closer to 1 rings travel further\n};\n\nconst SCENE: any = {\n  start: 0,\n  still: 0,\n  init(ctx: any, w: any, h: any) {\n    const cell = 8;\n    const gw = Math.ceil(w / cell) + 2;\n    const gh = Math.ceil(h / cell) + 2;\n    const off = document.createElement(\"canvas\");\n    off.width = gw;\n    off.height = gh;\n    const octx = off.getContext(\"2d\");\n    const st: any = { cell, gw, gh, a: new Float32Array(gw * gh), b: new Float32Array(gw * gh), off, octx, img: octx.createImageData(gw, gh), clock: 0, taps: 0 };\n    // A few drops have already landed when the block first appears.\n    for (let i = 0; i < 5; i++) {\n      SCENE.drop(st, Math.random() * w, Math.random() * h, 1);\n      for (let k = 0; k < 22; k++) SCENE.step(st);\n    }\n    return st;\n  },\n  drop(st: any, x: any, y: any, power: any) {\n    const cx = Math.round(x / st.cell) + 1;\n    const cy = Math.round(y / st.cell) + 1;\n    for (let j = -2; j <= 2; j++)\n      for (let i = -2; i <= 2; i++) {\n        const xi = cx + i;\n        const yj = cy + j;\n        if (xi < 1 || yj < 1 || xi >= st.gw - 1 || yj >= st.gh - 1) continue;\n        const d = Math.sqrt(i * i + j * j);\n        st.a[yj * st.gw + xi] -= Math.max(0, 1 - d / 2.6) * 14 * power;\n      }\n  },\n  step(st: any) {\n    // The classic two-buffer wave: each cell moves toward the average of its neighbours, then loses a little energy.\n    const { gw, gh, a, b } = st;\n    for (let y = 1; y < gh - 1; y++) {\n      for (let x = 1; x < gw - 1; x++) {\n        const i = y * gw + x;\n        b[i] = ((a[i - 1] + a[i + 1] + a[i - gw] + a[i + gw]) / 2 - b[i]) * SETTINGS.calm;\n      }\n    }\n    st.a = b;\n    st.b = a;\n  },\n  draw(ctx: any, w: any, h: any, t: any, st: any, ptr: any, dt: any) {\n    if (dt > 0) {\n      st.clock += dt * SETTINGS.drops;\n      while (st.clock > 1) {\n        st.clock -= Math.random() * 1.5;\n        SCENE.drop(st, Math.random() * w, Math.random() * h, 0.6 + Math.random() * 0.6);\n      }\n      if (ptr.down !== st.taps) {\n        st.taps = ptr.down;\n        SCENE.drop(st, ptr.x, ptr.y, 1.4);\n      }\n      if (ptr.on && Math.random() < 0.25) SCENE.drop(st, ptr.x, ptr.y, 0.25);\n      SCENE.step(st);\n      SCENE.step(st);\n    }\n    const { gw, gh, cell } = st;\n    const hgt = st.a;\n    const water = ctx.createLinearGradient(0, 0, w, h);\n    water.addColorStop(0, SETTINGS.water[0]);\n    water.addColorStop(1, SETTINGS.water[1]);\n    ctx.fillStyle = water;\n    ctx.fillRect(0, 0, w, h);\n    const slope = (x: any, y: any) => {\n      const xi = Math.min(gw - 2, Math.max(1, Math.round(x / cell) + 1));\n      const yi = Math.min(gh - 2, Math.max(1, Math.round(y / cell) + 1));\n      const i = yi * gw + xi;\n      return [hgt[i + 1] - hgt[i - 1], hgt[i + gw] - hgt[i - gw]];\n    };\n    // Grout lines, each point pushed sideways by the slope of the water above it.\n    const k = SETTINGS.bend / 10;\n    ctx.strokeStyle = SETTINGS.grout;\n    ctx.lineWidth = 2;\n    ctx.beginPath();\n    const s = SETTINGS.tile;\n    for (let x = (w % s) / 2; x <= w; x += s) {\n      for (let y = -6; y <= h + 6; y += 6) {\n        const g = slope(x, y);\n        if (y === -6) ctx.moveTo(x + g[0] * k, y + g[1] * k);\n        else ctx.lineTo(x + g[0] * k, y + g[1] * k);\n      }\n    }\n    for (let y = (h % s) / 2; y <= h; y += s) {\n      for (let x = -6; x <= w + 6; x += 6) {\n        const g = slope(x, y);\n        if (x === -6) ctx.moveTo(x + g[0] * k, y + g[1] * k);\n        else ctx.lineTo(x + g[0] * k, y + g[1] * k);\n      }\n    }\n    ctx.stroke();\n    // Light and shade on each ring: slopes facing the sun brighten, the far side darkens.\n    const data = st.img.data;\n    for (let y = 0; y < gh; y++) {\n      for (let x = 0; x < gw; x++) {\n        const i = y * gw + x;\n        const l = x > 0 && y > 0 && x < gw - 1 && y < gh - 1 ? hgt[i - 1] - hgt[i + 1] + hgt[i - gw] - hgt[i + gw] : 0;\n        const o = i * 4;\n        if (l > 0) {\n          data[o] = 255;\n          data[o + 1] = 255;\n          data[o + 2] = 250;\n          data[o + 3] = Math.min(150, l * 90);\n        } else {\n          data[o] = 20;\n          data[o + 1] = 92;\n          data[o + 2] = 110;\n          data[o + 3] = Math.min(90, -l * 60);\n        }\n      }\n    }\n    st.octx.putImageData(st.img, 0, 0);\n    ctx.imageSmoothingEnabled = true;\n    ctx.drawImage(st.off, -cell, -cell, gw * cell, gh * cell);\n  },\n};\n\n// Runs a scene on a canvas: sizes it to the block, pauses off screen and in hidden tabs, cleans up after itself.\nfunction runScene(root: any, canvas: any, scene: any, reduced: boolean) {\n  const ctx = canvas.getContext(\"2d\");\n  if (!ctx) return () => {};\n  let w = 0;\n  let h = 0;\n  let st: any = null;\n  let raf = 0;\n  let last = 0;\n  let acc = 0;\n  let onScreen = true;\n  let time = reduced ? (scene.still ?? scene.start ?? 0) : (scene.start ?? 0);\n  const ptr: any = { x: 0, y: 0, on: false, down: 0 };\n  const step = 1 / (scene.fps || 60);\n  const resize = () => {\n    const r = root.getBoundingClientRect();\n    const nw = Math.max(1, Math.round(r.width));\n    const nh = Math.max(1, Math.round(r.height));\n    if (nw === w && nh === h && st) return;\n    w = nw;\n    h = nh;\n    // Device pixels are capped at 1.5 and scaled by the scene's own quality factor.\n    const d = Math.min(window.devicePixelRatio || 1, 1.5) * (scene.scale || 1);\n    canvas.width = Math.max(1, Math.round(w * d));\n    canvas.height = Math.max(1, Math.round(h * d));\n    ctx.setTransform(canvas.width / w, 0, 0, canvas.height / h, 0, 0);\n    st = scene.init(ctx, w, h, canvas.width / w);\n    scene.draw(ctx, w, h, time, st, ptr, 0);\n  };\n  const tick = (now: number) => {\n    raf = requestAnimationFrame(tick);\n    const dt = last ? Math.min(0.1, (now - last) / 1000) : step;\n    last = now;\n    acc += dt;\n    if (acc < step * 0.95) return;\n    const used = Math.min(acc, 0.1);\n    acc = 0;\n    time += used;\n    scene.draw(ctx, w, h, time, st, ptr, used);\n  };\n  const play = () => {\n    if (raf || reduced || !onScreen || document.hidden) return;\n    last = 0;\n    raf = requestAnimationFrame(tick);\n  };\n  const stop = () => {\n    cancelAnimationFrame(raf);\n    raf = 0;\n  };\n  const ro = new ResizeObserver(resize);\n  ro.observe(root);\n  const io = new IntersectionObserver((entries: any) => {\n    onScreen = entries[0].isIntersecting;\n    if (onScreen) play();\n    else stop();\n  });\n  io.observe(root);\n  const onVis = () => (document.hidden ? stop() : play());\n  const onMove = (e: any) => {\n    const r = root.getBoundingClientRect();\n    ptr.x = e.clientX - r.left;\n    ptr.y = e.clientY - r.top;\n    ptr.on = true;\n  };\n  const onLeave = () => {\n    ptr.on = false;\n  };\n  const onDown = (e: any) => {\n    onMove(e);\n    ptr.down += 1;\n  };\n  document.addEventListener(\"visibilitychange\", onVis);\n  root.addEventListener(\"pointermove\", onMove);\n  root.addEventListener(\"pointerleave\", onLeave);\n  root.addEventListener(\"pointerdown\", onDown);\n  resize();\n  play();\n  return () => {\n    stop();\n    ro.disconnect();\n    io.disconnect();\n    document.removeEventListener(\"visibilitychange\", onVis);\n    root.removeEventListener(\"pointermove\", onMove);\n    root.removeEventListener(\"pointerleave\", onLeave);\n    root.removeEventListener(\"pointerdown\", onDown);\n  };\n}\n\nexport default function PoolWaterRippleBackground() {\n  const rootRef = useRef<HTMLElement>(null);\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  useEffect(() => {\n    const root = rootRef.current;\n    const canvas = canvasRef.current;\n    if (!root || !canvas) return;\n    // Visitors who prefer reduced motion get one settled frame and no loop.\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    return runScene(root, canvas, SCENE, reduced);\n  }, []);\n\n  return (\n    <section ref={rootRef} className=\"relative h-[620px] w-full overflow-hidden bg-[#b9e4e2] text-[#0f2f36]\">\n      <canvas ref={canvasRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 block h-full w-full\" />\n      <div className=\"relative z-10 mx-auto flex h-full max-w-6xl flex-col justify-center items-start text-left px-6 py-14 sm:px-10\">\n        <div className=\"max-w-xl rounded-2xl border p-7 sm:p-10 bg-[#f7fbfa] border-[rgba(15,47,54,0.12)]\">\n          <p className=\"text-xs font-semibold uppercase tracking-[0.2em] text-[#0f4c5c]\">Saltwater Lido</p>\n          <h2 className=\"mt-4 text-4xl font-semibold leading-[1.05] tracking-tight sm:text-5xl lg:text-6xl\">Heated outdoor pool, open from six.</h2>\n          <p className=\"mt-5 text-base leading-relaxed text-[#2c4b52] sm:text-lg\">Lane swimming in the morning, family sessions after school and a sauna by the deep end.</p>\n          <a href=\"#\" className=\"mt-8 inline-flex h-12 items-center rounded-[10px] bg-[#0f4c5c] px-6 text-sm font-semibold text-[#f2fbfa] transition-opacity hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#0f4c5c] focus-visible:ring-offset-2 focus-visible:ring-offset-[#b9e4e2]\">Book a lane</a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
