176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.
HTML ai-promptsanimationcss-animationsfrontendgithub-pagesjavascriptmicrointeractionsmotion-designno-dependenciesreferencescroll-animationui-animationweb-animationweb-developmentwebgl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Animated Gradient Background — Animation Handbook</title>
<meta name="description" content="Colors slowly drift across a soft gradient background. Best for hero sections.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/animated-gradient-background/">
<link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png">
<link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
<link rel="apple-touch-icon" href="../../../apple-touch-icon.png">
<meta property="og:type" content="article">
<meta property="og:site_name" content="Animation Handbook">
<meta property="og:title" content="Animated Gradient Background — Animation Handbook">
<meta property="og:description" content="Colors slowly drift across a soft gradient background. Best for hero sections.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/animated-gradient-background/">
<meta property="og:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Animated Gradient Background — Animation Handbook">
<meta name="twitter:description" content="Colors slowly drift across a soft gradient background. Best for hero sections.">
<meta name="twitter:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
<script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Animated Gradient Background", "name": "Animated Gradient Background", "description": "Colors slowly drift across a soft gradient background. Best for hero sections.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/animated-gradient-background/", "inLanguage": "en", "articleSection": "Ambient & Background", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Ambient & Background, Animated Gradient Background"}</script>
<style>
:root{--ui-accent:#ffce5a;--dur:20s;--fast:4s}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
.stage{position:relative;overflow:hidden}
/* ── Gradient variants ── */
.grad-bg{position:absolute;inset:0}
.var-pos{background:linear-gradient(135deg,#0d1a2e,#1a0d2e,#0d2e1a,#2e1a0d,#0d1a2e);background-size:400% 400%;animation:grad-pos var(--dur) ease infinite}
.var-hue{background:linear-gradient(135deg,#58a6ff,#d2a8ff,#56d364,#ffa657);animation:grad-hue var(--dur) linear infinite}
.var-multi{background:linear-gradient(135deg,#0d1a2e 0%,#58a6ff 25%,#d2a8ff 50%,#56d364 75%,#0d1a2e 100%);background-size:300% 300%;animation:grad-pos calc(var(--dur)*1.5) ease infinite}
@keyframes grad-pos{0%{background-position:0% 50%}50%{background-position:100% 50%}100%{background-position:0% 50%}}
@keyframes grad-hue{from{filter:saturate(var(--sat,1)) hue-rotate(0deg)}to{filter:saturate(var(--sat,1)) hue-rotate(360deg)}}
.grad-bg.subtle{--sat:.3;filter:saturate(.3)}
/* Color sets: a class on the stage swaps the colors of each variant */
.pal-sunset .var-pos{background:linear-gradient(135deg,#1a0d05,#2e1505,#1a0518,#2e0505,#1a0d05);background-size:400% 400%}
.pal-sunset .var-hue{background:linear-gradient(135deg,#ffa657,#f85149,#d2a8ff,#ffa657)}
.pal-sunset .var-multi{background:linear-gradient(135deg,#1a0d05 0%,#ffa657 25%,#f85149 50%,#d2a8ff 75%,#1a0d05 100%);background-size:300% 300%}
.pal-ocean .var-pos{background:linear-gradient(135deg,#05101a,#051a1a,#05101a,#0a0525,#05101a);background-size:400% 400%}
.pal-ocean .var-hue{background:linear-gradient(135deg,#58a6ff,#56d364,#0d3040,#58a6ff)}
.pal-ocean .var-multi{background:linear-gradient(135deg,#05101a 0%,#58a6ff 25%,#56d364 50%,#0d3040 75%,#05101a 100%);background-size:300% 300%}
.pal-mono .var-pos{background:linear-gradient(135deg,#050608,#0d1117,#1a1f28,#0d1117,#050608);background-size:400% 400%}
.pal-mono .var-hue{background:linear-gradient(135deg,#3a4050,#c9d1d9,#3a4050)}
.pal-mono .var-multi{background:linear-gradient(135deg,#050608 0%,#3a4050 25%,#c9d1d9 50%,#3a4050 75%,#050608 100%);background-size:300% 300%}
/* Foreground card */
.fg-card{position:absolute;bottom:40px;left:50%;transform:translateX(-50%);width:clamp(200px,60%,320px);background:rgba(6,10,16,.75);backdrop-filter:blur(10px);-webkit-backdrop-filter:blur(10px);border:1px solid rgba(255,255,255,.12);border-radius:10px;padding:20px 22px;z-index:10}
.fg-eyebrow{font-size:11px;font-weight:600;letter-spacing:2px;text-transform:uppercase;color:var(--ui-accent);margin-bottom:8px}
.fg-title{font-size:clamp(15px,2vw,20px);font-weight:700;color:#fff;margin-bottom:8px;line-height:1.2}
.fg-body{font-size:13px;color:rgba(255,255,255,.72);line-height:1.6}
/* On phones and short screens the stage is small, so the card shrinks to leave the gradient in view */
@media(max-width:600px),(max-height:760px){
.fg-card{padding:12px 14px}
.fg-eyebrow{margin-bottom:4px}
.fg-title{margin-bottom:0}
.fg-body{display:none}
}
/* ── See inside and Compare: nothing of this shows in Normal ── */
.vin,.cmp-fast,.cmp-card,.cmp-name{display:none}
.lbl{padding:3px 7px;border-radius:5px;background:rgba(8,8,10,.92);color:#f4f4f2;font:600 13px/1.2 var(--hb-font)}
/* See inside: the whole gradient as a small map with the box as a window sliding over it, or the color wheel. The live gradient
keeps filling the stage behind it. */
.stage[data-hb-mode="inside"] .fg-card{display:none}
.stage[data-hb-mode="inside"] .vin{display:flex}
.vin{position:absolute;inset:0;z-index:5;flex-direction:column;align-items:center;gap:6px;padding:8px 10px;pointer-events:none}
.vroom{flex:1;min-height:0;width:100%;padding:7px;container-type:size;display:grid;place-items:center}
.vfig{--f:4;--k:1;--ar:var(--sar,2);--ov:72px;display:grid;grid-template-columns:minmax(0,1fr);justify-items:center;row-gap:4px;width:max(0px,min(100cqw,calc((100cqh - var(--ov))*var(--ar))))}
.vfig.wheel{--ar:1;--ov:54px}
.vcap{max-width:100%;text-align:center;text-wrap:balance}
.vfig .lbl{width:max-content;max-width:100cqw}
.vbox,.vwheel{position:relative;width:100%;aspect-ratio:var(--ar)}
.vwheel,.vfig.wheel .vbox,.vfig.wheel .varrow{display:none}
.vfig.wheel .vwheel{display:block}
/* a dark mat and a light frame round the map, wide enough to hold the dashed outline of where the window began */
.vbox{margin-bottom:7px;border-radius:6px;box-shadow:0 0 0 5px rgba(8,8,10,.6),0 0 0 7px rgba(244,244,242,.85)}
/* the map shows the gradient whole, in the page's own colors; it beats the palette rules that paint it oversized */
.vbox .vmap.grad-bg{border-radius:6px;background-size:100% 100%;animation:none}
.vstart,.vwin{position:absolute;left:0;top:calc(50% - 50%/var(--f));width:calc(100%/var(--f));height:calc(100%/var(--f));border-radius:3px}
.vstart{outline:2px dashed rgba(244,244,242,.85);outline-offset:2px}
.vwin{border:3px solid var(--ui-accent);box-shadow:0 0 0 1.5px rgba(8,8,10,.8);animation:vslide calc(var(--dur)*var(--k)) ease infinite}
@keyframes vslide{0%,100%{transform:translateX(0)}50%{transform:translateX(calc((var(--f) - 1)*100%))}}
.varrow{position:relative;width:100%;height:16px;border-radius:5px;background:rgba(8,8,10,.92)}
.varrow i{position:absolute;left:calc(50%/var(--f));right:calc(50%/var(--f));top:6px;height:4px;border-radius:2px;background:var(--ui-accent)}
.varrow i::before,.varrow i::after{content:"";position:absolute;top:-4px;border:6px solid transparent}
.varrow i::before{left:0;border-left:0;border-right:10px solid var(--ui-accent)}
.varrow i::after{right:0;border-right:0;border-left:10px solid var(--ui-accent)}
/* the wheel: hue goes around, saturation outward; the dots turn with the colors, faint dashed dots mark where each began */
.vwheel::before{content:"";position:absolute;inset:0;border-radius:50%;background:rgba(8,8,10,.6)}
.vdisc,.vdots,.vstarts{position:absolute;inset:13%;border-radius:50%}
.vdisc{background:radial-gradient(circle closest-side,#fff,rgba(255,255,255,0)),conic-gradient(#f00,#ff0,#0f0,#0ff,#00f,#f0f,#f00);box-shadow:0 0 0 2px rgba(244,244,242,.85)}
.vdots{animation:vturn var(--dur) linear infinite,grad-hue var(--dur) linear infinite}
@keyframes vturn{to{transform:rotate(360deg)}}
.vdot,.vdot0{position:absolute;width:clamp(12px,11%,22px);aspect-ratio:1;border-radius:50%;transform:translate(-50%,-50%)}
.vdot{border:2px solid #fff;box-shadow:0 0 0 1.5px rgba(8,8,10,.8)}
.vdot0{outline:2px dashed rgba(244,244,242,.9);outline-offset:3px}
.vturn{position:absolute;inset:0;width:100%;height:100%;overflow:visible}
.vturn .ring{fill:none;stroke:var(--ui-accent);stroke-width:2.4;stroke-linecap:round}
.vturn .head{fill:var(--ui-accent)}
/* Compare: on the left a copy of the gradient on a cycle that is too fast, on the right the real one as set; each half is named
above its picture with its cycle in words and a bar as long as how fast its colors change */
.stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:auto minmax(0,1fr);gap:6px;padding:6px;background:#08080a}
.stage[data-hb-mode="compare"] #grad-bg,.stage[data-hb-mode="compare"] .cmp-fast{position:relative;inset:auto;grid-row:2;border-radius:8px}
.stage[data-hb-mode="compare"] #grad-bg{grid-column:2}
.stage[data-hb-mode="compare"] .cmp-fast{display:block;grid-column:1;animation-duration:var(--fast)}
.stage[data-hb-mode="compare"] .cmp-fast.var-multi{animation-duration:calc(var(--fast)*1.5)}
.stage[data-hb-mode="compare"] .cmp-name{display:block;grid-row:1;position:relative;z-index:6;min-width:0}
.stage[data-hb-mode="compare"] .cmp-a{grid-column:1}
.stage[data-hb-mode="compare"] .cmp-b{grid-column:2}
.cmp-name b{display:block;font-weight:700}
.cmp-name span{font-size:12px;font-weight:500;color:#d6d6d2;white-space:nowrap}
.cmp-bar{display:block;height:5px;margin-top:4px;border-radius:3px;background:var(--ui-accent);width:max(4px,calc(var(--rate)*18%))}
.stage[data-hb-mode="compare"] .fg-card,.stage[data-hb-mode="compare"] .cmp-card{bottom:24px;width:min(320px,calc(50% - 48px));background:rgba(6,10,16,.9);-webkit-backdrop-filter:none;backdrop-filter:none}
.stage[data-hb-mode="compare"] .fg-card{left:75%}
.stage[data-hb-mode="compare"] .cmp-card{display:block;left:25%}
.stage[data-hb-mode="compare"] .fg-body{display:none}
@media(max-width:600px){.cmp-name span{display:block}}
@media(max-width:600px),(max-height:500px){.stage[data-hb-mode="compare"] .fg-card,.stage[data-hb-mode="compare"] .cmp-card{display:none}}
</style>
<link rel="stylesheet" href="../../../assets/css/demo-page.css?v=11">
<script src="../../../assets/js/demo-page.js?v=11" defer></script>
</head>
<body class="hb" data-hb-kind="loop">
<nav class="hb-bar" aria-label="Animation Handbook">
<a class="hb-home" href="../../../"><svg class="hb-mark" viewBox="0 0 32 32" aria-hidden="true"><g class="g"><circle cx="11.16" cy="20.84" r="6.1"/><circle cx="15.12" cy="16.88" r="6.1"/><circle cx="18.51" cy="13.49" r="6.1"/></g><circle class="b" cx="21.2" cy="10.8" r="6.6"/></svg><span>Animation Handbook</span></a>
<span class="hb-pager"><a href="../mesh-gradient/" rel="next" aria-label="Next: Mesh Gradient Animation"><span><span class="hb-dir">Next: </span>Mesh Gradient Animation</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg></a></span>
<a class="hb-star" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener" aria-label="Star on GitHub"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg><span>Star on GitHub</span></a>
</nav>
<main class="hb-page">
<header class="hb-head">
<p class="hb-cat">07.01 · Ambient & Background</p>
<h1>Animated Gradient Background</h1>
<p class="hb-lede">Colors slowly drift across a soft gradient background. Best for hero sections.</p>
</header>
<section class="hb-step hb-watch" aria-labelledby="hb-watch-title">
<div class="hb-step-head">
<span class="hb-num" aria-hidden="true">1</span>
<div><h2 id="hb-watch-title">Watch it</h2><p>It moves by itself. Pause it to look closely, or turn on slow motion to see each part of the movement.</p></div>
</div>
<div class="seg hb-modes" role="group" aria-label="What the box shows" data-hb-modes>
<button type="button" data-hb-mode="normal" class="on" aria-pressed="true">Normal</button>
<button type="button" data-hb-mode="inside" aria-pressed="false">See inside</button>
<button type="button" data-hb-mode="compare" aria-pressed="false">Compare</button>
</div>
<div class="stage" id="stage">
<div class="grad-bg var-pos" id="grad-bg" aria-hidden="true"></div>
<div class="fg-card">
<div class="fg-eyebrow">Animated gradient</div>
<div class="fg-title">Color that moves without demanding it</div>
<div class="fg-body">A slow cycle keeps it calm: the color shifts, and the eye barely notices.</div>
</div>
<div class="vin" aria-hidden="true" inert>
<p class="lbl vcap" data-hb-caption>Painted four times as wide and tall as the box</p>
<div class="vroom"><div class="vfig">
<div class="vbox"><i class="vmap grad-bg var-pos"></i><i class="vstart"></i><i class="vwin"></i></div>
<div class="vwheel"><i class="vdisc grad-bg"></i><div class="vstarts"></div><div class="vdots grad-bg"></div><svg class="vturn" viewBox="0 0 100 100"><path class="ring" d="M50 6A44 44 0 1 1 31.4 10.1"/><path class="head" d="M46 2 55 6 46 10z" transform="rotate(335 50 50)"/></svg></div>
<p class="lbl vcap" data-hb-caption>The box shows only the outlined part</p>
<div class="varrow"><i></i></div>
<p class="lbl vcap" data-hb-caption>Slides across and back every 20 seconds</p>
</div></div>
</div>
</div>
<div class="hb-player">
<button class="hb-play" type="button" id="btn-pause" data-hb-pause="css" data-state="playing"><svg class="hb-ic hb-i-pause" viewBox="0 0 24 24" aria-hidden="true"><rect x="6" y="4" width="4" height="16" rx="1"/><rect x="14" y="4" width="4" height="16" rx="1"/></svg><svg class="hb-ic hb-i-play" viewBox="0 0 24 24" aria-hidden="true"><path d="M6 3l14 9-14 9z"/></svg><span class="hb-pause-label">Pause</span></button>
<label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo="css" autocomplete="off"><span>Slow motion</span></label>
</div>
<div class="hb-mode-notes" aria-live="polite">
<p data-hb-for="inside" hidden>Inside, the gradient is painted four times as wide and tall as the box, and the box shows only the outlined part as it slides across and back.</p>
<p data-hb-for="compare" hidden>Too fast runs a whole cycle in a few seconds, which pulls the eye from the content. Calm, as you set it, takes several times as long, so it stays in the background.</p>
</div>
</section>
<section class="hb-step hb-try" aria-labelledby="hb-try-title">
<div class="hb-step-head">
<span class="hb-num" aria-hidden="true">2</span>
<div><h2 id="hb-try-title">Try it</h2><p>Change a setting and see the difference as it moves.</p></div>
</div>
<div class="hb-settings">
<div class="hb-setting">
<p class="hb-setting-name" id="grad-lbl">Gradient</p>
<div class="seg" id="grad-seg" role="group" aria-labelledby="grad-lbl">
<button type="button" data-cls="var-pos" class="on" aria-pressed="true">Sliding</button>
<button type="button" data-cls="var-hue" aria-pressed="false">Color wheel</button>
<button type="button" data-cls="var-multi" aria-pressed="false">Bright bands</button>
</div>
<p class="hb-hint">Sliding is the calmest; the color wheel turns every color.</p>
</div>
<div class="hb-setting">
<p class="hb-setting-name" id="speed-lbl">Speed</p>
<div class="seg" id="speed-seg" role="group" aria-labelledby="speed-lbl">
<button type="button" data-secs="32" aria-pressed="false">Slow</button>
<button type="button" data-secs="20" class="on" aria-pressed="true">Normal</button>
<button type="button" data-secs="12" aria-pressed="false">Fast</button>
</div>
<p class="hb-hint">Longer cycles feel calmer.</p>
</div>
<div class="hb-setting">
<p class="hb-setting-name" id="pal-lbl">Colors</p>
<div class="seg" id="pal-seg" role="group" aria-labelledby="pal-lbl">
<button type="button" data-pal="" class="on" aria-pressed="true">Cool</button>
<button type="button" data-pal="pal-sunset" aria-pressed="false">Sunset</button>
<button type="button" data-pal="pal-ocean" aria-pressed="false">Ocean</button>
<button type="button" data-pal="pal-mono" aria-pressed="false">Gray</button>
</div>
<p class="hb-hint">The set of colors the gradient moves through.</p>
</div>
</div>
<details class="hb-options">
<summary><span class="hb-more-label">More options</span><span class="hb-less">Fewer options</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg></summary>
<div class="hb-settings">
<div class="hb-setting">
<label class="hb-switch-row"><span>Muted colors</span><input class="hb-switch" type="checkbox" role="switch" id="muted-tog" autocomplete="off"></label>
<p class="hb-hint">Takes most of the color out for a quieter look.</p>
</div>
</div>
</details>
</section>
<section class="hb-step hb-prompt-step" aria-labelledby="hb-prompt-title">
<div class="hb-step-head">
<span class="hb-num" aria-hidden="true">3</span>
<div><h2 id="hb-prompt-title">Copy the prompt</h2><p>Paste it into an AI assistant such as ChatGPT, Claude or Cursor to build this in your own project.</p></div>
</div>
<div class="hb-card">
<p class="hb-prompt">Add an animated gradient background to [the section you want behind your content]. Paint a soft gradient much larger than the section and slowly slide it back and forth, or turn all its colors around the color wheel, so the colors keep shifting behind the content. End the gradient on the color it starts with, so the loop never shows a seam. A long, slow cycle feels calm; a fast one pulls attention away from the content. If the visitor has reduced motion turned on, show the gradient still. Match the settings listed below.</p>
<div class="hb-chips-box" hidden>
<p class="hb-chips-title">Your settings</p>
<ul class="hb-chips"></ul>
</div>
<div class="hb-copy-row">
<button class="hb-copy" type="button"><svg class="hb-ic hb-i-copy" viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg><svg class="hb-ic hb-i-done" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg><span class="hb-copy-label" aria-live="polite">Copy prompt</span></button>
<p class="hb-hint">Replace the words in brackets with your own element. Your settings are added at the end.</p>
</div>
</div>
<div class="hb-star-row"><p>Free and open source. If this helped, a star on GitHub helps other people find it.</p><a class="hb-star-btn" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg>Star on GitHub</a></div>
</section>
<section class="hb-about" aria-labelledby="hb-what-title">
<div class="hb-what">
<h2 id="hb-what-title">What it is</h2>
<div class="hb-what-text"><p>The full explanation is in <a href="README.md">README.md</a>.</p></div>
</div>
<div class="hb-fit">
<h2>Good for</h2>
<ul class="hb-tags hb-good"><li>Hero sections</li><li>Landing pages</li><li>Dashboards</li><li>Splash screens</li></ul>
<h3>Avoid on</h3>
<ul class="hb-tags hb-avoid"><li>Long reading pages</li><li>Small elements</li></ul>
</div>
</section>
<section class="hb-related" aria-labelledby="hb-related-title" hidden>
<h2 id="hb-related-title">Similar animations</h2>
<div class="hb-rel-list"></div>
</section>
</main>
<footer class="hb-foot"><span>Animation Handbook</span><a href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener">Source on GitHub</a><a href="#top">Back to top ↑</a></footer>
<script>
const stage=document.getElementById('stage');
const bg=document.getElementById('grad-bg');
const vars=document.documentElement.style;
const mutedTog=document.getElementById('muted-tog');
let secs=20;
function choices(id,pick){
const buttons=document.querySelectorAll('#'+id+' button');
buttons.forEach(b=>b.addEventListener('click',()=>{
buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});
pick(b.dataset);
}));
}
choices('grad-seg',d=>{
bg.classList.remove('var-pos','var-hue','var-multi');
bg.classList.add(d.cls);
});
// A new --dur keeps the time already played, so the colors would jump to another point of the cycle.
// Scaling each animation's time by the same factor as its length keeps the picture where it is, moving or paused.
choices('speed-seg',d=>{
const next=+d.secs;
// (Compare's too-fast copy has a cycle of its own, so it stays out of this)
stage.getAnimations({subtree:true}).forEach(a=>{if(a.currentTime!=null&&!a.effect.target.closest('.cmp-fast'))a.currentTime=a.currentTime*next/secs});
secs=next;
vars.setProperty('--dur',next+'s');
});
choices('pal-seg',d=>{
stage.classList.remove('pal-sunset','pal-ocean','pal-mono');
if(d.pal)stage.classList.add(d.pal);
});
mutedTog.addEventListener('change',()=>{bg.classList.toggle('subtle',mutedTog.checked)});
// ── See inside and Compare ──
// Every view animation is a CSS animation inside the stage, so Pause and Slow motion reach it. An animation starts at zero when
// its element appears, so each is set to the real gradient's time when its view is drawn.
const reduceMQ=matchMedia('(prefers-reduced-motion: reduce)'),slowTog=document.getElementById('slow-tog');
const part=s=>stage.querySelector(s);
const vin=part('.vin'),vfig=part('.vfig'),vmap=part('.vmap'),vdisc=part('.vdisc'),vdots=part('.vdots'),vstarts=part('.vstarts');
const caps=[...vin.querySelectorAll('.vcap')],card=part('.fg-card');
const notes=[...document.querySelectorAll('[data-hb-for]')],moving=notes.map(n=>n.textContent);
const STYLES=['var-pos','var-hue','var-multi'],FAST=parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--fast'));
const REF=secs,POSE=1000; // the bars measure against Normal's speed; Compare's still is one second into the cycle
let mode='normal',posed=false,saved=0,posedOn=null,lastSecs=secs;
const kind=()=>bg.classList.contains('var-hue')?'hue':bg.classList.contains('var-multi')?'bands':'slide';
const paused=()=>stage.classList.contains('hb-paused');
const lead=()=>bg.getAnimations()[0];
const mark=el=>{el.setAttribute('aria-hidden','true');el.inert=true;return el};
// Compare's parts, made once: the faster copy of the gradient, a copy of the card and the two names
const fast=mark(Object.assign(document.createElement('div'),{className:'grad-bg cmp-fast var-pos'}));
const cardCopy=mark(card.cloneNode(true));cardCopy.classList.add('cmp-card');
const names=['Too fast','Calm'].map((t,i)=>{
const el=mark(document.createElement('div'));el.className='lbl cmp-name cmp-'+'ab'[i];el.setAttribute('data-hb-caption','');
el.innerHTML='<b>'+t+'</b><span>a full cycle </span><span class="n2"></span><i class="cmp-bar"></i>';return el;
});
stage.append(fast,cardCopy,...names);
// A note is written only when its words change, since its box is read out loud; See inside's still words show while the loop is
// paused under reduced motion, and Compare's while its pose shows
const STILL_IN={slide:'Inside, shown still: the gradient is four times as wide and tall as the box, which shows only the outlined part; the arrow shows how far that part slides.',
hue:'Inside, shown still: the gradient stays put while all its colors turn around the color wheel together; the arrow shows they go all the way around.'};
const HUE_IN='Inside, the gradient itself stays put: every one of its colors turns around the color wheel together, so the whole picture changes color.';
const STILL_CMP='Both are shown still, one second in: Too fast has already changed color a lot, while Calm has changed much less.';
function describe(){
const k=kind(),still=reduceMQ.matches&&paused(),three=s=>k==='bands'?s.replace('four','three'):s;
const words=[k==='hue'?(still?STILL_IN.hue:HUE_IN):three(still?STILL_IN.slide:moving[0]),posed?STILL_CMP:moving[1]];
notes.forEach((n,i)=>{if(n.textContent!==words[i])n.textContent=words[i]});
}
describe();
reduceMQ.addEventListener('change',describe);
// The words on the views come from the page's own numbers: the cycle is Speed's (one and a half times as long for bright bands,
// three times as long in slow motion) and Too fast's is --fast
function texts(){
const k=kind(),f=(k==='bands'?1.5:1)*(slowTog.checked?3:1),real=secs*f;
const c=k==='hue'?["Each dot is one of the gradient's colors",'All turn around the color wheel together','Once around every '+real+' seconds']
:['Painted '+(k==='bands'?'three':'four')+' times as wide and tall as the box','The box shows only the outlined part','Slides across and back every '+real+' seconds'];
caps.forEach((el,i)=>{if(el.textContent!==c[i])el.textContent=c[i]});
[FAST*f,real].forEach((s,i)=>{names[i].querySelector('.n2').textContent='every '+s+' seconds';names[i].style.setProperty('--rate',REF/s)});
fit();
}
// The map has the box's own proportions, so the window covers what the box shows; it takes the room left under the first
// caption once the other captions (which can wrap on a narrow box) are measured
function fit(){
if(mode!=='inside')return;
const st=vfig.style,pic=vfig.querySelector(kind()==='hue'?'.vwheel':'.vbox');
st.setProperty('--sar',stage.clientWidth/stage.clientHeight);
st.setProperty('--ov',vfig.offsetHeight-pic.offsetHeight+'px');
}
function follow(b){const a=lead();if(a&&a.currentTime!=null&&b.playState!=='idle'){b.currentTime=a.currentTime;b.playbackRate=a.playbackRate}}
function syncTime(el){el.getAnimations({subtree:true}).forEach(b=>{follow(b);b.ready.then(()=>follow(b),()=>{})})}
// The wheel: one dot for each color of the gradient, at its hue around the wheel and its saturation out from the middle
function placeDots(){
vdots.textContent=vstarts.textContent='';
new Set(getComputedStyle(bg).backgroundImage.match(/rgba?\([^)]+\)/g)).forEach(c=>{
const [r,g,b]=c.match(/[\d.]+/g).map(Number),top=Math.max(r,g,b),d=top-Math.min(r,g,b);
let h=0;if(d)h=((top===r?((g-b)/d)%6:top===g?(b-r)/d+2:(r-g)/d+4)*60+360)%360;
const rad=Math.min(.88,Math.max(.2,d/top||0))*50,at=h*Math.PI/180;
[['vdot',vdots],['vdot0',vstarts]].forEach(([cls,to])=>{
const dot=document.createElement('i');dot.className=cls;
dot.style.cssText='left:'+(50+rad*Math.sin(at))+'%;top:'+(50-rad*Math.cos(at))+'%'+(cls==='vdot'?';background:'+c:'');
to.appendChild(dot);
});
});
}
function drawInside(){
const k=kind(),muted=bg.classList.contains('subtle'),st=vfig.style;
vfig.classList.toggle('wheel',k==='hue');
st.setProperty('--f',k==='bands'?3:4);
st.setProperty('--k',k==='bands'?1.5:1);
vmap.className='vmap grad-bg '+(k==='bands'?'var-multi':'var-pos')+(muted?' subtle':'');
[vdisc,vdots].forEach(el=>el.classList.toggle('subtle',muted));
if(k==='hue')placeDots();
texts();syncTime(vin);
}
// Compare: the copy follows the real gradient's style and muting; both then run from the same moment. Where the loop is paused
// the two cycles can line up and the halves look alike, so both are posed one second in, and the real time is kept to put back.
function drawCompare(entering){
fast.className='grad-bg cmp-fast '+STYLES.find(c=>bg.classList.contains(c))+(bg.classList.contains('subtle')?' subtle':'');
const a=lead(),c=fast.getAnimations()[0];
if(a&&c&&a.currentTime!=null){
if(paused()){
if(!posed){saved=a.currentTime;posedOn=a;posed=true}
a.currentTime=c.currentTime=POSE;
}else if(entering)syncTime(fast);
}
texts();
}
function leaveCompare(){const a=lead();if(posed&&a)a.currentTime=saved;posed=false}
document.addEventListener('hb:mode',e=>{
if(mode==='compare'&&e.detail.mode!=='compare')leaveCompare();
mode=e.detail.mode;
if(mode==='inside')drawInside();
if(mode==='compare')drawCompare(true);
describe();
});
// Play lifts the pose and the gradient carries on from where it is; every Pause poses again
document.addEventListener('hb:pause',e=>{
if(!e.detail.paused)posed=false;
else if(mode==='compare')drawCompare(false);
describe();
});
const on=(id,type,fn)=>document.getElementById(id).addEventListener(type,fn);
// A gradient with another animation starts it at zero, as in Normal, so a pose is dealt again and the real time to put back is
// zero; Sliding and Bright bands share one animation, which keeps its time, so the time to put back stays
on('grad-seg','click',()=>{
if(mode==='compare'){if(posed&&lead()!==posedOn){saved=0;posedOn=lead()}drawCompare(true)}
if(mode==='inside')drawInside();
describe();
});
on('pal-seg','click',()=>{if(mode==='inside')drawInside()});
on('muted-tog','change',()=>{if(mode==='inside')drawInside();if(mode==='compare')drawCompare(false)});
// Speed rescales the real time (and every view animation); the posed time to put back is rescaled with it
on('speed-seg','click',()=>{
const f=secs/lastSecs;lastSecs=secs;
if(mode==='compare'&&posed){saved*=f;drawCompare(false)}
if(mode!=='normal')texts();
});
on('slow-tog','change',()=>{if(mode!=='normal')texts()});
new ResizeObserver(fit).observe(stage);
if(document.fonts)document.fonts.ready.then(fit);
</script>
</body>
</html>