Water Ripple
水面涟漪
Concentric ripples travel outward from the contact point and bend the image before settling.
Where to use it
Let visitors disturb a landscape hero, map, or product image. Keep displacement strongest near the contact point and allow it to decay before the next wave.
Practical example
An aquarium site lets visitors tap its lagoon photograph. Concentric waves bend the reflections outward and then settle.
Also called
- Image Ripple
- Ripple Distortion
AI prompt
Add Water Ripple to [image]. On pointer down or hover, concentric ripples distort the image from the contact point and settle. Use lightweight displacement. For reduced motion, replace the distortion with a brief opacity pulse.
Example code
Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.
View HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Water Ripple</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.demo {
background: radial-gradient(ellipse at 49% 18%, #274343, #172927 68%, #111d1c);
}
.ripple-picture {
width: min(100%, 510px); aspect-ratio: 360/196; max-height: calc(100vh - 28px);
overflow: hidden; padding: 0; border: 1px solid #b1926a; border-radius: 8px;
background: #355753; box-shadow: 0 8px 24px #111b1bc9;
cursor: crosshair; touch-action: manipulation;
}
.ripple-picture canvas { display: block; width: 100%; height: 100%; }
</style>
</head>
<body>
<main class="demo">
<button class="ripple-picture" type="button" aria-label="Create a ripple on the lake">
<canvas width="360" height="196" aria-hidden="true"></canvas>
</button>
</main>
<script>
const picture = document.querySelector('.ripple-picture');
const canvas = picture.querySelector('canvas');
const context = canvas.getContext('2d', { willReadFrequently: true });
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
const { width, height } = canvas;
const horizon = 94;
const duration = 980;
const waves = [];
let frame = 0;
let pulse;
if (context) {
paintLake();
const original = context.getImageData(0, 0, width, height);
const distorted = context.createImageData(width, height);
const pixels = new Uint32Array(original.data.buffer);
const result = new Uint32Array(distorted.data.buffer);
function paintLake() {
function fillGradient(y1, y2, stops) {
const gradient = context.createLinearGradient(0, y1, 0, y2);
stops.forEach(([offset, color]) => gradient.addColorStop(offset, color));
return gradient;
}
function ridge(heights, color) {
context.beginPath();
context.moveTo(0, horizon);
heights.forEach((y, index) => context.lineTo(index * width / (heights.length - 1), y));
context.lineTo(width, horizon);
context.closePath();
context.fillStyle = color;
context.fill();
}
function trees(points, color) {
context.fillStyle = color;
for (const [x, y, size] of points) {
context.beginPath();
context.moveTo(x, y - size);
context.lineTo(x + size * .23, y - size * .48);
context.lineTo(x + size * .14, y - size * .48);
context.lineTo(x + size * .32, y);
context.lineTo(x - size * .32, y);
context.lineTo(x - size * .14, y - size * .48);
context.lineTo(x - size * .23, y - size * .48);
context.closePath();
context.fill();
}
}
context.fillStyle = fillGradient(0, horizon, [
[0, '#44394f'], [.57, '#b76f6b'], [1, '#f2ba88'],
]);
context.fillRect(0, 0, width, horizon);
const sunlight = context.createRadialGradient(250, 58, 4, 250, 58, 46);
sunlight.addColorStop(0, '#ffe2abb2');
sunlight.addColorStop(1, '#ffc99000');
context.fillStyle = sunlight;
context.fillRect(197, 12, 102, 82);
context.fillStyle = '#ffe6b1';
context.beginPath();
context.arc(250, 58, 14, 0, 2 * Math.PI);
context.fill();
context.strokeStyle = '#f5b38a68';
for (const [x, y, length] of [[38,36,80],[61,45,45],[216,28,71],[235,65,71],[264,69,56]]) {
context.beginPath();
context.moveTo(x, y);
context.lineTo(x + length, y);
context.stroke();
}
ridge([53,47,55,48,43,25,48,59,72,64,55,66,60,72,66], '#83616e');
ridge([58,57,64,47,57,59,69,74,81,76,72,74,66,61,56], '#625964');
ridge([42,43,50,65,76,83,88,93,93,93,91,87,77,70,60], '#374c4b');
ridge([54,56,70,86,90,94,94,94,94,94,94,91,87,78,71], '#283f3e');
const silhouettes = [];
let seed = 24;
function random() {
seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
return seed / 4294967296;
}
for (let x = 1; x < 114; x += 3 + random() * 5) {
silhouettes.push([x, 53 + 44 * Math.sin(x / 80), 7 + random() * (14 - x / 11)]);
}
for (let x = 285; x < width + 5; x += 3 + random() * 5) {
silhouettes.push([x, 92 - (x - 285) * .33, 5 + random() * (x - 270) / 3]);
}
trees(silhouettes, '#283f3e');
const reflection = document.createElement('canvas');
reflection.width = width;
reflection.height = horizon;
reflection.getContext('2d').drawImage(canvas, 0, 0);
context.fillStyle = fillGradient(horizon, height, [
[0, '#756f68'], [.35, '#42645d'], [1, '#172e2f'],
]);
context.fillRect(0, horizon, width, height - horizon);
context.globalAlpha = .46;
for (let y = 0; y < horizon; y += 2) {
context.drawImage(reflection, 0, horizon - y - 2, width, 2, Math.sin(y / 4) * y / 5, horizon + y, width, 2);
}
context.globalAlpha = 1;
for (let index = 0; index < 165; index += 1) {
const y = horizon + 1 + random() * (height - horizon);
const x = random() * width;
const distance = (y - horizon) / (height - horizon);
context.fillStyle = index % 3 === 0 ? '#fad6a93f' : '#10383346';
context.fillRect(x, y, 3 + random() * 42 * distance, .6 + distance * .7);
}
}
function draw(time) {
frame = 0;
while (waves.length && time - waves[0].started >= duration) waves.shift();
result.set(pixels);
for (const wave of waves) {
const progress = Math.max(0, (time - wave.started) / duration);
const radius = progress * 187;
const thickness = 36;
const feather = Math.min(progress * 14, 1) * (1 - progress);
const limit = radius + thickness;
const left = Math.max(0, Math.floor(wave.x - limit));
const right = Math.min(width - 1, Math.ceil(wave.x + limit));
const top = Math.max(0, Math.floor(wave.y - limit));
const bottom = Math.min(height - 1, Math.ceil(wave.y + limit));
for (let y = top; y <= bottom; y += 1) {
const dy = y - wave.y;
for (let x = left; x <= right; x += 1) {
const dx = x - wave.x;
const distance = Math.sqrt(dx * dx + dy * dy);
const edge = distance - radius;
if (distance < 1 || Math.abs(edge) >= thickness) continue;
const envelope = 1 - Math.abs(edge) / thickness;
const shift = Math.sin(edge * .37) * envelope * feather * 11 / distance;
const sourceX = Math.round(Math.min(width - 1, Math.max(0, x + dx * shift)));
const sourceY = Math.round(Math.min(height - 1, Math.max(0, y + dy * shift)));
result[y * width + x] = pixels[sourceY * width + sourceX];
}
}
}
context.putImageData(distorted, 0, 0);
if (waves.length) frame = requestAnimationFrame(draw);
}
function disturb(x, y) {
if (reduced.matches) {
pulse?.cancel();
pulse = canvas.animate([{ opacity: 1 }, { opacity: .72 }, { opacity: 1 }], { duration: 200 });
return;
}
if (waves.length === 3) waves.shift();
waves.push({ x, y, started: performance.now() });
if (!frame) frame = requestAnimationFrame(draw);
}
function locate(event) {
const bounds = canvas.getBoundingClientRect();
return [
(event.clientX - bounds.left) * width / bounds.width,
(event.clientY - bounds.top) * height / bounds.height,
];
}
let lastHover = -Infinity;
picture.addEventListener('pointerdown', (event) => {
if (event.button === 0) disturb(...locate(event));
});
picture.addEventListener('pointermove', (event) => {
if (event.pointerType !== 'mouse' || event.buttons || reduced.matches) return;
const now = performance.now();
if (now - lastHover < 270) return;
lastHover = now;
disturb(...locate(event));
});
picture.addEventListener('click', (event) => {
if (event.detail === 0) disturb(width / 2, height / 2);
});
reduced.addEventListener('change', () => {
cancelAnimationFrame(frame);
frame = 0;
waves.length = 0;
context.putImageData(original, 0, 0);
});
}
</script>
</body>
</html>Original example by UI Skills