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>Glitch Text — Animation Handbook</title>
<meta name="description" content="Text tears into red and cyan strips like a broken signal. Best for bold titles.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/glitch-text/">
<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="Glitch Text — Animation Handbook">
<meta property="og:description" content="Text tears into red and cyan strips like a broken signal. Best for bold titles.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/glitch-text/">
<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="Glitch Text — Animation Handbook">
<meta name="twitter:description" content="Text tears into red and cyan strips like a broken signal. Best for bold titles.">
<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": "Glitch Text", "name": "Glitch Text", "description": "Text tears into red and cyan strips like a broken signal. Best for bold titles.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/glitch-text/", "inLanguage": "en", "articleSection": "Text & Typography", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Text & Typography, Glitch Text"}</script>
<style>
:root{--ui-accent:#ff6f8b;--offset:4px;--speed:2.4s;--pill:rgba(8,8,10,.9);--ink:#f4f4f2}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
.stage{display:flex;align-items:center;justify-content:center;background:radial-gradient(circle at 50% 40%,#141018,#04060c);padding:24px;overflow:hidden}
/* Glitch text: two colored copies via ::before/::after clones, sliced by an animated clip-path */
.glitch{
position:relative;font-weight:800;
font-size:clamp(28px,min(12vw,120vw/var(--n,6)),120px);line-height:1;letter-spacing:-.02em;color:#fff;
text-transform:uppercase;white-space:nowrap;-webkit-user-select:none;user-select:none;
}
.glitch::before,.glitch::after{
content:attr(data-text);position:absolute;inset:0;
clip-path:inset(0 0 0 0);
}
.glitch::before{color:#ff2d5e;text-shadow:calc(var(--offset)*-1) 0 #ff2d5e;left:calc(var(--offset)*-1);
animation:slice var(--speed) infinite linear alternate-reverse}
.glitch::after{color:#2dffe6;text-shadow:var(--offset) 0 #2dffe6;left:var(--offset);
animation:slice2 var(--speed) infinite linear alternate-reverse}
.glitch::before,.glitch::after{animation-delay:var(--phase,0s)}
/* JS reshuffles the slice phase by restarting the clones with a random negative delay (--phase) */
@keyframes slice{
0%{clip-path:inset(20% 0 60% 0)}20%{clip-path:inset(70% 0 5% 0)}
40%{clip-path:inset(10% 0 80% 0)}60%{clip-path:inset(45% 0 35% 0)}
80%{clip-path:inset(85% 0 2% 0)}100%{clip-path:inset(30% 0 55% 0)}
}
@keyframes slice2{
0%{clip-path:inset(65% 0 10% 0)}20%{clip-path:inset(15% 0 70% 0)}
40%{clip-path:inset(80% 0 8% 0)}60%{clip-path:inset(30% 0 45% 0)}
80%{clip-path:inset(5% 0 85% 0)}100%{clip-path:inset(50% 0 30% 0)}
}
/* Glitches only on hover: the copies hold still until the stage is hovered, or tapped active on touch */
.hover-only .glitch::before,.hover-only .glitch::after{animation-play-state:paused}
@media(hover:hover){.hover-only:hover .glitch::before,.hover-only:hover .glitch::after{animation-play-state:running}}
.hover-only.active .glitch::before,.hover-only.active .glitch::after{animation-play-state:running}
/* toggling this class restarts the pseudo-element animations (reshuffles phase) */
.glitch.reset::before,.glitch.reset::after{animation:none}
/* Shown only in See inside and Compare. The word's box wraps it and takes no part in Normal's layout. */
.real{display:contents}
.steady,.lbl,.slot,.gh{display:none}
.sw{font-weight:800;line-height:1;letter-spacing:-.02em;color:#fff;text-transform:uppercase;white-space:nowrap;user-select:none}
.lbl{padding:3px 8px;border-radius:6px;background:var(--pill);color:var(--ink);font:600 13px/1.25 var(--hb-font);text-align:center;text-wrap:balance}
/* The faint whole copies sit inside the word (so the shake moves them) exactly where the colored strips are; each carries the
dashed line at the word's left edge for its row */
.gh{position:absolute;inset:0;font-style:normal;pointer-events:none}
.gh.gr{--gc:rgba(255,45,94,.22);color:var(--gc);text-shadow:calc(var(--offset)*-1) 0 var(--gc);left:calc(var(--offset)*-1);translate:0 calc(var(--up,0px)*-1)}
.gh.gc{--gc:rgba(45,255,230,.22);color:var(--gc);text-shadow:var(--offset) 0 var(--gc);left:var(--offset);translate:0 var(--dn,0px)}
.gh.gr::after,.gh.gc::after,.gh.gw{content:"";position:absolute;top:0;bottom:0;border-left:2px dashed rgba(244,244,242,.6)}
.gh.gr::after{left:var(--offset)}
.gh.gc::after{left:calc(var(--offset)*-1)}
.gh.gw{inset:0 auto 0 0}
/* See inside: red copy lifted above the word, cyan copy dropped below it (the separate translate property, as the shake owns
transform); a caption above each row, the behavior caption last. The script sets --fs and the lift (--up, --dn). */
.stage[data-hb-mode="inside"]{padding:8px 12px}
.stage[data-hb-mode="inside"] .real{display:flex;flex-direction:column;align-items:center;max-width:100%;min-width:0}
.stage[data-hb-mode="inside"] .lbl.cap{display:block;max-width:100%;margin:8px 0 2px}
.stage[data-hb-mode="inside"] .nm+.cap{margin-top:0}
.stage[data-hb-mode="inside"] .slot{display:block;height:var(--fs,0px)}
.stage[data-hb-mode="inside"] .gh{display:block}
.stage[data-hb-mode="inside"] .glitch{font-size:clamp(28px,min(12vw,120vw/var(--n,6)),64px)}
.stage[data-hb-mode="inside"] .glitch::before{translate:0 calc(var(--up,0px)*-1)}
.stage[data-hb-mode="inside"] .glitch::after{translate:0 var(--dn,0px)}
/* Compare: the plain word beside (above, on phones) the real one, each half clipping its own scene below its name */
.stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));align-items:stretch;padding:0}
.stage[data-hb-mode="compare"] .half{display:flex;position:relative;overflow:hidden;align-items:center;justify-content:center;min-width:0;padding:40px 12px 12px}
.stage[data-hb-mode="compare"] .real{border-left:1px solid rgba(255,255,255,.14)}
.stage[data-hb-mode="compare"] .lbl.nm{display:block;position:absolute;top:8px;left:10px}
@media(max-width:600px){
.stage[data-hb-mode="compare"]{grid-template-columns:minmax(0,1fr);grid-template-rows:repeat(2,minmax(0,1fr))}
.stage[data-hb-mode="compare"] .real{border-left:0;border-top:1px solid rgba(255,255,255,.14)}
}
</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="../rotate-word-carousel/" rel="prev" aria-label="Previous: Rotate Word Carousel"><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>Rotate Word Carousel</span></a><a href="../text-on-path/" rel="next" aria-label="Next: Text on a Path"><span><span class="hb-dir">Next: </span>Text on a Path</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">05.12 · Text & Typography</p>
<h1>Glitch Text</h1>
<p class="hb-lede">Text tears into red and cyan strips like a broken signal. Best for bold titles.</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="half steady" aria-hidden="true" inert>
<p class="lbl nm" data-hb-caption>Steady</p>
<div class="sw">GLITCH</div>
</div>
<div class="half real" id="real">
<p class="lbl nm" data-hb-caption aria-hidden="true">Glitching</p>
<p class="lbl cap" data-hb-caption aria-hidden="true">Red copy, shifted left: only thin strips show</p>
<div class="slot" aria-hidden="true"></div>
<p class="lbl cap" data-hb-caption aria-hidden="true">The word itself</p>
<div class="glitch" id="glitch" data-text="GLITCH">GLITCH</div>
<p class="lbl cap" data-hb-caption aria-hidden="true">Cyan copy, shifted right: only thin strips show</p>
<div class="slot" aria-hidden="true"></div>
<p class="lbl cap" id="act" data-hb-caption aria-hidden="true">Strips slide at a steady pace and jump to a new spot two or three times a second. The word shakes some.</p>
</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 word has a red copy shifted left and a cyan copy shifted right, pulled apart here. Only thin strips of each copy show, and the strips keep jumping.</p>
<p data-hb-for="compare" hidden>Steady is the word on its own. Glitching adds the red and cyan copies, shows only strips of them and shakes the word now and then, like a broken signal.</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="offset-lbl">Color split</p>
<div class="seg" id="offset-seg" role="group" aria-labelledby="offset-lbl">
<button type="button" data-offset="2" aria-pressed="false">Small</button>
<button type="button" data-offset="4" class="on" aria-pressed="true">Medium</button>
<button type="button" data-offset="8" aria-pressed="false">Large</button>
</div>
<p class="hb-hint">How far the red and cyan copies sit from the word.</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-dur="3.8" aria-pressed="false">Slow</button>
<button type="button" data-dur="2.4" class="on" aria-pressed="true">Normal</button>
<button type="button" data-dur="1.4" aria-pressed="false">Fast</button>
</div>
<p class="hb-hint">How fast the torn strips jump around.</p>
</div>
<div class="hb-setting">
<p class="hb-setting-name" id="intensity-lbl">Glitch strength</p>
<div class="seg" id="intensity-seg" role="group" aria-labelledby="intensity-lbl">
<button type="button" data-intensity="0.3" aria-pressed="false">Mild</button>
<button type="button" data-intensity="0.6" class="on" aria-pressed="true">Medium</button>
<button type="button" data-intensity="0.9" aria-pressed="false">Strong</button>
</div>
<p class="hb-hint">Stronger shakes more and tears more often.</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>Glitches only on hover</span><input class="hb-switch" type="checkbox" role="switch" id="hover-tog" autocomplete="off"></label>
<p class="hb-hint">Holds still until you point at it or tap it.</p>
</div>
<div class="hb-setting">
<label class="hb-setting-name" for="text-inp">Your text</label>
<input class="hb-text" type="text" id="text-inp" value="GLITCH" maxlength="12" autocomplete="off">
<p class="hb-hint">Short words work best, up to 12 letters.</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 glitch effect to [your headline or short word]. Place two copies of the text behind it, one tinted red and one tinted cyan, shifted to either side, and show only thin horizontal strips of each copy, changing which strips show so the colors seem to tear away from the letters like a broken video signal. Every so often, nudge the whole word sideways for a moment and reshuffle the strips so the pattern never repeats. Keep it below a few flickers per second and avoid full-screen flashes. If it only glitches on hover, a tap should start and stop it on touch screens. If the visitor has reduced motion turned on, keep the word 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>Bold titles</li><li>Music and games</li><li>Error pages</li></ul>
<h3>Avoid on</h3>
<ul class="hb-tags hb-avoid"><li>Long text</li><li>Calm brands</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 glitch=document.getElementById('glitch');
const stage=document.getElementById('stage');
const real=document.getElementById('real'),sw=stage.querySelector('.sw'),act=document.getElementById('act');
const vars=document.documentElement.style;
const txtIn=document.getElementById('text-inp');
const hoverTog=document.getElementById('hover-tog');
const slowTog=document.getElementById('slow-tog');
const reduceMQ=matchMedia('(prefers-reduced-motion:reduce)');
let intensity=0.6,dur=2.4,jitterTimer=null;
function rand(max){return Math.floor(Math.random()*max)}
// Runs on its own timer: nudges the word sideways and, by chance, restarts the color copies at a
// random point so the tear never repeats. Except under reduced motion, where it stops, it ends by
// scheduling its own next call, so only one call is ever pending; hb:pause below clears that pending
// call before pausing or restarting it.
function reslice(){
if(reduceMQ.matches){glitch.style.transform='';jitterTimer=null;return}
const spread=Math.round(intensity*40);
const jitter=(intensity*6).toFixed(1);
// in hover-only mode the jitter only runs while the stage is hovered / tapped active; a touch screen
// keeps :hover on the stage after a tap, so only a device that can hover counts as hovering
const live=!stage.classList.contains('hover-only')||(matchMedia('(hover:hover)').matches&&stage.matches(':hover'))||stage.classList.contains('active');
if(live){
// nudge the whole element sideways for a frame to sell the tear
glitch.style.transform=`translateX(${(Math.random()*2-1)*jitter}px)`;
// occasionally restart the clone animations at a random point on their timeline
if(Math.random()<intensity){
glitch.style.setProperty('--phase',(-Math.random()*dur).toFixed(2)+'s');
glitch.classList.add('reset');void glitch.offsetWidth;glitch.classList.remove('reset');
// Slow motion: the restarted copies would run at full speed for a frame before the shared
// script's own loop catches them, so slow them here too, right away.
if(slowTog.checked)glitch.getAnimations({subtree:true}).forEach(a=>{a.playbackRate=1/3});
}
}else{
glitch.style.transform='';
}
const wait=120+rand(spread*8+60);
jitterTimer=setTimeout(reslice,slowTog.checked?wait*3:wait);
}
// Registered at the top level, before the shared script (which is deferred) can run: paused stops the
// jitter timer and any shake in progress; unpausing clears it too, then starts the chain again.
document.addEventListener('hb:pause',e=>{
clearTimeout(jitterTimer);
if(e.detail.paused)glitch.style.transform='';
else{still=null;reslice()}
});
// See inside and Compare are drawn from the page's own numbers; a press draws the mode at once and changes nothing else.
// Under reduced motion a paused loop can rest where neither strip crosses the letters, so a mode press poses the strips at their
// arrival frame (both cross the letters) and Normal puts them back where they stood; Play lets go of the saved times.
let still=null;
document.addEventListener('hb:mode',e=>{
fit();
const strips=glitch.getAnimations({subtree:true});
if(e.detail.mode!=='normal'&&reduceMQ.matches&&stage.classList.contains('hb-paused')){
if(!still)still=strips.map(a=>[a,a.currentTime]);
strips.forEach(a=>{a.currentTime=a.effect.getTiming().delay});
}else if(e.detail.mode==='normal'&&still){
still.forEach(([a,t])=>{a.currentTime=t});
still=null;
}
});
// The CSS formula is viewport-based; measure against the room the mode on show gives and shrink further if needed. Normal keeps
// the page's own sizing. See inside shares the height between three rows and the captions; a Compare half has its own width.
function fit(){
glitch.style.fontSize='';
const mode=stage.dataset.hbMode,base=parseFloat(getComputedStyle(glitch).fontSize);
let avail=stage.clientWidth-48,want=base;
if(mode==='compare'){avail=real.clientWidth-60;want=Math.min(base,real.clientHeight-56)}
if(mode==='inside'){
avail=stage.clientWidth-64;
glitch.style.fontSize='0px';real.style.setProperty('--fs','0px'); // what the captions and gaps take of the height
want=Math.max(24,Math.min(base,(stage.clientHeight-16-real.offsetHeight)/3));
glitch.style.fontSize='';
}
const w=glitch.scrollWidth*(want/base),size=w>avail?Math.max(16,want*avail/w):want;
if(size!==base)glitch.style.fontSize=size+'px';
const px=getComputedStyle(glitch).fontSize;
real.style.setProperty('--fs',px);sw.style.fontSize=px;
if(mode==='inside'){ // how far the copies are lifted: from the word's row to the empty rows above and below it
const slots=real.querySelectorAll('.slot');
real.style.setProperty('--up',glitch.offsetTop-slots[0].offsetTop+'px');
real.style.setProperty('--dn',slots[1].offsetTop-glitch.offsetTop+'px');
}
}
// The faint whole copies and the word's dashed line live inside the word, so a new text puts them back
const ghosts=['gr','gc','gw'].map(c=>{const g=document.createElement('i');g.className='gh '+c;g.setAttribute('aria-hidden','true');g.inert=true;return g});
function copies(t){
glitch.dataset.text=t;sw.textContent=t;
ghosts.slice(0,2).forEach(g=>{g.textContent=t});
ghosts.forEach(g=>glitch.append(g));
}
copies(glitch.dataset.text);
txtIn.addEventListener('input',e=>{
const t=e.target.value.trim()||'GLITCH';
glitch.textContent=t;copies(t);
glitch.style.setProperty('--n',t.length); // longer words shrink so they stay inside the stage
fit();
});
addEventListener('resize',fit);
fit();
if(document.fonts)document.fonts.ready.then(fit); // the site font can still be loading when fit() first runs
hoverTog.addEventListener('change',()=>{
stage.classList.toggle('hover-only',hoverTog.checked);
stage.classList.remove('active');
words();
});
// touch has no hover: a tap on the stage toggles the hover-only state instead
stage.addEventListener('pointerdown',e=>{
if(e.pointerType!=='mouse'&&stage.classList.contains('hover-only'))stage.classList.toggle('active');
});
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('offset-seg',d=>{vars.setProperty('--offset',d.offset+'px')});
choices('speed-seg',d=>{dur=+d.dur;vars.setProperty('--speed',d.dur+'s');words()});
choices('intensity-seg',d=>{intensity=+d.intensity;words()});
// The behavior caption and the notes, in the page's own numbers: how long a slide takes, how often reslice() restarts the strips
// (about 1.5, 2.5 and 3 a second, a third as often in slow motion) and how far it shakes the word. Written only when they change.
const notes=[...document.querySelectorAll('[data-hb-for]')],normalNotes=notes.map(n=>n.textContent),reducedNotes=[
'Pulled apart here: the red copy sits above the word, shifted left, and the cyan copy below, shifted right. Only the bright strips show; the faint rest marks what stays hidden.',
'Steady is the word on its own, while Glitching shows strips of its red and cyan copies torn off to either side.'];
function words(){
const i=intensity<.5?0:intensity<.8?1:2,slide=['slowly','at a steady pace','quickly'][dur>3?0:dur>2?1:2];
const jumps=[['once or twice a second','once every two seconds'],['two or three times a second','about once a second'],['three times a second','about once a second']][i][+slowTog.checked];
const text=(reduceMQ.matches?`With reduced motion on, the strips only slide ${slide}, and the word never shakes.`
:`Strips slide ${slide} and jump to a new spot ${jumps}. The word shakes ${['a little','some','a lot'][i]}.`)
+(hoverTog.checked?' Nothing moves until you point or tap.':'');
if(act.textContent!==text)act.textContent=text;
notes.forEach((n,k)=>{let t=reduceMQ.matches?reducedNotes[k]:normalNotes[k];if(hoverTog.checked)t=t.replace('and the strips keep jumping.','and they jump while you point or tap.');if(n.textContent!==t)n.textContent=t});
if(stage.dataset.hbMode==='inside')fit(); // the caption can take more or fewer lines
}
slowTog.addEventListener('change',words);
reduceMQ.addEventListener('change',words);
words();
if(!reduceMQ.matches)reslice();
</script>
</body>
</html>