Liquid Image Distortion
液态图像扭曲
A moving ripple bends the image directly under the pointer, then the pixels settle into place.
Where to use it
Add a tactile response to an image-led hero or a gallery cover. Keep important text in a separate layer and concentrate the distortion around the pointer so most of the image stays recognizable.
Practical example
On a travel journal, moving across a canal photograph sends a ripple through the water below the pointer. As the reader moves away, the scene settles back into focus.
AI prompt
Add Liquid Image Distortion to [target image]. Keep an undistorted source and offset sample coordinates in a small radial field centered on the pointer. Fade the displacement toward the edge of the field. Replay a finite ripple on tap or keyboard activation, stop animation when idle, and show the original image with reduced motion.
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>Liquid Image Distortion</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;
}
.liquid-panel {
display: block; position: relative; width: min(100%, 420px); padding: 0;
max-width: calc((100vh - 30px) * 2.23);
overflow: hidden; border: 1px solid #c3d3ff5c; border-radius: 12px;
background: linear-gradient(135deg, #153a56, #295a69 60%, #885bae);
color: #eaf8ff; cursor: pointer;
}
.liquid-panel canvas { display: block; width: 100%; height: auto; }
.liquid-panel span {
position: absolute; right: 7px; bottom: 7px; padding: 3px 7px;
border: 1px solid #bdd9ff54; border-radius: 5px; background: #112838d9;
font: 640 10px/1.45 system-ui, sans-serif; pointer-events: none;
}
@media (prefers-reduced-motion: reduce) {
.liquid-panel { cursor: default; }
.liquid-panel span { display: none; }
}
</style>
</head>
<body>
<main class="demo">
<button type="button" class="liquid-panel" aria-label="Play the image ripple">
<canvas width="420" height="188" aria-hidden="true"></canvas>
<span>Tap for a ripple</span>
</button>
</main>
<script>
const panel = document.querySelector('.liquid-panel');
const canvas = panel.querySelector('canvas');
const context = canvas.getContext('2d', { willReadFrequently: true });
const motionPreference = matchMedia('(prefers-reduced-motion: reduce)');
const { width, height } = canvas;
function paintOriginal() {
const base = context.createLinearGradient(0, 0, width, height);
base.addColorStop(0, '#0d2a46');
base.addColorStop(.55, '#215468');
base.addColorStop(1, '#392c64');
context.fillStyle = base;
context.fillRect(0, 0, width, height);
const glow = context.createRadialGradient(302, 74, 2, 302, 74, 120);
glow.addColorStop(0, '#ffe57ab0');
glow.addColorStop(.42, '#ee91ab55');
glow.addColorStop(1, '#ffb8ea00');
context.fillStyle = glow;
context.fillRect(0, 0, width, height);
context.save();
context.beginPath();
context.arc(302, 70, 39, 0, Math.PI * 2);
context.clip();
const sun = context.createLinearGradient(0, 32, 0, 109);
sun.addColorStop(0, '#fff3bc');
sun.addColorStop(1, '#fba6a7');
context.fillStyle = sun;
for (let y = 30; y < 111; y += 6) context.fillRect(261, y, 82, 4);
context.restore();
for (let row = 0; row < 19; row += 1) {
const shade = context.createLinearGradient(0, 0, width, 0);
shade.addColorStop(0, '#9cf5dd');
shade.addColorStop(.46, '#8fd6ff');
shade.addColorStop(1, '#dab8fa');
context.strokeStyle = shade;
context.globalAlpha = row % 4 === 0 ? .86 : .45;
context.lineWidth = row % 4 === 0 ? 1.55 : .85;
context.beginPath();
for (let x = 0; x <= width; x += 3) {
const y = 39 + row * 8 + Math.sin(x / 47 + row * .19) * 23
+ Math.sin(x / 72 - row * .08) * 19 - x * .058;
if (x === 0) context.moveTo(x, y);
else context.lineTo(x, y);
}
context.stroke();
}
context.globalAlpha = 1;
}
paintOriginal();
const original = context.getImageData(0, 0, width, height);
const distorted = context.createImageData(width, height);
const point = { x: width / 2, y: height / 2 };
let hovering = false;
let pulseStart = -Infinity;
let animation = 0;
function render(time) {
animation = 0;
const elapsed = time - pulseStart;
const pulse = Math.max(0, 1 - elapsed / 950);
const strength = Math.max(hovering ? 1 : 0, pulse);
if (motionPreference.matches || strength === 0) {
context.putImageData(original, 0, 0);
return;
}
const radius = 54 + (hovering ? 0 : 30 * (1 - pulse));
const left = Math.max(0, Math.floor(point.x - radius));
const right = Math.min(width, Math.ceil(point.x + radius));
const top = Math.max(0, Math.floor(point.y - radius));
const bottom = Math.min(height, Math.ceil(point.y + radius));
distorted.data.set(original.data);
for (let y = top; y < bottom; y += 1) {
for (let x = left; x < right; x += 1) {
const dx = x - point.x;
const dy = y - point.y;
const distance = Math.hypot(dx, dy);
if (distance >= radius || distance === 0) continue;
const falloff = (1 - distance / radius) ** 1.6;
const displacement = Math.sin(distance * .185 - time * .019) * 19 * falloff * strength;
const sourceX = Math.max(0, Math.min(width - 1, Math.round(x + dx / distance * displacement)));
const sourceY = Math.max(0, Math.min(height - 1, Math.round(y + dy / distance * displacement)));
const sourceOffset = (sourceY * width + sourceX) * 4;
const destinationOffset = (y * width + x) * 4;
distorted.data[destinationOffset] = original.data[sourceOffset];
distorted.data[destinationOffset + 1] = original.data[sourceOffset + 1];
distorted.data[destinationOffset + 2] = original.data[sourceOffset + 2];
}
}
context.putImageData(distorted, 0, 0);
animation = requestAnimationFrame(render);
}
function schedule() {
if (!animation) animation = requestAnimationFrame(render);
}
function locate(event) {
const bounds = canvas.getBoundingClientRect();
point.x = (event.clientX - bounds.left) / bounds.width * width;
point.y = (event.clientY - bounds.top) / bounds.height * height;
}
panel.addEventListener('pointermove', (event) => {
if (event.pointerType === 'touch' || motionPreference.matches) return;
locate(event);
hovering = true;
schedule();
});
panel.addEventListener('pointerleave', () => {
if (!hovering) return;
hovering = false;
pulseStart = performance.now() - 610;
schedule();
});
panel.addEventListener('click', (event) => {
if (motionPreference.matches) return;
if (event.detail === 0) {
point.x = width / 2;
point.y = height / 2;
} else {
locate(event);
}
pulseStart = performance.now();
schedule();
});
motionPreference.addEventListener('change', () => {
panel.disabled = motionPreference.matches;
hovering = false;
pulseStart = -Infinity;
if (animation) cancelAnimationFrame(animation);
animation = 0;
context.putImageData(original, 0, 0);
});
panel.disabled = motionPreference.matches;
</script>
</body>
</html>Original example by UI Skills