gradient orb blob ambient soft background glow
Gradient Orb
gradient-orb.js
/**
* Gradient Orb — Soft radial gradient blobs that drift slowly.
* Category: decorative | Animate: true | Interactive: false | Complexity: low
*/
export default function gradientOrb(ctx, state) {
const orbs = [
{ cx: 0.3, cy: 0.3, r: 0.35, color: [99, 102, 241], speed: 0.15, phase: 0 },
{ cx: 0.7, cy: 0.6, r: 0.3, color: [168, 85, 247], speed: 0.12, phase: 2 },
{ cx: 0.5, cy: 0.8, r: 0.25, color: [236, 72, 153], speed: 0.1, phase: 4 },
];
return {
frame({ width, height, time, reducedMotion }) {
for (const orb of orbs) {
const drift = reducedMotion ? 0 : 1;
const x = orb.cx * width + Math.sin(time * orb.speed + orb.phase) * width * 0.08 * drift;
const y =
orb.cy * height + Math.cos(time * orb.speed * 0.7 + orb.phase) * height * 0.06 * drift;
const r = orb.r * Math.min(width, height);
const grad = ctx.createRadialGradient(x, y, 0, x, y, r);
grad.addColorStop(0, `rgba(${orb.color.join(',')}, 0.25)`);
grad.addColorStop(0.5, `rgba(${orb.color.join(',')}, 0.08)`);
grad.addColorStop(1, `rgba(${orb.color.join(',')}, 0)`);
ctx.fillStyle = grad;
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);
};
}
/**
* Gradient Orb — Soft radial gradient blobs that drift slowly.
* Category: decorative | Animate: true | Interactive: false | Complexity: low
*/
const __effect = function gradientOrb(ctx, state) {
const orbs = [
{ cx: 0.3, cy: 0.3, r: 0.35, color: [99, 102, 241], speed: 0.15, phase: 0 },
{ cx: 0.7, cy: 0.6, r: 0.3, color: [168, 85, 247], speed: 0.12, phase: 2 },
{ cx: 0.5, cy: 0.8, r: 0.25, color: [236, 72, 153], speed: 0.1, phase: 4 },
];
return {
frame({ width, height, time, reducedMotion }) {
for (const orb of orbs) {
const drift = reducedMotion ? 0 : 1;
const x = orb.cx * width + Math.sin(time * orb.speed + orb.phase) * width * 0.08 * drift;
const y =
orb.cy * height + Math.cos(time * orb.speed * 0.7 + orb.phase) * height * 0.06 * drift;
const r = orb.r * Math.min(width, height);
const grad = ctx.createRadialGradient(x, y, 0, x, y, r);
grad.addColorStop(0, `rgba(${orb.color.join(',')}, 0.25)`);
grad.addColorStop(0.5, `rgba(${orb.color.join(',')}, 0.08)`);
grad.addColorStop(1, `rgba(${orb.color.join(',')}, 0)`);
ctx.fillStyle = grad;
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
gradientorbblobambientsoftbackgroundglow
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| orbs | array | 3 | Number of gradient orbs |
Three radial gradient orbs in indigo, purple, and pink drift slowly using sine/cosine movement. Each orb is a createRadialGradient with alpha falloff. On prefers-reduced-motion: reduce, orbs render at their center position without movement.