水面涟漪
Water Ripple
同心波纹从接触点向外扩散,短暂扭曲图像后逐渐平息。
适用场景
在风景首屏、地图或产品图片上加一个可交互的水波效果。 把最强位移放在接触点附近,再沿扩散方向逐步衰减。
应用示例
水族馆网站让访客点按潟湖照片。同心波纹带着倒影向外弯折,随后恢复平静。
也叫
- 图像涟漪
- 水波扭曲
AI 提示词
给【图片】加上水面涟漪。点按或划过时,让同心波纹从接触点开始扭曲图像,然后平息。使用轻量位移计算。减少动态效果模式下改用短暂的透明度变化。
示例代码
保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。
查看 HTML
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>水面涟漪</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="在湖面上激起涟漪">
<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>UI Skills 原创示例