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>Flash / Light Leak Transition — Animation Handbook</title>
<meta name="description" content="A burst of light hides the moment the page changes. Best for bold, lively sites.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/03-page-transitions/flash-transition/">
<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="Flash / Light Leak Transition — Animation Handbook">
<meta property="og:description" content="A burst of light hides the moment the page changes. Best for bold, lively sites.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/03-page-transitions/flash-transition/">
<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="Flash / Light Leak Transition — Animation Handbook">
<meta name="twitter:description" content="A burst of light hides the moment the page changes. Best for bold, lively sites.">
<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": "Flash / Light Leak Transition", "name": "Flash / Light Leak Transition", "description": "A burst of light hides the moment the page changes. Best for bold, lively sites.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/03-page-transitions/flash-transition/", "inLanguage": "en", "articleSection": "Page Transitions", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Page Transitions, Flash / Light Leak Transition"}</script>
<style>
:root{--bg:#0b0b0d;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-accent:#b98cff;--ui-text:#f4f4f2;--ui-muted:#8a8a92;--flash-color:#ffffff}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
.stage{position:relative;display:flex;flex-direction:column;overflow:hidden;background:var(--bg);color:var(--ui-text)}
.mini-nav{display:flex;gap:6px;padding:10px 12px;background:var(--ui-bg);border-bottom:1px solid var(--ui-border);flex-shrink:0}
.nav-btn{background:none;border:1px solid var(--ui-border);color:var(--ui-muted);font:inherit;font-size:13px;padding:0 12px;border-radius:4px;cursor:pointer;min-height:44px}
.nav-btn.active{border-color:var(--ui-accent);color:var(--ui-accent)}
@media(hover:hover){.nav-btn:hover{border-color:var(--ui-accent)}}
.page-area{flex:1;position:relative;overflow:hidden}
.page{position:absolute;inset:0;pointer-events:none;visibility:hidden}
.page.active{pointer-events:auto;visibility:visible}
/* Flash overlay */
#flash-overlay{position:absolute;inset:0;background:var(--flash-color);opacity:0;pointer-events:none;z-index:10;filter:blur(0)}
#flash-overlay.blur-on{filter:blur(8px)}
/* page content */
.pc{display:flex;flex-direction:column;justify-content:center;height:100%;padding:clamp(20px,4vw,40px);gap:14px}
.pe{font-size:10px;text-transform:uppercase;letter-spacing:3px;opacity:.6}
.ph{font-size:clamp(22px,3.5vw,40px);font-weight:bold;line-height:1.1}
.pp{font-size:13px;opacity:.55;line-height:1.7;max-width:340px}
.pgrid{display:grid;grid-template-columns:repeat(3,1fr);gap:6px;margin-top:4px}
.gi{background:rgba(255,255,255,.07);border:1px solid rgba(255,255,255,.1);border-radius:6px;padding:18px 12px;font-size:11px}
.pstat{display:flex;gap:20px;margin-top:4px}
.pstat b{display:block;font-size:clamp(18px,2.5vw,24px);color:var(--ui-accent)}
.pstat span{font-size:9px;opacity:.6;text-transform:uppercase;letter-spacing:1px}
/* Short windows leave the page area under 220px: tighter spacing keeps the tallest page inside it with room to spare */
@media(max-height:640px){.pc{gap:8px}.gi{padding:12px}.pp{line-height:1.5}.pgrid,.pstat{margin-top:0}}
@media(prefers-reduced-motion:reduce){#flash-overlay{display:none}}
/* The small site: its nav row over its page area. In Compare a copy of it, the twin, sits beside it. */
.box{display:flex;flex-direction:column;flex:1;min-height:0;min-width:0}
.twin,.strip,.side{display:none}
.twin .nav-btn{display:inline-flex;align-items:center}
.lbl{padding:2px 6px;border:1px solid rgba(255,255,255,.18);border-radius:5px;background:#111114;color:#f4f4f2;font:600 12px/1.25 var(--hb-font);white-space:nowrap}
/* See inside: the flash is never filled. The real overlay becomes a thick border in the flash color (the page's own fades
brighten and dim it, its blur makes a glow); under the page a strip draws the flash's strength over time as a hill. */
.stage[data-hb-mode="inside"] #flash-overlay,.stage[data-hb-mode="compare"].posed #flash-overlay{display:block;background:none;border:clamp(6px,2vw,10px) solid var(--flash-color)}
.stage[data-hb-mode="inside"] .strip{display:block}
.stage[data-hb-mode="inside"] .pc{gap:8px}.stage[data-hb-mode="inside"] .gi{padding:12px}
.strip{position:relative;flex:none;height:90px;overflow:hidden;background:#111114;border-top:1px solid var(--ui-border)}
.hill{position:absolute;inset:0;width:100%;height:100%}
.hill path,.hill line{fill:none;stroke:#8a8a92}
.hill #h-fill{fill:var(--flash-color);fill-opacity:.2;stroke:none}
.hill #h-line{stroke:var(--flash-color);stroke-width:2}
.hill #h-base{stroke:#3a3a42}
.hill #h-full,.hill #h-peak{stroke-dasharray:4 4}
.strip .cap{position:absolute;left:0;top:0}
.strip #c-peak{max-width:calc(100% - 16px);white-space:normal}
.dot{position:absolute;left:0;top:0;width:12px;height:12px;margin:-6px 0 0 -6px;border-radius:50%;background:var(--flash-color);box-shadow:0 0 0 2px #0b0b0d,0 0 0 3px #8a8a92}
/* Compare: the copy beside the real site, whose flash stands at its peak as an outline at rest. The names sit above its glow. */
.stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:2px;background:#2a2a30}
.stage[data-hb-mode="compare"] .twin{display:flex}
.stage[data-hb-mode="compare"] .side{display:block}
.side{position:absolute;bottom:20px;z-index:11}
.side.first{left:10px}.side.second{left:calc(50% + 10px)}
@media(max-width:440px){.strip{height:110px}}
@media(max-width:600px){
.stage[data-hb-mode="compare"] .mini-nav{gap:4px;padding:6px}
.stage[data-hb-mode="compare"] .nav-btn{padding:0 6px;font-size:12px;min-width:44px;justify-content:center}
.stage[data-hb-mode="compare"] .pc{padding:16px;gap:6px}.stage[data-hb-mode="compare"] .ph{font-size:16px}
}
/* Phones and short windows leave the page area short: the page keeps only its eyebrow and heading */
@media(max-width:600px),(max-height:789px){
.stage[data-hb-mode="inside"] .pp,.stage[data-hb-mode="inside"] .pgrid,.stage[data-hb-mode="inside"] .pstat{display:none}
.stage[data-hb-mode="compare"] .pp,.stage[data-hb-mode="compare"] .pgrid,.stage[data-hb-mode="compare"] .pstat{display:none}
}
/* From 360px down the halves stack; a stacked half is short, so its page text starts at the top, clear of its name */
@media(max-width:360px){
.stage[data-hb-mode="compare"]{grid-template-columns:minmax(0,1fr);grid-template-rows:repeat(2,minmax(0,1fr))}
.stage[data-hb-mode="compare"] .side.first{bottom:calc(50% + 10px)}
.stage[data-hb-mode="compare"] .side.second{left:10px;bottom:10px}
.stage[data-hb-mode="compare"] .pc{justify-content:flex-start;padding-top:12px}
}
</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="do" 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="../zoom-transition/" rel="prev" aria-label="Previous: Zoom Transition"><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>Zoom Transition</span></a><a href="../blur-transition/" rel="next" aria-label="Next: Blur Transition"><span><span class="hb-dir">Next: </span>Blur Transition</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">03.07 · Page Transitions</p>
<h1>Flash / Light Leak Transition</h1>
<p class="hb-lede">A burst of light hides the moment the page changes. Best for bold, lively sites.</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">Click it</h2><p>Click Work or About at the top of the box, or press Show me.</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">
<div class="box" id="box">
<nav class="mini-nav" id="nav" aria-label="Demo pages"></nav>
<div class="page-area" id="page-area">
<div id="flash-overlay"></div>
</div>
</div>
<div class="strip" id="strip" aria-hidden="true">
<svg class="hill"><path id="h-fill"/><path id="h-line"/><line id="h-base"/><line id="h-full"/><line id="h-peak"/><line id="h-lr"/><line id="h-lf"/></svg>
<span class="lbl cap" id="c-rise" data-hb-caption>Flash rises</span>
<span class="lbl cap" id="c-peak" data-hb-caption>Page switches at the peak, hidden</span>
<span class="lbl cap" id="c-fall" data-hb-caption>Fades a little slower</span>
<span class="lbl cap" id="c-full" data-hb-caption>Fully hidden at this line</span>
<span class="dot" id="dot"></span>
</div>
<p class="lbl side first" aria-hidden="true" data-hb-caption>Hard cut</p>
<p class="lbl side second" aria-hidden="true" data-hb-caption>With a flash</p>
</div>
<div class="hb-player">
<button class="hb-play" type="button" id="btn-demo" data-hb-demo><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z"/></svg>Show me</button>
<button class="hb-play" type="button" id="btn-reset" data-hb-reset><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/></svg>Reset</button>
<label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo autocomplete="off"><span>Slow motion</span></label>
</div>
<div class="hb-mode-notes" aria-live="polite">
<p data-hb-for="inside" hidden>Inside, the flash is drawn as an outline so the page stays in view: it switches at the brightest moment, marked on the strip below. Click Work or About, or press Show me.</p>
<p data-hb-for="compare" hidden>Hard cut switches the page with a visible jolt; With a flash switches it at the peak of a burst of light. Click Work or About, or press Show me.</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, then try it again or press Show me.</p></div>
</div>
<div class="hb-settings">
<div class="hb-setting">
<p class="hb-setting-name" id="color-lbl">Flash color</p>
<div class="swatches" id="color-sw" role="group" aria-labelledby="color-lbl">
<button type="button" class="on" aria-pressed="true" aria-label="White" data-c="#ffffff" style="background:#ffffff"></button>
<button type="button" aria-pressed="false" aria-label="Gold" data-c="#f5d78e" style="background:#f5d78e"></button>
<button type="button" aria-pressed="false" aria-label="Cyan" data-c="#7ae8f0" style="background:#7ae8f0"></button>
<button type="button" aria-pressed="false" aria-label="Red" data-c="#f07a7a" style="background:#f07a7a"></button>
</div>
<p class="hb-hint">White looks like a camera flash; colors, a light leak.</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-in="320" data-out="510" aria-pressed="false">Slow</button>
<button type="button" data-in="200" data-out="320" class="on" aria-pressed="true">Normal</button>
<button type="button" data-in="120" data-out="190" aria-pressed="false">Fast</button>
</div>
<p class="hb-hint">How fast the flash rises and fades.</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">
<p class="hb-setting-name" id="strength-lbl">Flash strength</p>
<div class="seg" id="strength-seg" role="group" aria-labelledby="strength-lbl">
<button type="button" data-v="1" class="on" aria-pressed="true">Full</button>
<button type="button" data-v="0.75" aria-pressed="false">Strong</button>
<button type="button" data-v="0.5" aria-pressed="false">Soft</button>
</div>
<p class="hb-hint">Below full, the page change shows through.</p>
</div>
<div class="hb-setting">
<label class="hb-switch-row"><span>Blurs the flash</span><input class="hb-switch" type="checkbox" role="switch" id="blur-tog" autocomplete="off"></label>
<p class="hb-hint">Softens the flash's edges into a glow.</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 a flash transition to [the pages or scenes you want to switch between]. Cover the content with a full-size colored layer that rises to its peak, swap the page while it is covered, then fade the layer away to reveal the new page, like a burst of light hiding a cut in a film. At full strength the change is completely hidden. Let the fade-out take a little longer than the rise. Keep flashes rare, never several in one second, to protect people who are sensitive to flashing light. If the visitor has reduced motion turned on, switch pages with no flash. 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>Product launches</li><li>Music and fashion</li><li>Photo stories</li></ul>
<h3>Avoid on</h3>
<ul class="hb-tags hb-avoid"><li>Frequent page changes</li><li>Calm sites</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 PAGES=[
{name:'Home',bg:'#0a0f18',html:`<div class="pc"><div class="pe">Creative Studio</div><p class="ph">Design without compromise.</p><p class="pp">We craft digital products that earn attention and keep it. Every pixel considered.</p></div>`},
{name:'Work',bg:'#0a180a',html:`<div class="pc"><div class="pe">Selected Work</div><p class="ph">The Portfolio</p><div class="pgrid">${['Atlas','Orbit','Prism','Lumen','Frame','Echo'].map(n=>`<div class="gi">${n}</div>`).join('')}</div></div>`},
{name:'About',bg:'#150a18',html:`<div class="pc"><div class="pe">Our Story</div><p class="ph">Built on conviction.</p><p class="pp">18 people who care unreasonably about craft. Independent since 2016.</p><div class="pstat"><div><b>140+</b><span>Projects</span></div><div><b>9</b><span>Years</span></div><div><b>3</b><span>Continents</span></div></div></div>`},
];
const $=id=>document.getElementById(id);
const box=$('box'),stage=box.parentElement,nav=$('nav'),area=$('page-area'),flashEl=$('flash-overlay');
const blurTog=$('blur-tog'),slowTog=$('slow-tog');
const motionOk=!window.matchMedia('(prefers-reduced-motion:reduce)').matches;
// No flash starts less than this many ms after the last one began, whatever the visitor presses (page names, Show me, Reset):
// never more than two flashes a second. A change asked for sooner waits, with the overlay clear, until then.
const MIN_CYCLE=500;
let mode='normal',ringOn=false,flashAt=0,runScale=1,raf=0,G=null,dotT=0,dotOp=0;
let home=-1,current=0,animating=false,flashIn=200,flashOut=320,intensity=1,lastRise=-MIN_CYCLE,afterMove=null,wayBack=0,timers=[];
const pages=[],navBtns=[];
PAGES.forEach((p,i)=>{
const d=document.createElement('div');d.className='page';d.style.background=p.bg;d.innerHTML=p.html;area.insertBefore(d,flashEl);pages.push(d);
const b=document.createElement('button');b.type='button';b.className='nav-btn';b.textContent=p.name;
b.addEventListener('click',()=>navigate(i));nav.appendChild(b);navBtns.push(b);
});
// Compare's first half: a copy of the small site (no ids, no buttons, inert) that cuts to the next page when the real one switches
const twin=document.createElement('div');
twin.className='box twin';twin.inert=true;twin.setAttribute('aria-hidden','true');
twin.innerHTML=`<div class="mini-nav">${PAGES.map(p=>`<span class="nav-btn">${p.name}</span>`).join('')}</div>`+
`<div class="page-area">${PAGES.map(p=>`<div class="page" style="background:${p.bg}">${p.html}</div>`).join('')}</div>`;
box.before(twin);
const twinPages=[...twin.querySelectorAll('.page')],twinNav=[...twin.querySelectorAll('.nav-btn')];
function showTwin(n){twinPages.forEach((el,i)=>el.classList.toggle('active',i===n));twinNav.forEach((b,i)=>b.classList.toggle('active',i===n))}
// Under reduced motion both views are still pictures, and their notes say so
if(!motionOk){
document.querySelector('[data-hb-for="inside"]').textContent='Shown still, with the flash as an outline: with motion on, the page switches at the brightest moment on the strip below, then the flash fades a little slower. Here it switches at once.';
document.querySelector('[data-hb-for="compare"]').textContent='Shown still: Hard cut simply switches page; With a flash is at its brightest moment, its light drawn as an outline.';
}
// Drops a timer's id once it fires, so `timers` only holds the ones still waiting.
function later(fn,wait){
const id=setTimeout(()=>{
const i=timers.indexOf(id);
if(i>-1)timers.splice(i,1);
fn();
},wait);
timers.push(id);
return id;
}
function cancel(id){
clearTimeout(id);
const i=timers.indexOf(id);
if(i>-1)timers.splice(i,1);
}
function updateNav(n){navBtns.forEach((b,i)=>{b.classList.toggle('active',i===n);b.setAttribute('aria-current',i===n?'page':'false')})}
// The resting state for page n, at once. A hidden page is also out of the Tab order and the accessibility tree (visibility).
function showPage(n){
pages.forEach((el,i)=>el.classList.toggle('active',i===n));
updateNav(n);
if(mode==='compare')showTwin(n);
}
// The overlay back to nothing at once: no transition, no opacity, no blur, and no fade left running. The reflow
// commits this before a new flash starts, so a flash begun in the same task rises from nothing.
function clearFlash(){
ringOn=false;
flashEl.style.transition='none';
flashEl.style.opacity='0';
flashEl.classList.remove('blur-on');
flashEl.getAnimations().forEach(a=>a.cancel());
void flashEl.offsetWidth;
}
// Ends a flash: the demo is idle again, and a Show me run goes on to its next step
function finish(){
animating=false;flashAt=0;
const then=afterMove;afterMove=null;
if(then)then();
pose();
}
// Stops everything at once and shows the resting state for `current`
function settle(){
timers.forEach(clearTimeout);timers=[];wayBack=0;afterMove=null;
animating=false;flashAt=0;cancelAnimationFrame(raf);
clearFlash();
showPage(current);
}
function navigate(next,then){
if(next===current||animating)return;
const prev=current;current=next;updateNav(next);
afterMove=then||null;
if(!motionOk){showPage(next);finish();return}
animating=true;
// A mode's pose goes the moment a change starts; the dot waits at the start of the hill
pose();if(mode==='inside')placeDot(0,0);
// settle() cancels a wait, and the next change measures from the same lastRise, so no input can start two rises too close
// together
const wait=MIN_CYCLE-(performance.now()-lastRise);
if(wait>0)later(()=>flash(prev,next),wait);else flash(prev,next);
}
function flash(prev,next){
lastRise=flashAt=performance.now();
const scale=runScale=slowTog.checked?3:1;
if(mode==='inside')ride();
doTransition(prev,next,flashIn*scale,flashOut*scale);
}
// flashIn and flashOut are in ms (three times as long in slow motion), read when the flash starts
function doTransition(prev,next,flashIn,flashOut){
flashEl.style.transition=`opacity ${flashIn}ms ease-in, filter ${flashIn}ms ease`;
flashEl.classList.toggle('blur-on',blurTog.checked);
flashEl.style.opacity=intensity;
later(()=>{
// Peak: swap behind the flash and fade it out in the same step
pages[prev].classList.remove('active');
pages[next].classList.add('active');
if(mode==='compare')showTwin(next);
flashEl.style.transition=`opacity ${flashOut}ms ease-out, filter ${flashOut}ms ease`;
flashEl.style.opacity='0';flashEl.classList.remove('blur-on');
later(finish,flashOut);
},flashIn);
}
// Show me: one visit to the next page, a hold of 1200ms, and the way back to where the run started (pressed again mid-run, from there)
function showMe(){
const start=home<0?current:home;
home=start;current=start;settle();
navigate((start+1)%PAGES.length,()=>{wayBack=later(()=>{wayBack=0;home=-1;navigate(start)},1200)});
}
function reset(){current=0;home=-1;settle();pose()}
document.getElementById('btn-demo').addEventListener('click',showMe);
document.getElementById('btn-reset').addEventListener('click',reset);
// The visitor took over: cancel what the run has not started yet. A flash already under way finishes on its own.
document.addEventListener('hb:input',()=>{afterMove=null;cancel(wayBack);wayBack=0;home=-1;pose()});
// See inside's strip: the flash's strength over time from the page's own numbers, the overlay's two eases drawn as the curves they
// are over flashIn and flashOut; the strip stands for 1.2 s (three times that in slow motion).
const strip=$('strip'),dot=$('dot'),CAP={rise:$('c-rise'),peak:$('c-peak'),fall:$('c-fall'),full:$('c-full')};
const SPAN=1200,PHONE=matchMedia('(max-width:600px),(max-height:650px)');
const bez=(a,b,c,d,s)=>{const u=1-s;return u*u*u*a+3*u*u*s*b+3*u*s*s*c+s*s*s*d};
const seg=(id,x1,y1,x2,y2)=>{const l=$(id);l.setAttribute('x1',x1);l.setAttribute('y1',y1);l.setAttribute('x2',x2);l.setAttribute('y2',y2)};
const at=(el,x,y)=>{el.style.left=x.toFixed(1)+'px';el.style.top=y.toFixed(1)+'px'};
function layout(){
const W=strip.clientWidth,Hs=strip.clientHeight;
if(!W||mode!=='inside')return;
CAP.peak.textContent=intensity===1?'Page switches at the peak, hidden':'Page switches at the peak: it shows through';
CAP.full.hidden=PHONE.matches;
Object.values(CAP).forEach(c=>{c.style.left=c.style.top='0px'}); // measured from the strip's corner, not from the last place
const h=CAP.rise.offsetHeight,lane=Math.max(h,CAP.peak.offsetHeight),edge=8,base=Hs-8,fwid=CAP.fall.offsetWidth;
const x0=edge+CAP.rise.offsetWidth+12,xe=W-edge,Wg=xe-x0;
const rw=flashIn/SPAN*Wg,fw=flashOut/SPAN*Wg,x1=x0+rw,x2=x1+fw;
// The curves and lines for one or two rows of labels above the strength line (a second row only where the hill leaves no room)
const geom=rows=>{
const fullY=4+lane+9+(rows-1)*(h+4),H=base-fullY,top=base-intensity*H;
return{fullY,H,top,rise:t=>[bez(x0,x0+.42*rw,x1,x1,t),bez(base,base,top,top,t)],fall:t=>[bez(x1,x1,x1+.58*fw,x2,t),bez(top,top,base,base,t)]};
};
// "Fades a little slower" above the falling side, nearest the peak where the curve has dropped well clear of it
let g=geom(1),spot=null;
const clear=(l,b)=>{for(let q=0;q<=1;q+=.05){const [px,py]=g.fall(q);if(px>l-4&&px<l+fwid+4&&py<b+8)return false}return true};
for(let t=.3;t<=1.001&&!spot;t+=.04){
const [ax,ay]=g.fall(t),left=Math.min(ax+8,xe-fwid),bottom=ay-9;
if(bottom-h>=g.fullY+3&&clear(left,bottom))spot=[ax,ay,left,bottom-h,left,bottom];
}
if(!spot){
g=geom(2);
const [ax,ay]=g.fall(.5),left=xe-fwid;
spot=[ax,ay,left,4+lane+4,Math.max(left,Math.min(ax,xe)),4+lane+4+h];
}
const {fullY,H,top,rise}=g;
G={x0,Wg,base,H};
const d=`M${x0} ${base}C${x0+.42*rw} ${base} ${x1} ${top} ${x1} ${top}C${x1} ${top} ${x1+.58*fw} ${base} ${x2} ${base}`;
$('h-line').setAttribute('d',d);$('h-fill').setAttribute('d',d+'Z');
seg('h-base',x0-4,base,xe,base);seg('h-full',x0,fullY,xe,fullY);seg('h-peak',x1,base,x1,4+lane);
// The peak's label above its line, "Flash rises" left of the hill and level with a point on the rising side
const pw=CAP.peak.offsetWidth,pl=Math.max(edge,Math.min(x1-pw/2,W-edge-pw));
at(CAP.peak,pl,4);
const [rx,ry]=rise(.6),rt=Math.max(fullY+3,Math.min(ry-h/2,Hs-2-h));
at(CAP.rise,edge,rt);seg('h-lr',edge+CAP.rise.offsetWidth,rt+h/2,rx,ry);
at(CAP.fall,spot[2],spot[3]);seg('h-lf',spot[0],spot[1],spot[4],spot[5]);
if(!CAP.full.hidden){
const fl=W-edge-CAP.full.offsetWidth;
if(pl+pw+6>fl)CAP.full.hidden=true;else at(CAP.full,fl,4);
}
placeDot(dotT,dotOp);
}
function placeDot(t,op){
dotT=t;dotOp=op;
if(!G)return;
dot.style.transform=`translate(${(G.x0+Math.min(t,SPAN)/SPAN*G.Wg).toFixed(1)}px,${(G.base-op*G.H).toFixed(1)}px)`;
}
// While a flash runs the dot rides the hill: across by the clock, up and down by the strength the overlay really has
function ride(){
cancelAnimationFrame(raf);
const step=()=>{
if(!flashAt)return;
const t=(performance.now()-flashAt)/runScale;
placeDot(t,+getComputedStyle(flashEl).opacity);
raf=flashAt&&mode==='inside'&&t<flashIn+flashOut?requestAnimationFrame(step):0;
};
step();
}
// At rest in a mode the flash stands at its peak as an outline (the overlay at the chosen strength, never filled), the dot at the
// peak. A change lifts the pose; under reduced motion it stays.
function pose(){
const on=mode!=='normal'&&(!motionOk||!animating&&!wayBack);
stage.classList.toggle('posed',on);
if(!on){if(ringOn)clearFlash();return}
ringOn=true;cancelAnimationFrame(raf);
flashEl.style.transition='none';flashEl.style.opacity=intensity;flashEl.classList.toggle('blur-on',blurTog.checked);
placeDot(flashIn,intensity);
}
// The mode switch: draws the mode at once. The shared script then presses Show me (not under reduced motion), which plays it.
document.addEventListener('hb:mode',e=>{
mode=e.detail.mode;
if(mode==='compare')showTwin(pages.findIndex(el=>el.classList.contains('active')));
layout();
if(mode==='inside'&&animating){if(flashAt)ride();else placeDot(0,0)}
pose();
});
const redraw=()=>{layout();pose()};
addEventListener('resize',redraw);document.fonts.ready.then(redraw);
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);
redraw();
}));
}
choices('color-sw',d=>{document.documentElement.style.setProperty('--flash-color',d.c)});
choices('speed-seg',d=>{flashIn=+d.in;flashOut=+d.out});
choices('strength-seg',d=>{intensity=+d.v});
blurTog.addEventListener('change',pose);
showPage(0);
</script>
</body>
</html>