metal chrome specular gradient liquid shimmer background
Liquid Metal
liquid-metal.js
/**
* Liquid Metal — Flowing chrome gradient with animated specular highlight streak.
* Category: backgrounds | Animate: true | Interactive: false | Complexity: medium
*/
export default function liquidMetal(ctx, state) {
return {
frame({ width, height, time, reducedMotion }) {
const t = reducedMotion ? 0 : time;
// Base chrome gradient — radial center drifts
const cx = width * 0.5 + Math.sin(t * 0.38) * width * 0.22;
const cy = height * 0.42 + Math.cos(t * 0.28) * height * 0.14;
const r = Math.max(width, height) * 0.95;
const base = ctx.createRadialGradient(cx, cy, 0, cx, cy, r);
base.addColorStop(0, 'rgba(228, 232, 242, 1)');
base.addColorStop(0.18, 'rgba(188, 196, 212, 1)');
base.addColorStop(0.45, 'rgba(128, 138, 158, 1)');
base.addColorStop(0.75, 'rgba(74, 84, 102, 1)');
base.addColorStop(1, 'rgba(36, 42, 56, 1)');
ctx.fillStyle = base;
ctx.fillRect(0, 0, width, height);
if (reducedMotion) return;
// Specular streak — angled band of light
const sx = width * 0.15 + Math.sin(t * 0.52 + 0.8) * width * 0.65;
const angle = -0.38 + Math.sin(t * 0.22) * 0.12;
const bandW = 60 + Math.sin(t * 0.35) * 20;
ctx.save();
ctx.translate(sx, 0);
ctx.rotate(angle);
const streak = ctx.createLinearGradient(-bandW, 0, bandW, 0);
streak.addColorStop(0, 'rgba(255,255,255,0)');
streak.addColorStop(0.35, 'rgba(255,255,255,0.28)');
streak.addColorStop(0.5, 'rgba(255,255,255,0.55)');
streak.addColorStop(0.65, 'rgba(255,255,255,0.28)');
streak.addColorStop(1, 'rgba(255,255,255,0)');
ctx.fillStyle = streak;
ctx.fillRect(-bandW, -height, bandW * 2, height * 3);
ctx.restore();
// Secondary soft reflection
const rx = width * 0.72 + Math.cos(t * 0.41 + 2) * width * 0.18;
const ry = height * 0.3 + Math.sin(t * 0.33 + 1) * height * 0.12;
const rg = ctx.createRadialGradient(rx, ry, 0, rx, ry, 120);
rg.addColorStop(0, 'rgba(255,255,255,0.18)');
rg.addColorStop(0.5, 'rgba(255,255,255,0.06)');
rg.addColorStop(1, 'rgba(255,255,255,0)');
ctx.fillStyle = rg;
ctx.fillRect(0, 0, width, height);
},
};
}
embed.html
<canvas id="canvas-effect" style="width:100%;height:400px;display:block" aria-hidden="true"></canvas>
<script>
/**
* mountCanvas — Minimal runtime for Canvas effects.
*
* Handles HiDPI scaling, ResizeObserver, RAF loop, pointer tracking,
* reduced-motion, and cleanup. The effect only implements draw logic.
*
* @param {HTMLCanvasElement} canvas
* @param {(ctx: CanvasRenderingContext2D, state: object) => object} createEffect
* @param {object} [options]
* @returns {() => void} destroy function
*/
function mountCanvas(canvas, createEffect, options = {}) {
const ctx = canvas.getContext('2d', { alpha: true });
if (!ctx) throw new Error('2D context not available');
const opts = {
animate: true,
clear: true,
maxDpr: 2,
pauseWhenHidden: true,
respectReducedMotion: true,
...options,
};
const reducedMotion =
opts.respectReducedMotion && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const state = {
canvas,
ctx,
width: 0,
height: 0,
pixelWidth: 0,
pixelHeight: 0,
dpr: 1,
time: 0,
delta: 0,
frame: 0,
playing: false,
reducedMotion,
pointer: { x: 0, y: 0, inside: false, down: false },
};
let rafId = 0;
let lastTime = 0;
let disposed = false;
function measure() {
const rect = canvas.getBoundingClientRect();
const w = Math.max(1, rect.width);
const h = Math.max(1, rect.height);
const dpr = Math.min(window.devicePixelRatio || 1, opts.maxDpr);
const pw = Math.round(w * dpr);
const ph = Math.round(h * dpr);
if (canvas.width !== pw || canvas.height !== ph) {
canvas.width = pw;
canvas.height = ph;
}
state.width = w;
state.height = h;
state.pixelWidth = pw;
state.pixelHeight = ph;
state.dpr = dpr;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
}
function clear() {
ctx.clearRect(0, 0, state.width, state.height);
}
const effect = createEffect(ctx, state) || {};
function onResize() {
measure();
effect.resize?.(state);
if (!opts.animate || reducedMotion) {
if (opts.clear) clear();
effect.frame?.(state);
}
}
function loop(now) {
if (disposed) return;
if (opts.pauseWhenHidden && document.hidden) {
rafId = requestAnimationFrame(loop);
return;
}
state.delta = lastTime ? (now - lastTime) / 1000 : 0;
state.time = now / 1000;
state.frame += 1;
lastTime = now;
if (opts.clear) clear();
effect.frame?.(state);
rafId = requestAnimationFrame(loop);
}
function onPointerMove(e) {
const rect = canvas.getBoundingClientRect();
state.pointer.x = e.clientX - rect.left;
state.pointer.y = e.clientY - rect.top;
if (!opts.animate || reducedMotion) {
if (opts.clear) clear();
effect.frame?.(state);
}
}
function onPointerEnter() {
state.pointer.inside = true;
}
function onPointerLeave() {
state.pointer.inside = false;
state.pointer.down = false;
}
function onPointerDown() {
state.pointer.down = true;
}
function onPointerUp() {
state.pointer.down = false;
}
const ro = new ResizeObserver(onResize);
ro.observe(canvas);
canvas.addEventListener('pointermove', onPointerMove);
canvas.addEventListener('pointerenter', onPointerEnter);
canvas.addEventListener('pointerleave', onPointerLeave);
canvas.addEventListener('pointerdown', onPointerDown);
window.addEventListener('pointerup', onPointerUp);
measure();
if (opts.animate && !reducedMotion) {
state.playing = true;
rafId = requestAnimationFrame(loop);
} else {
effect.frame?.(state);
}
return function destroy() {
disposed = true;
state.playing = false;
cancelAnimationFrame(rafId);
ro.disconnect();
canvas.removeEventListener('pointermove', onPointerMove);
canvas.removeEventListener('pointerenter', onPointerEnter);
canvas.removeEventListener('pointerleave', onPointerLeave);
canvas.removeEventListener('pointerdown', onPointerDown);
window.removeEventListener('pointerup', onPointerUp);
effect.destroy?.(state);
};
}
/**
* Liquid Metal — Flowing chrome gradient with animated specular highlight streak.
* Category: backgrounds | Animate: true | Interactive: false | Complexity: medium
*/
const __effect = function liquidMetal(ctx, state) {
return {
frame({ width, height, time, reducedMotion }) {
const t = reducedMotion ? 0 : time;
// Base chrome gradient — radial center drifts
const cx = width * 0.5 + Math.sin(t * 0.38) * width * 0.22;
const cy = height * 0.42 + Math.cos(t * 0.28) * height * 0.14;
const r = Math.max(width, height) * 0.95;
const base = ctx.createRadialGradient(cx, cy, 0, cx, cy, r);
base.addColorStop(0, 'rgba(228, 232, 242, 1)');
base.addColorStop(0.18, 'rgba(188, 196, 212, 1)');
base.addColorStop(0.45, 'rgba(128, 138, 158, 1)');
base.addColorStop(0.75, 'rgba(74, 84, 102, 1)');
base.addColorStop(1, 'rgba(36, 42, 56, 1)');
ctx.fillStyle = base;
ctx.fillRect(0, 0, width, height);
if (reducedMotion) return;
// Specular streak — angled band of light
const sx = width * 0.15 + Math.sin(t * 0.52 + 0.8) * width * 0.65;
const angle = -0.38 + Math.sin(t * 0.22) * 0.12;
const bandW = 60 + Math.sin(t * 0.35) * 20;
ctx.save();
ctx.translate(sx, 0);
ctx.rotate(angle);
const streak = ctx.createLinearGradient(-bandW, 0, bandW, 0);
streak.addColorStop(0, 'rgba(255,255,255,0)');
streak.addColorStop(0.35, 'rgba(255,255,255,0.28)');
streak.addColorStop(0.5, 'rgba(255,255,255,0.55)');
streak.addColorStop(0.65, 'rgba(255,255,255,0.28)');
streak.addColorStop(1, 'rgba(255,255,255,0)');
ctx.fillStyle = streak;
ctx.fillRect(-bandW, -height, bandW * 2, height * 3);
ctx.restore();
// Secondary soft reflection
const rx = width * 0.72 + Math.cos(t * 0.41 + 2) * width * 0.18;
const ry = height * 0.3 + Math.sin(t * 0.33 + 1) * height * 0.12;
const rg = ctx.createRadialGradient(rx, ry, 0, rx, ry, 120);
rg.addColorStop(0, 'rgba(255,255,255,0.18)');
rg.addColorStop(0.5, 'rgba(255,255,255,0.06)');
rg.addColorStop(1, 'rgba(255,255,255,0)');
ctx.fillStyle = rg;
ctx.fillRect(0, 0, width, height);
},
};
}
const canvas = document.getElementById('canvas-effect');
mountCanvas(canvas, __effect, { animate: true, clear: true });
</script> Details
Animated Reduced Motion aria-hidden
metalchromespeculargradientliquidshimmerbackground
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| COLOR stops | string | silver → dark slate | Modify the base.addColorStop() calls to change the metal palette |
A radial chrome gradient with a drifting center creates the base — dark at the periphery, bright at the highlight. A narrow rotated band represents the specular highlight that moves across the surface as if the viewing angle is shifting.
prefers-reduced-motion freezes all movement and renders the static gradient only.