Skip to content

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.

feat: move Scroll-Driven Background Color to the guided-steps page

matin6938065188c2
diff --git a/animations/01-scroll-based/scroll-background-color/README.md b/animations/01-scroll-based/scroll-background-color/README.md
index d586b41c..5a36b5ec 100644
--- a/animations/01-scroll-based/scroll-background-color/README.md
+++ b/animations/01-scroll-based/scroll-background-color/README.md
@@ -2,7 +2,7 @@
## What it is
-The page background continuously blends through a palette as the user scrolls, so each section appears to own its own color while the transitions between them are smooth gradients in time rather than hard cuts. It is a portfolio-site staple: the whole canvas becomes a scroll progress indicator. Because light palette entries would make light text unreadable, the text ink flips automatically based on the computed luminance of the current background.
+A scroll-driven background color gives each section of a page its own color and blends from one to the next as you scroll, so the whole page seems to change mood. Halfway between two sections the background is a mix of both colors. When the background turns light, the text switches to dark so it stays readable.
## When to use it
@@ -27,23 +27,22 @@ t = t * t * (3 - 2 * t); // smoothstep
const r = Math.round(a[0] + (b[0] - a[0]) * t); // per-channel lerp
```
-The contrast flip computes the relative luminance of the interpolated color and toggles a class when it crosses a threshold, swapping the section text from light ink to dark ink:
+The contrast flip computes the relative luminance of the interpolated color and toggles a class when it crosses a threshold, swapping all of the section text from white to black:
```js
-const lum = (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;
-stage.classList.toggle('light', lum > 0.55);
+const lin = c => (c /= 255) <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
+const lum = 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b); // relative luminance
+stage.classList.toggle('light', lum > 0.18); // white and black text contrast equally at 0.18
```
-The scroll handler is rAF-coalesced and performs no layout reads; `maxScroll` is cached at init and on a debounced resize. The background write is guarded — channels are rounded and the write is skipped when the resulting string is unchanged.
+The scroll handler is rAF-coalesced and performs no layout reads; `maxScroll` is cached at init, when the fonts have loaded and whenever the window resizes. The background write is guarded — channels are rounded and the write is skipped when the resulting string is unchanged.
## Key parameters
-| Parameter | What it controls |
-| --- | --- |
-| Palette | Array of `[r, g, b]` stops, one per section. Order defines the journey; adjacent stops should not produce muddy midpoints. |
-| Blend window | Fraction of each segment (0.3–1) that actually blends. At 1 the color moves continuously; narrower values hold each color near section centers and confine the transition to a band around each boundary. |
-| Snap toggle | Replaces interpolation with `t = t < .5 ? 0 : 1` — a hard cut at each boundary. Exists to show why the blend matters. |
-| Luminance threshold | The cutoff (0.55 here) above which the background counts as light and the text ink flips dark. Tune per palette. |
+| Parameter | Default | Effect |
+|-----------|---------|--------|
+| Colors | Dusk | The palette, one color per section: Dusk stays dark; Daylight passes through pale colors and Forest through a light green, and the text turns dark on those |
+| Blend | Gradual | Gradual changes the color the whole way between sections; Quick holds each color and changes it near the boundary; Hard cut switches at the boundary with no blend, to show why the blend matters |
## Production notes
@@ -56,6 +55,6 @@ The scroll handler is rAF-coalesced and performs no layout reads; `maxScroll` is
## See also
-- [Scrollytelling](../scrollytelling/)
-- [Animated Gradient Background](../../07-ambient-background/animated-gradient-background/)
-- [Section Wipe](../section-wipe/)
+- [Scrollytelling](../scrollytelling/) — a picture beside the text changes as a story scrolls by
+- [Animated Gradient Background](../../07-ambient-background/animated-gradient-background/) — colors that shift on their own, with no scrolling
+- [Section Wipe](../section-wipe/) — each section slides up over the one before
diff --git a/animations/01-scroll-based/scroll-background-color/index.html b/animations/01-scroll-based/scroll-background-color/index.html
index 9b4dcc44..aa589330 100644
--- a/animations/01-scroll-based/scroll-background-color/index.html
+++ b/animations/01-scroll-based/scroll-background-color/index.html
@@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scroll-Driven Background Color — Animation Handbook</title>
- <meta name="description" content="The page background continuously blends between each section&#x27;s color as you scroll — interpolation through a palette, not a snap at boundaries.">
+ <meta name="description" content="The background color changes as you scroll through sections. Best for stories.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/scroll-background-color/">
<link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png">
<link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
@@ -12,98 +12,120 @@
<meta property="og:type" content="article">
<meta property="og:site_name" content="Animation Handbook">
<meta property="og:title" content="Scroll-Driven Background Color — Animation Handbook">
- <meta property="og:description" content="The page background continuously blends between each section&#x27;s color as you scroll — interpolation through a palette, not a snap at boundaries.">
+ <meta property="og:description" content="The background color changes as you scroll through sections. Best for stories.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/scroll-background-color/">
<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="Scroll-Driven Background Color — Animation Handbook">
- <meta name="twitter:description" content="The page background continuously blends between each section&#x27;s color as you scroll — interpolation through a palette, not a snap at boundaries.">
+ <meta name="twitter:description" content="The background color changes as you scroll through sections. Best for stories.">
<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": "Scroll-Driven Background Color", "name": "Scroll-Driven Background Color", "description": "The page background continuously blends between each section's color as you scroll — interpolation through a palette, not a snap at boundaries.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/scroll-background-color/", "inLanguage": "en", "articleSection": "Scroll-Based", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Scroll-Based, Scroll-Driven Background Color"}</script>
+ <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Scroll-Driven Background Color", "name": "Scroll-Driven Background Color", "description": "The background color changes as you scroll through sections. Best for stories.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/scroll-background-color/", "inLanguage": "en", "articleSection": "Scroll-Based", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Scroll-Based, Scroll-Driven Background Color"}</script>
<style>
- @font-face{font-family:'Bricolage';src:url('../../../assets/fonts/brico-700.woff2') format('woff2');font-weight:700;font-display:swap}
- @font-face{font-family:'Bricolage';src:url('../../../assets/fonts/brico-800.woff2') format('woff2');font-weight:800;font-display:swap}
- @font-face{font-family:'PlexMono';src:url('../../../assets/fonts/plex-400.woff2') format('woff2');font-weight:400;font-display:swap}
- @font-face{font-family:'PlexMono';src:url('../../../assets/fonts/plex-500.woff2') format('woff2');font-weight:500;font-display:swap}
- :root{--bg:#0b0b0d;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-accent:#6ea8ff;--ui-text:#f4f4f2;--ui-muted:#77777e;--stage-h:620px;--disp:'Bricolage','Arial Narrow',sans-serif;--mono:'PlexMono',ui-monospace,Menlo,Consolas,monospace}
+ :root{--ui-accent:#6ea8ff}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
- body{background:var(--bg);color:var(--ui-text);font-family:var(--mono);-webkit-font-smoothing:antialiased;padding:clamp(12px,3vw,24px)}
- header{margin-bottom:20px}
- header h1{font-family:var(--disp);font-weight:700;font-size:clamp(16px,2.2vw,22px);letter-spacing:-.01em}
- header p{font-size:clamp(10px,1.2vw,12px);color:var(--ui-muted);margin-top:4px}
- .layout{display:flex;gap:clamp(12px,2vw,24px);align-items:flex-start}
- .stage{flex:1;min-width:0;height:var(--stage-h);overflow-y:scroll;scrollbar-width:none;border:1px solid var(--ui-border);border-radius:8px;background:#0d1124}
+ .stage{position:relative;overflow-y:auto;scrollbar-width:none;container-type:size;background:#0d1124}
.stage::-webkit-scrollbar{display:none}
- section{min-height:calc(var(--stage-h)*0.9);display:flex;flex-direction:column;justify-content:center;padding:clamp(20px,4vw,48px);background:transparent}
- .num{font-size:clamp(11px,1.4vw,13px);letter-spacing:2px;color:rgba(255,255,255,.55)}
- .stage h2{font-family:var(--disp);font-weight:800;font-size:clamp(24px,4.5vw,44px);letter-spacing:-.02em;margin:10px 0 14px;color:#f4f4f2}
- .stage p{font-size:clamp(11px,1.4vw,13px);line-height:1.7;max-width:44ch;color:rgba(255,255,255,.7)}
- .stage.light h2{color:#16161a}
- .stage.light .num{color:rgba(0,0,0,.5)}
- .stage.light p{color:rgba(0,0,0,.62)}
- aside{width:clamp(200px,22vw,240px);flex-shrink:0;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:8px;padding:16px;display:flex;flex-direction:column;gap:12px}
- .note{font-size:10px;color:var(--ui-muted);line-height:1.6;border-left:2px solid var(--ui-accent);padding-left:8px}
- .lbl{font-size:10px;text-transform:uppercase;letter-spacing:1px;color:var(--ui-accent)}
- .sr{display:flex;flex-direction:column;gap:4px}
- .sr .row2{display:flex;justify-content:space-between;font-size:10px;color:var(--ui-muted)}
- .sr .row2 .v{color:var(--ui-text)}
- input[type=range]{width:100%;min-height:44px;accent-color:var(--ui-accent);cursor:pointer}
- select{background:var(--ui-bg);border:1px solid var(--ui-border);color:var(--ui-text);font-family:var(--mono);font-size:10px;padding:8px 6px;border-radius:4px;width:100%;min-height:44px}
- .tog{display:flex;align-items:center;gap:8px;font-size:11px;cursor:pointer;min-height:44px;user-select:none}
- .tog input{accent-color:var(--ui-accent);width:18px;height:18px;flex-shrink:0}
- .div{height:1px;background:var(--ui-border)}
- .kv{display:flex;justify-content:space-between;align-items:center;font-size:10px;color:var(--ui-muted)}
- .kv .v{color:var(--ui-text);font-variant-numeric:tabular-nums}
- .swatch{display:inline-block;width:16px;height:16px;border-radius:4px;border:1px solid var(--ui-border);vertical-align:middle;margin-right:6px}
- @media(max-width:600px){.layout{flex-direction:column}aside{width:100%}.stage{flex:none;width:100%}:root{--stage-h:480px}}
+ .stage section{min-height:90cqh;display:flex;flex-direction:column;justify-content:center;padding:clamp(20px,4vw,48px)}
+ .num{font-size:13px;font-weight:600;letter-spacing:2px}
+ .stage h3{font-weight:800;font-size:clamp(24px,4.5vw,44px);letter-spacing:-.02em;margin:10px 0 14px}
+ .stage p{font-size:14px;line-height:1.7;max-width:44ch}
+ /* White text on the dark colors, black on the light ones: the script sets .light once the background is light enough */
+ .num,.stage h3,.stage p{color:#fff}
+ .stage.light .num,.stage.light h3,.stage.light p{color:#000}
</style>
-<style id="ah-inject">
-:root{--ah-bg:rgba(17,17,20,.82);--ah-line:rgba(255,255,255,.1);--ah-ink:#f4f4f2;--ah-muted:#9a9aa2;--ah-accent:#6ea8ff}
-body{padding-top:46px}
-.ah-bar{position:fixed;top:0;left:0;right:0;height:46px;z-index:2147483000;display:flex;align-items:center;justify-content:space-between;gap:10px;padding:0 clamp(10px,3vw,18px);background:var(--ah-bg);backdrop-filter:blur(10px);-webkit-backdrop-filter:blur(10px);border-bottom:1px solid var(--ah-line);font-family:ui-monospace,Menlo,Consolas,monospace;font-size:12px;box-sizing:border-box}
-.ah-bar a,.ah-bar button{color:var(--ah-muted);text-decoration:none;background:none;border:0;font:inherit;cursor:pointer;display:inline-flex;align-items:center;gap:6px;min-height:34px;padding:0 7px;border-radius:6px;transition:color .18s,background .18s;white-space:nowrap}
-.ah-bar a:hover,.ah-bar button:hover{color:var(--ah-ink)}
-.ah-home{color:var(--ah-ink);font-weight:600}
-.ah-actions{display:flex;align-items:center;gap:2px}
-.ah-sep{width:1px;height:16px;background:var(--ah-line);margin:0 4px}
-.ah-copied{color:var(--ah-accent)!important}
-@media(max-width:600px){.ah-bar .ah-hideSm{display:none}}
-@media print{.ah-bar{display:none}body{padding-top:0}}
-</style>
+ <link rel="stylesheet" href="../../../assets/css/demo-page.css?v=4">
+ <script src="../../../assets/js/demo-page.js?v=4" defer></script>
</head>
-<body>
-<div class="ah-bar" role="navigation" aria-label="Animation Handbook"><a class="ah-home" href="../../../">← Animation Handbook</a><span class="ah-actions"><a class="ah-hideSm" href="../scrollspy-nav/" rel="prev" title="Previous">‹ Prev</a><span class="ah-sep ah-hideSm"></span><button type="button" id="ah-copy" class="ah-hideSm">Copy source</button><a class="ah-hideSm" href="https://github.com/matinmonshizadeh/animation-handbook/blob/main/animations/01-scroll-based/scroll-background-color/index.html" target="_blank" rel="noopener noreferrer">Source ↗</a></span></div>
-<header>
- <h1>Scroll-Driven Background Color</h1>
- <p>The page background continuously blends between each section's color as you scroll — interpolation through a palette, not a snap at boundaries.</p>
-</header>
-<div class="layout">
- <div class="stage" id="stage"></div>
- <aside>
- <p class="note" id="note">Scroll position maps to a segment of the palette, and the two neighboring colors are interpolated. Toggle "Snap at boundaries" to see the difference between hard cuts and blending — the blend is what sells the effect.</p>
- <div class="lbl">Theme</div>
- <select id="theme" aria-label="Color theme">
- <option value="dusk" selected>Dusk</option>
- <option value="daylight">Daylight</option>
- <option value="forest">Forest</option>
- </select>
- <div class="lbl">Blend window</div>
- <div class="sr">
- <input type="range" id="win" min="0.3" max="1" step="0.05" value="1">
- <div class="row2"><span>of each segment</span><span class="v" id="winVal">1.00</span></div>
+<body class="hb" data-hb-kind="scroll" data-hb-autoplay>
+<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="../scrollspy-nav/" rel="prev" aria-label="Previous: Scrollspy Navigation"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m15 18-6-6 6-6"/></svg><span><span class="hb-dir">Previous: </span>Scrollspy Navigation</span></a></span>
+</nav>
+<main class="hb-page">
+ <header class="hb-head">
+ <p class="hb-cat">01.26 · Scroll-Based</p>
+ <h1>Scroll-Driven Background Color</h1>
+ <p class="hb-lede">The background color changes as you scroll through sections. Best for stories.</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">Scroll it</h2><p>Scroll inside the box, or press Play and it scrolls for you.</p></div>
+ </div>
+ <div class="stage" id="stage" tabindex="0" role="region" aria-label="Sections whose background color blends as you scroll"></div>
+ <div class="hb-player">
+ <button class="hb-play" type="button" id="btn-scroll" data-hb-autoscroll><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 5v14M19 12l-7 7-7-7"/></svg>Play</button>
+ <button class="hb-play" type="button" id="btn-top" data-hb-top><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 19V5M5 12l7-7 7 7"/></svg>Back to top</button>
+ </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, then scroll again or press Play.</p></div>
+ </div>
+ <div class="hb-settings">
+ <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="dusk" class="on" aria-pressed="true">Dusk</button>
+ <button type="button" data-pal="daylight" aria-pressed="false">Daylight</button>
+ <button type="button" data-pal="forest" aria-pressed="false">Forest</button>
+ </div>
+ <p class="hb-hint">On Daylight and Forest the text turns dark on light colors.</p>
+ </div>
+ <div class="hb-setting">
+ <p class="hb-setting-name" id="blend-lbl">Blend</p>
+ <div class="seg" id="blend-seg" role="group" aria-labelledby="blend-lbl">
+ <button type="button" data-blend="1" class="on" aria-pressed="true">Gradual</button>
+ <button type="button" data-blend="0.3" aria-pressed="false">Quick</button>
+ <button type="button" data-blend="cut" aria-pressed="false">Hard cut</button>
+ </div>
+ <p class="hb-hint">Hard cut shows the change with no blending at all.</p>
+ </div>
+ </div>
+ </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>
- <label class="tog"><input type="checkbox" id="snap"> Snap at boundaries</label>
- <div class="div"></div>
- <div class="lbl">Live values</div>
- <div class="kv"><span>Scroll</span><span class="v" id="pctVal">0%</span></div>
- <div class="kv"><span>Segment</span><span class="v" id="segVal">1 → 2</span></div>
- <div class="kv"><span>Background</span><span class="v"><span class="swatch" id="swatch"></span><span id="hexVal">#0d1124</span></span></div>
- </aside>
-</div>
+ <div class="hb-card">
+ <p class="hb-prompt">Add a scroll-driven background color to [the sections of your page]. Give each section a color, and as the visitor scrolls, change the page background from one section's color to the next in step with their position, blending between them unless the settings ask for a hard cut, so the color follows the scroll both ways. Blend in a perceptual color space so the colors in between stay clean. Check the text against the background at every point, and switch the text to dark when the background turns light. A change of color is not movement, so keep it when the visitor has reduced motion turned on. 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>
+ </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>Stories</li><li>Portfolios</li><li>Case studies</li></ul>
+ <h3>Avoid on</h3>
+ <ul class="hb-tags hb-avoid"><li>Text-heavy pages</li><li>Forms</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="#top">Back to top ↑</a></footer>
<script>
- const stage=document.getElementById('stage'),note=document.getElementById('note');
- const pctVal=document.getElementById('pctVal'),segVal=document.getElementById('segVal');
- const swatch=document.getElementById('swatch'),hexVal=document.getElementById('hexVal');
+ const stage=document.getElementById('stage');
const clamp=(v,a,b)=>Math.min(Math.max(v,a),b);
const THEMES={
@@ -113,28 +135,29 @@ body{padding-top:46px}
};
const CHAPTERS=[
['Departure','The first section owns the darkest color.','Scroll down and the ground shifts under the text.'],
- ['Ascent','Halfway between two sections the background is','a literal mix of both palette entries.'],
- ['Meridian','On light palettes the ink flips automatically —','luminance is computed, never assumed.'],
- ['Descent','A narrower blend window holds each color steady','near section centers and blends only at edges.'],
- ['Arrival','Snapping at boundaries shows what the effect','looks like without interpolation. Jarring.']
+ ['Ascent','Halfway between two sections, the background','is an even mix of both colors.'],
+ ['Meridian','When the background turns light,','the text turns dark so it stays readable.'],
+ ['Descent','Each section has a color of its own,','and the change happens as you move between them.'],
+ ['Arrival','The last color stays','as you reach the end.']
];
stage.innerHTML=CHAPTERS.map((c,i)=>
- `<section><div class="num">CHAPTER ${String(i+1).padStart(2,'0')}</div><h2>${c[0]}</h2><p>${c[1]}<br>${c[2]}</p></section>`).join('');
+ `<section><div class="num">CHAPTER ${String(i+1).padStart(2,'0')}</div><h3>${c[0]}</h3><p>${c[1]}<br>${c[2]}</p></section>`).join('');
let PAL=THEMES.dusk,WIN=1,SNAP=false;
let maxScroll=1,lastBg='',isLight=false,ticking=false;
// Layout reads live here only — never in the scroll handler.
- function measure(){maxScroll=Math.max(1,stage.scrollHeight-stage.clientHeight);}
+ function measure(){maxScroll=Math.max(1,stage.scrollHeight-stage.clientHeight)}
- const hex=n=>n.toString(16).padStart(2,'0');
+ // Relative luminance of the blended color: white and black text read equally well at 0.18, so the text flips there
+ const lin=c=>(c/=255)<=0.03928?c/12.92:((c+0.055)/1.055)**2.4;
function update(){
const p=clamp(stage.scrollTop/maxScroll,0,1);
const seg=p*(PAL.length-1);
const i=Math.min(Math.floor(seg),PAL.length-2);
let t=seg-i;
- if(SNAP){t=t<.5?0:1;}
+ if(SNAP){t=t<.5?0:1}
else{
t=clamp((t-(1-WIN)/2)/WIN,0,1); // remap into the blend window around the boundary
t=t*t*(3-2*t); // smoothstep
@@ -147,36 +170,34 @@ body{padding-top:46px}
if(css!==lastBg){ // guard: skip identical writes
lastBg=css;
stage.style.background=css;
- swatch.style.background=css;
- hexVal.textContent='#'+hex(r)+hex(g)+hex(bl);
- const lum=(0.2126*r+0.7152*g+0.0722*bl)/255;
- const light=lum>0.55;
- if(light!==isLight){isLight=light;stage.classList.toggle('light',light);}
+ const lum=0.2126*lin(r)+0.7152*lin(g)+0.0722*lin(bl);
+ const light=lum>0.18;
+ if(light!==isLight){isLight=light;stage.classList.toggle('light',light)}
}
- pctVal.textContent=Math.round(p*100)+'%';
- segVal.textContent=(i+1)+' → '+(i+2);
}
stage.addEventListener('scroll',()=>{
if(ticking)return; // rAF-coalesced: one update per frame
ticking=true;
- requestAnimationFrame(()=>{ticking=false;update();});
+ requestAnimationFrame(()=>{ticking=false;update()});
},{passive:true});
- const winInp=document.getElementById('win'),winVal=document.getElementById('winVal');
- winInp.addEventListener('input',()=>{WIN=parseFloat(winInp.value);winVal.textContent=WIN.toFixed(2);update();});
- document.getElementById('snap').addEventListener('change',e=>{SNAP=e.target.checked;update();});
- document.getElementById('theme').addEventListener('change',e=>{PAL=THEMES[e.target.value];update();});
-
- // Color change is not vestibular motion — the blend stays on under reduced
- // motion, deliberately. There are no transform-based decorations to disable.
- if(window.matchMedia('(prefers-reduced-motion: reduce)').matches){
- note.textContent='Reduced motion is on. The color blend is kept: it tracks scroll position directly and involves no movement, parallax, or transforms — background color is not vestibular motion.';
+ 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);
+ lastBg='';update(); // a new palette or blend is drawn at once, where the box is
+ }));
}
+ choices('pal-seg',d=>{PAL=THEMES[d.pal]});
+ choices('blend-seg',d=>{SNAP=d.blend==='cut';if(!SNAP)WIN=+d.blend});
- let rt;window.addEventListener('resize',()=>{clearTimeout(rt);rt=setTimeout(()=>{measure();update();},150);});
+ // The box changes size with the window, and the site font can change the height of a section: measure again and draw at once
+ function remeasure(){measure();update()}
+ window.addEventListener('resize',remeasure);
measure();update();
+ if(document.fonts&&document.fonts.ready)document.fonts.ready.then(remeasure);
</script>
-<script>(function(){var b=document.getElementById("ah-copy");if(!b)return;b.addEventListener("click",function(){fetch(location.href).then(function(r){return r.text();}).catch(function(){return document.documentElement.outerHTML;}).then(function(t){return navigator.clipboard.writeText(t);}).then(function(){b.textContent="Copied!";b.classList.add("ah-copied");setTimeout(function(){b.textContent="Copy source";b.classList.remove("ah-copied");},1500);}).catch(function(){b.textContent="Copy failed";setTimeout(function(){b.textContent="Copy source";},1500);});});})();</script>
</body>
</html>
diff --git a/index.html b/index.html
index e933164b..45e9775d 100644
--- a/index.html
+++ b/index.html
@@ -904,7 +904,7 @@ const CATS=[
['scroll-velocity-skew','Scroll Velocity Skew','Content shears with scroll speed and springs straight when you stop — motion mapped to velocity, not position.'],
['svg-line-draw','SVG Line Draw on Scroll','A winding route draws itself down the page via stroke-dashoffset, passing waypoints as you travel.'],
['scrollspy-nav','Scrollspy Navigation','A menu highlights the section you are reading. Best for long docs.'],
- ['scroll-background-color','Scroll-Driven Background Color','The background blends between section palettes as you scroll, flipping text contrast on light stops.'],
+ ['scroll-background-color','Scroll-Driven Background Color','The background color changes as you scroll through sections. Best for stories.'],
]
},
{