dots grid wave sine animation subtle minimal background pattern
Wave Grid
wave-grid.js
/**
* Wave Grid — dot matrix with sine-wave vertical displacement and radial fade.
* Category: backgrounds | Animate: true | Interactive: false | Complexity: low
*/
export default function waveGrid(ctx, state) {
const SPACING = 28;
const DOT_RADIUS = 1.8;
const AMPLITUDE = 7;
const WAVE_SPEED = 0.7;
const WAVE_FREQ = 0.38;
return {
frame({ width, height, time, reducedMotion }) {
ctx.clearRect(0, 0, width, height);
const cols = Math.ceil(width / SPACING) + 1;
const rows = Math.ceil(height / SPACING) + 1;
const cx = width / 2;
const cy = height / 2;
const maxDist = Math.hypot(cx, cy);
for (let c = 0; c < cols; c++) {
for (let r = 0; r < rows; r++) {
const x = c * SPACING;
const baseY = r * SPACING;
const wave = reducedMotion
? 0
: Math.sin((c + r) * WAVE_FREQ + time * WAVE_SPEED) * AMPLITUDE;
const y = baseY + wave;
const dist = Math.hypot(x - cx, baseY - cy);
const proximity = Math.max(0, 1 - dist / maxDist);
const alpha = 0.06 + proximity * 0.3;
ctx.beginPath();
ctx.arc(x, y, DOT_RADIUS, 0, Math.PI * 2);
ctx.fillStyle = `oklch(0.6 0.12 250 / ${alpha.toFixed(3)})`;
ctx.fill();
}
}
},
};
}
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);
};
}
/**
* Wave Grid — dot matrix with sine-wave vertical displacement and radial fade.
* Category: backgrounds | Animate: true | Interactive: false | Complexity: low
*/
const __effect = function waveGrid(ctx, state) {
const SPACING = 28;
const DOT_RADIUS = 1.8;
const AMPLITUDE = 7;
const WAVE_SPEED = 0.7;
const WAVE_FREQ = 0.38;
return {
frame({ width, height, time, reducedMotion }) {
ctx.clearRect(0, 0, width, height);
const cols = Math.ceil(width / SPACING) + 1;
const rows = Math.ceil(height / SPACING) + 1;
const cx = width / 2;
const cy = height / 2;
const maxDist = Math.hypot(cx, cy);
for (let c = 0; c < cols; c++) {
for (let r = 0; r < rows; r++) {
const x = c * SPACING;
const baseY = r * SPACING;
const wave = reducedMotion
? 0
: Math.sin((c + r) * WAVE_FREQ + time * WAVE_SPEED) * AMPLITUDE;
const y = baseY + wave;
const dist = Math.hypot(x - cx, baseY - cy);
const proximity = Math.max(0, 1 - dist / maxDist);
const alpha = 0.06 + proximity * 0.3;
ctx.beginPath();
ctx.arc(x, y, DOT_RADIUS, 0, Math.PI * 2);
ctx.fillStyle = `oklch(0.6 0.12 250 / ${alpha.toFixed(3)})`;
ctx.fill();
}
}
},
};
}
const canvas = document.getElementById('canvas-effect');
mountCanvas(canvas, __effect, { animate: true, clear: true });
</script> Details
Animated Reduced Motion aria-hidden
dotsgridwavesineanimationsubtleminimalbackgroundpattern
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| SPACING | number | 28 | Gap between dot grid columns and rows in px |
| AMPLITUDE | number | 7 | Max vertical displacement of dots in px |
| WAVE_SPEED | number | 0.7 | Speed of wave propagation (radians per second) |
| WAVE_FREQ | number | 0.38 | Spatial frequency of the wave across the grid |
Each dot’s vertical position is offset by sin((col + row) * WAVE_FREQ + time * WAVE_SPEED) * AMPLITUDE, creating a diagonal wave across the entire grid. Alpha is driven by distance from the canvas center via a radial proximity calculation — dots near the center are most visible, fading to near-invisible at the edges for a natural vignette.
At reducedMotion: true the wave displacement is set to zero, leaving a static dot grid.