Ripple pond
Every click or tap on the canvas drops a circular ripple that fades with time — up to twelve waves sharing one surface. A raw WebGL1 shader of sine rings with exponential decay; the floating buttons below rain a drop, drop three at once and calm the surface.
Real scenarios
The component as it is used in a product — not a specimen grid.
A pond that answers the touch
Each press writes its position, birth time and strength into a twelve-slot ring-buffer uniform; the shader sums sine rings with exponential decay, so up to twelve waves share one surface before the oldest silently gives way. The dock floats below the waterline.
12-slot ripple ring buffer · exp decay ≈ 3s · half resolution · parked off screen · still water under reduced motion.
Important props
| Prop | Type | Default | What it does |
|---|---|---|---|
| ripples | uniform vec4[12] | 'ring buffer' | Up to twelve waves at once; each drop writes its position, birth time and strength into the next ring-buffer slot. |
| decay | exp | '≈ 3s' | Exponential decay: the amplitude dies out over about three seconds, and distance from the centre pays a spatial toll too. |
| ring | sine | '26 rad⁻¹ · 8 rad/s' | The rings are sine waves; the spatial frequency sets their density and the temporal one their outward speed. |
| pointer | event | 'pointerdown · Enter/Space' | A click or tap drops exactly where it lands; the keyboard drops near the centre. |
| resolution | scale | '0.5×' | The canvas renders at half CSS resolution and upscales — smooth rings, a happy GPU. |
Snippet
<div class="shrp" x-data="{ count: 0 }">
<div class="shrp-stage">
<canvas class="shrp-cv" tabindex="0" role="img" aria-label="Interactive pond; click or press Enter to drop"
@pointerdown="drop($event)" @keydown.enter.prevent="dropAt(0, 0)"></canvas>
<span class="shrp-cap">Drop a ripple — click, tap or Enter</span>
</div>
<div class="shrp-dock">
<button @click="rainOne()">Drop</button>
<button @click="rainThree()">Triple drop</button>
<button @click="calm()">Calm the surface</button>
<span role="status" x-text="count + ' live waves'"></span>
</div>
</div>
<style>
.shrp-stage { position: relative; overflow: clip; border-radius: 1.25rem; aspect-ratio: 16 / 8;
background: #08131f; }
.shrp-cv { position: absolute; inset: 0; inline-size: 100%; block-size: 100%; touch-action: manipulation; }
.shrp-cap { position: absolute; inset-block-start: .9rem; inset-inline-start: .9rem; z-index: 1;
padding: .35rem .8rem; border-radius: 999px; background: #ffffff24; color: #eaf6ff;
font-size: .76rem; backdrop-filter: blur(8px); }
.shrp-dock { display: flex; flex-wrap: wrap; justify-content: center; gap: .5rem; margin-block-start: -1.4rem;
position: relative; z-index: 2; }
.shrp-dock button { padding: .55rem 1.1rem; border-radius: 999px; border: 1px solid #ffffff33;
background: #101c2bd9; color: #eaf6ff; backdrop-filter: blur(10px); cursor: pointer; }
</style>
// app/Pages/Pond.jsx — Inertia (React) page; renders the React snippet
// below unchanged, with Inertia's Link wired into the provider.
import { Link } from '@inertiajs/react';
import { NabuXUIProvider } from '@nabuxai/ui-react';
import RipplePond from '@/components/RipplePond';
export default function Pond() {
return (
<NabuXUIProvider locale="en" linkComponent={Link}>
<main className="nx-page">
<RipplePond />
</main>
</NabuXUIProvider>
);
}
// RipplePond.jsx — click-dropped sine ripples with exponential decay, raw WebGL1
import { useEffect, useRef, useState } from 'react';
const MAX = 12;
const FRAG = `precision mediump float;
uniform vec2 u_res; uniform float u_time; uniform float u_amp;
uniform vec4 u_r[12];
void main() {
vec2 uv = (gl_FragCoord.xy * 2.0 - u_res) / min(u_res.x, u_res.y);
vec3 col = mix(vec3(0.031, 0.08, 0.145), vec3(0.11, 0.27, 0.38), uv.y * 0.5 + 0.5);
float ring = 0.0;
for (int i = 0; i < 12; i++) {
vec4 r = u_r[i];
float age = u_time - r.z;
if (age > 0.0 && age < 3.0) { // dead slots (t = -1000) never pass the guard
float d = length(uv - r.xy);
ring += sin(d * 26.0 - age * 8.0) * exp(-age * 1.7) * exp(-d * 1.25) * r.w;
}
}
col += vec3(0.30, 0.55, 0.68) * max(ring, 0.0) * u_amp; // crests catch the light
col -= vec3(0.04, 0.07, 0.09) * max(-ring, 0.0) * u_amp; // troughs deepen
float vig = 1.0 - 0.35 * dot(uv * 0.62, uv * 0.62);
gl_FragColor = vec4(col * vig, 1.0);
}`;
export default function RipplePond() {
const canvasRef = useRef(null);
const apiRef = useRef(null); // { drop, dropAt, calm } — set once GL is up
const [count, setCount] = useState(0);
const [noGl, setNoGl] = useState(false);
useEffect(() => {
const canvas = canvasRef.current;
const gl = canvas.getContext('webgl', { antialias: false, alpha: false, powerPreference: 'low-power' });
if (!gl) { setNoGl(true); return; }
const sh = (type, src) => {
const s = gl.createShader(type); gl.shaderSource(s, src); gl.compileShader(s); return s;
};
const prog = gl.createProgram();
gl.attachShader(prog, sh(gl.VERTEX_SHADER, 'attribute vec2 a; void main() { gl_Position = vec4(a, 0.0, 1.0); }'));
gl.attachShader(prog, sh(gl.FRAGMENT_SHADER, FRAG));
gl.linkProgram(prog); gl.useProgram(prog);
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);
const a = gl.getAttribLocation(prog, 'a');
gl.enableVertexAttribArray(a); gl.vertexAttribPointer(a, 2, gl.FLOAT, false, 0, 0);
const U = { res: gl.getUniformLocation(prog, 'u_res'), time: gl.getUniformLocation(prog, 'u_time'),
amp: gl.getUniformLocation(prog, 'u_amp'), r: gl.getUniformLocation(prog, 'u_r') };
const mq = matchMedia('(prefers-reduced-motion: reduce)');
const s = { raf: 0, visible: true, t0: performance.now(), last: 0 };
const buf = new Float32Array(MAX * 4).fill(-1000, 0).map((_, i) => (i % 4 === 2 ? -1000 : 0));
let head = 0, timers = [];
const clock = () => (performance.now() - s.t0) / 1000;
const size = () => { // half resolution keeps the rings cheap
canvas.width = Math.max(2, Math.round(canvas.clientWidth * 0.5));
canvas.height = Math.max(2, Math.round(canvas.clientHeight * 0.5));
};
const draw = (t) => {
let live = 0;
for (let k = 0; k < MAX; k++) if (t - buf[k * 4 + 2] < 3.0 && buf[k * 4 + 2] > 0) live++;
setCount(live);
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
gl.uniform2f(U.res, gl.drawingBufferWidth, gl.drawingBufferHeight);
gl.uniform1f(U.time, t);
gl.uniform1f(U.amp, mq.matches ? 0.0 : 1.0); // reduced motion → still water
gl.uniform4fv(U.r, buf);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
};
const redraw = () => draw(mq.matches ? 0 : s.last);
const run = () => {
cancelAnimationFrame(s.raf);
if (!s.visible) return; // parked while off screen
if (mq.matches) { redraw(); return; }
const loop = (now) => { s.last = (now - s.t0) / 1000; draw(s.last); s.raf = requestAnimationFrame(loop); };
s.raf = requestAnimationFrame(loop);
};
const spawn = (x, y, k = 1) => {
const i = head; head = (head + 1) % MAX;
buf[i * 4] = x; buf[i * 4 + 1] = y; buf[i * 4 + 2] = clock(); buf[i * 4 + 3] = k;
};
const drop = (e) => {
const r = canvas.getBoundingClientRect(), m = Math.min(r.width, r.height);
spawn((((e.clientX - r.left) * 2) - r.width) / m, ((r.height - (e.clientY - r.top) * 2)) / m);
};
const dropAt = (x, y) => spawn(x, y);
const rain = (n) => { // staggered drops so the waves visibly share the surface
for (let j = 0; j < n; j++) timers.push(setTimeout(() => spawn((Math.random() - 0.5) * 1.4, (Math.random() - 0.5) * 0.8), j * 180));
};
const calm = () => { timers.forEach(clearTimeout); timers = []; for (let k = 0; k < MAX; k++) buf[k * 4 + 2] = -1000; redraw(); };
apiRef.current = { drop, dropAt, rain, calm };
const io = new IntersectionObserver(([e]) => { s.visible = e.isIntersecting; run(); });
io.observe(canvas);
const ro = new ResizeObserver(() => { size(); redraw(); });
ro.observe(canvas);
const onDown = (e) => drop(e);
canvas.addEventListener('pointerdown', onDown);
size(); run();
return () => { // full cleanup — timers and context included
timers.forEach(clearTimeout);
cancelAnimationFrame(s.raf); io.disconnect(); ro.disconnect();
canvas.removeEventListener('pointerdown', onDown);
apiRef.current = null;
gl.getExtension('WEBGL_lose_context')?.loseContext();
};
}, []);
const api = () => apiRef.current;
return (
<>
<style>{`
.shrp-stage { position: relative; overflow: clip; border-radius: 1.25rem; aspect-ratio: 16 / 8; background: #08131f; }
.shrp-cv, .shrp-fallback { position: absolute; inset: 0; inline-size: 100%; block-size: 100%; touch-action: manipulation; }
.shrp-fallback { background: radial-gradient(120% 90% at 50% 10%, #1c4258, #08131f 70%); }
.shrp-cap { position: absolute; inset-block-start: .9rem; inset-inline-start: .9rem; z-index: 1; padding: .35rem .8rem;
border-radius: 999px; background: rgba(255,255,255,.14); color: #eaf6ff; font-size: .76rem;
backdrop-filter: blur(8px); }
.shrp-dock { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: .5rem;
margin-block-start: -1.4rem; position: relative; z-index: 2; }
.shrp-dock button { padding: .55rem 1.1rem; border-radius: 999px; border: 1px solid rgba(255,255,255,.2);
background: rgba(16,28,43,.85); color: #eaf6ff; backdrop-filter: blur(10px); cursor: pointer; }
`}</style>
<div className="shrp-stage">
{noGl
? <div className="shrp-fallback" />
: <canvas ref={canvasRef} className="shrp-cv" tabIndex={0} role="img"
aria-label="Interactive pond; click or press Enter to drop a ripple"
onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); api()?.dropAt(0, 0); } }} />}
<span className="shrp-cap">Drop a ripple — click, tap or Enter</span>
</div>
<div className="shrp-dock">
<button type="button" onClick={() => api()?.rain(1)}>Drop</button>
<button type="button" onClick={() => api()?.rain(3)}>Triple drop</button>
<button type="button" onClick={() => api()?.calm()}>Calm the surface</button>
<span role="status">{count} live waves</span>
</div>
</>
);
}
<!-- RipplePond.vue — click-dropped sine ripples with exponential decay, raw WebGL1 -->
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue';
const MAX = 12;
const FRAG = `precision mediump float;
uniform vec2 u_res; uniform float u_time; uniform float u_amp;
uniform vec4 u_r[12];
void main() {
vec2 uv = (gl_FragCoord.xy * 2.0 - u_res) / min(u_res.x, u_res.y);
vec3 col = mix(vec3(0.031, 0.08, 0.145), vec3(0.11, 0.27, 0.38), uv.y * 0.5 + 0.5);
float ring = 0.0;
for (int i = 0; i < 12; i++) {
vec4 r = u_r[i];
float age = u_time - r.z;
if (age > 0.0 && age < 3.0) { // dead slots (t = -1000) never pass the guard
float d = length(uv - r.xy);
ring += sin(d * 26.0 - age * 8.0) * exp(-age * 1.7) * exp(-d * 1.25) * r.w;
}
}
col += vec3(0.30, 0.55, 0.68) * max(ring, 0.0) * u_amp; // crests catch the light
col -= vec3(0.04, 0.07, 0.09) * max(-ring, 0.0) * u_amp; // troughs deepen
float vig = 1.0 - 0.35 * dot(uv * 0.62, uv * 0.62);
gl_FragColor = vec4(col * vig, 1.0);
}`;
const canvas = ref<HTMLCanvasElement | null>(null);
const count = ref(0);
const noGl = ref(false);
const api = ref<{ drop(e: PointerEvent): void; dropAt(x: number, y: number): void; rain(n: number): void; calm(): void } | null>(null);
let stop: (() => void) | null = null;
onMounted(() => {
const cv = canvas.value!;
const gl = cv.getContext('webgl', { antialias: false, alpha: false, powerPreference: 'low-power' });
if (!gl) { noGl.value = true; return; }
const sh = (type: number, src: string) => {
const s = gl.createShader(type)!; gl.shaderSource(s, src); gl.compileShader(s); return s;
};
const prog = gl.createProgram()!;
gl.attachShader(prog, sh(gl.VERTEX_SHADER, 'attribute vec2 a; void main() { gl_Position = vec4(a, 0.0, 1.0); }'));
gl.attachShader(prog, sh(gl.FRAGMENT_SHADER, FRAG));
gl.linkProgram(prog); gl.useProgram(prog);
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);
const a = gl.getAttribLocation(prog, 'a');
gl.enableVertexAttribArray(a); gl.vertexAttribPointer(a, 2, gl.FLOAT, false, 0, 0);
const U = { res: gl.getUniformLocation(prog, 'u_res')!, time: gl.getUniformLocation(prog, 'u_time')!,
amp: gl.getUniformLocation(prog, 'u_amp')!, r: gl.getUniformLocation(prog, 'u_r')! };
const mq = matchMedia('(prefers-reduced-motion: reduce)');
let raf = 0, visible = true, last = 0, head = 0;
const t0 = performance.now();
let timers: ReturnType<typeof setTimeout>[] = [];
const buf = new Float32Array(MAX * 4);
for (let k = 0; k < MAX; k++) buf[k * 4 + 2] = -1000;
const clock = () => (performance.now() - t0) / 1000;
const size = () => { // half resolution keeps the rings cheap
cv.width = Math.max(2, Math.round(cv.clientWidth * 0.5));
cv.height = Math.max(2, Math.round(cv.clientHeight * 0.5));
};
const draw = (t: number) => {
let live = 0;
for (let k = 0; k < MAX; k++) if (buf[k * 4 + 2] > 0 && t - buf[k * 4 + 2] < 3.0) live++;
count.value = live;
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
gl.uniform2f(U.res, gl.drawingBufferWidth, gl.drawingBufferHeight);
gl.uniform1f(U.time, t);
gl.uniform1f(U.amp, mq.matches ? 0.0 : 1.0); // reduced motion → still water
gl.uniform4fv(U.r, buf);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
};
const redraw = () => draw(mq.matches ? 0 : last);
const run = () => {
cancelAnimationFrame(raf);
if (!visible) return; // parked while off screen
if (mq.matches) { redraw(); return; }
const loop = (now: number) => { last = (now - t0) / 1000; draw(last); raf = requestAnimationFrame(loop); };
raf = requestAnimationFrame(loop);
};
const spawn = (x: number, y: number, k = 1) => {
const i = head; head = (head + 1) % MAX;
buf[i * 4] = x; buf[i * 4 + 1] = y; buf[i * 4 + 2] = clock(); buf[i * 4 + 3] = k;
};
const drop = (e: PointerEvent) => {
const r = cv.getBoundingClientRect(), m = Math.min(r.width, r.height);
spawn((((e.clientX - r.left) * 2) - r.width) / m, ((r.height - (e.clientY - r.top) * 2)) / m);
};
const rain = (n: number) => { // staggered drops so the waves visibly share the surface
for (let j = 0; j < n; j++) timers.push(setTimeout(() => spawn((Math.random() - 0.5) * 1.4, (Math.random() - 0.5) * 0.8), j * 180));
};
const calm = () => { timers.forEach(clearTimeout); timers = []; for (let k = 0; k < MAX; k++) buf[k * 4 + 2] = -1000; redraw(); };
api.value = { drop, dropAt: spawn, rain, calm };
const io = new IntersectionObserver(([e]) => { visible = e.isIntersecting; run(); });
io.observe(cv);
const ro = new ResizeObserver(() => { size(); redraw(); });
ro.observe(cv);
const onDown = (e: PointerEvent) => drop(e);
cv.addEventListener('pointerdown', onDown);
size(); run();
stop = () => { // full cleanup — timers and context included
timers.forEach(clearTimeout);
cancelAnimationFrame(raf); io.disconnect(); ro.disconnect();
cv.removeEventListener('pointerdown', onDown);
gl.getExtension('WEBGL_lose_context')?.loseContext();
};
});
onBeforeUnmount(() => stop?.());
const onKey = (e: KeyboardEvent) => {
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); api.value?.dropAt(0, 0); }
};
</script>
<template>
<div class="shrp-stage">
<div v-if="noGl" class="shrp-fallback"></div>
<canvas v-else ref="canvas" class="shrp-cv" tabindex="0" role="img"
aria-label="Interactive pond; click or press Enter to drop a ripple"
@keydown="onKey"></canvas>
<span class="shrp-cap">Drop a ripple — click, tap or Enter</span>
</div>
<div class="shrp-dock">
<button type="button" @click="api?.rain(1)">Drop</button>
<button type="button" @click="api?.rain(3)">Triple drop</button>
<button type="button" @click="api?.calm()">Calm the surface</button>
<span role="status">{{ count }} live waves</span>
</div>
</template>
<style scoped>
.shrp-stage { position: relative; overflow: clip; border-radius: 1.25rem; aspect-ratio: 16 / 8; background: #08131f; }
.shrp-cv, .shrp-fallback { position: absolute; inset: 0; inline-size: 100%; block-size: 100%; touch-action: manipulation; }
.shrp-fallback { background: radial-gradient(120% 90% at 50% 10%, #1c4258, #08131f 70%); }
.shrp-cap { position: absolute; inset-block-start: .9rem; inset-inline-start: .9rem; z-index: 1; padding: .35rem .8rem;
border-radius: 999px; background: rgba(255,255,255,.14); color: #eaf6ff; font-size: .76rem;
backdrop-filter: blur(8px); }
.shrp-dock { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: .5rem;
margin-block-start: -1.4rem; position: relative; z-index: 2; }
.shrp-dock button { padding: .55rem 1.1rem; border-radius: 999px; border: 1px solid rgba(255,255,255,.2);
background: rgba(16,28,43,.85); color: #eaf6ff; backdrop-filter: blur(10px); cursor: pointer; }
</style>
<!-- RipplePond.svelte — click-dropped sine ripples with exponential decay, raw WebGL1 -->
<script lang="ts">
import { onMount } from 'svelte';
const MAX = 12;
const FRAG = `precision mediump float;
uniform vec2 u_res; uniform float u_time; uniform float u_amp;
uniform vec4 u_r[12];
void main() {
vec2 uv = (gl_FragCoord.xy * 2.0 - u_res) / min(u_res.x, u_res.y);
vec3 col = mix(vec3(0.031, 0.08, 0.145), vec3(0.11, 0.27, 0.38), uv.y * 0.5 + 0.5);
float ring = 0.0;
for (int i = 0; i < 12; i++) {
vec4 r = u_r[i];
float age = u_time - r.z;
if (age > 0.0 && age < 3.0) { // dead slots (t = -1000) never pass the guard
float d = length(uv - r.xy);
ring += sin(d * 26.0 - age * 8.0) * exp(-age * 1.7) * exp(-d * 1.25) * r.w;
}
}
col += vec3(0.30, 0.55, 0.68) * max(ring, 0.0) * u_amp; // crests catch the light
col -= vec3(0.04, 0.07, 0.09) * max(-ring, 0.0) * u_amp; // troughs deepen
float vig = 1.0 - 0.35 * dot(uv * 0.62, uv * 0.62);
gl_FragColor = vec4(col * vig, 1.0);
}`;
let canvas: HTMLCanvasElement;
let count = $state(0);
let noGl = $state(false);
let drop: (e: PointerEvent) => void = () => {};
let dropAt: (x: number, y: number) => void = () => {};
let rain: (n: number) => void = () => {};
let calm: () => void = () => {};
function onKey(e: KeyboardEvent) {
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); dropAt(0, 0); }
}
onMount(() => {
const gl = canvas.getContext('webgl', { antialias: false, alpha: false, powerPreference: 'low-power' });
if (!gl) { noGl = true; return; }
const sh = (type: number, src: string) => {
const s = gl.createShader(type)!; gl.shaderSource(s, src); gl.compileShader(s); return s;
};
const prog = gl.createProgram()!;
gl.attachShader(prog, sh(gl.VERTEX_SHADER, 'attribute vec2 a; void main() { gl_Position = vec4(a, 0.0, 1.0); }'));
gl.attachShader(prog, sh(gl.FRAGMENT_SHADER, FRAG));
gl.linkProgram(prog); gl.useProgram(prog);
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);
const a = gl.getAttribLocation(prog, 'a');
gl.enableVertexAttribArray(a); gl.vertexAttribPointer(a, 2, gl.FLOAT, false, 0, 0);
const U = { res: gl.getUniformLocation(prog, 'u_res')!, time: gl.getUniformLocation(prog, 'u_time')!,
amp: gl.getUniformLocation(prog, 'u_amp')!, r: gl.getUniformLocation(prog, 'u_r')! };
const mq = matchMedia('(prefers-reduced-motion: reduce)');
let raf = 0, visible = true, last = 0, head = 0;
const t0 = performance.now();
let timers: ReturnType<typeof setTimeout>[] = [];
const buf = new Float32Array(MAX * 4);
for (let k = 0; k < MAX; k++) buf[k * 4 + 2] = -1000;
const clock = () => (performance.now() - t0) / 1000;
const size = () => { // half resolution keeps the rings cheap
canvas.width = Math.max(2, Math.round(canvas.clientWidth * 0.5));
canvas.height = Math.max(2, Math.round(canvas.clientHeight * 0.5));
};
const draw = (t: number) => {
let live = 0;
for (let k = 0; k < MAX; k++) if (buf[k * 4 + 2] > 0 && t - buf[k * 4 + 2] < 3.0) live++;
count = live;
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
gl.uniform2f(U.res, gl.drawingBufferWidth, gl.drawingBufferHeight);
gl.uniform1f(U.time, t);
gl.uniform1f(U.amp, mq.matches ? 0.0 : 1.0); // reduced motion → still water
gl.uniform4fv(U.r, buf);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
};
const redraw = () => draw(mq.matches ? 0 : last);
const run = () => {
cancelAnimationFrame(raf);
if (!visible) return; // parked while off screen
if (mq.matches) { redraw(); return; }
const loop = (now: number) => { last = (now - t0) / 1000; draw(last); raf = requestAnimationFrame(loop); };
raf = requestAnimationFrame(loop);
};
const spawn = (x: number, y: number, k = 1) => {
const i = head; head = (head + 1) % MAX;
buf[i * 4] = x; buf[i * 4 + 1] = y; buf[i * 4 + 2] = clock(); buf[i * 4 + 3] = k;
};
drop = (e: PointerEvent) => {
const r = canvas.getBoundingClientRect(), m = Math.min(r.width, r.height);
spawn((((e.clientX - r.left) * 2) - r.width) / m, ((r.height - (e.clientY - r.top) * 2)) / m);
};
dropAt = spawn;
rain = (n: number) => { // staggered drops so the waves visibly share the surface
for (let j = 0; j < n; j++) timers.push(setTimeout(() => spawn((Math.random() - 0.5) * 1.4, (Math.random() - 0.5) * 0.8), j * 180));
};
calm = () => { timers.forEach(clearTimeout); timers = []; for (let k = 0; k < MAX; k++) buf[k * 4 + 2] = -1000; redraw(); };
const io = new IntersectionObserver(([e]) => { visible = e.isIntersecting; run(); });
io.observe(canvas);
const ro = new ResizeObserver(() => { size(); redraw(); });
ro.observe(canvas);
const onDown = (e: PointerEvent) => drop(e);
canvas.addEventListener('pointerdown', onDown);
size(); run();
return () => { // full cleanup — timers and context included
timers.forEach(clearTimeout);
cancelAnimationFrame(raf); io.disconnect(); ro.disconnect();
canvas.removeEventListener('pointerdown', onDown);
gl.getExtension('WEBGL_lose_context')?.loseContext();
};
});
</script>
<div class="shrp-stage">
{#if noGl}
<div class="shrp-fallback"></div>
{:else}
<canvas bind:this={canvas} class="shrp-cv" tabindex="0" role="img"
aria-label="Interactive pond; click or press Enter to drop a ripple"
onkeydown={onKey}></canvas>
{/if}
<span class="shrp-cap">Drop a ripple — click, tap or Enter</span>
</div>
<div class="shrp-dock">
<button type="button" onclick={() => rain(1)}>Drop</button>
<button type="button" onclick={() => rain(3)}>Triple drop</button>
<button type="button" onclick={() => calm()}>Calm the surface</button>
<span role="status">{count} live waves</span>
</div>
<style>
.shrp-stage { position: relative; overflow: clip; border-radius: 1.25rem; aspect-ratio: 16 / 8; background: #08131f; }
.shrp-cv, .shrp-fallback { position: absolute; inset: 0; inline-size: 100%; block-size: 100%; touch-action: manipulation; }
.shrp-fallback { background: radial-gradient(120% 90% at 50% 10%, #1c4258, #08131f 70%); }
.shrp-cap { position: absolute; inset-block-start: .9rem; inset-inline-start: .9rem; z-index: 1; padding: .35rem .8rem;
border-radius: 999px; background: rgba(255,255,255,.14); color: #eaf6ff; font-size: .76rem;
backdrop-filter: blur(8px); }
.shrp-dock { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: .5rem;
margin-block-start: -1.4rem; position: relative; z-index: 2; }
.shrp-dock button { padding: .55rem 1.1rem; border-radius: 999px; border: 1px solid rgba(255,255,255,.2);
background: rgba(16,28,43,.85); color: #eaf6ff; backdrop-filter: blur(10px); cursor: pointer; }
</style>