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
fix: tighten the do-it checks and keep stage focus rings visible
matin52fd4027bc9c
diff --git a/assets/css/demo-page.css b/assets/css/demo-page.cssindex d32f0292..c31af48d 100644--- a/assets/css/demo-page.css+++ b/assets/css/demo-page.css@@ -68,6 +68,10 @@ body.hb [hidden]{display:none!important}.hb-page .stage.hb-dots{background-image:radial-gradient(rgba(255,255,255,.075) 1px,transparent 1.5px);background-size:24px 24px;background-position:center}/* A stage whose text the visitor can change: at least the usual height, and taller when the text needs it */.hb-page .stage.hb-grow{height:auto;min-height:clamp(300px,calc(100vh - 420px),440px);min-height:clamp(300px,calc(100svh - 420px),440px)}+/* The stage clips what sticks out of it, so the site focus ring (drawn 3px outside) would be cut off on anything focusable+ inside it: there the ring is drawn inward. This selector outranks body.hb :focus-visible. A stage that is itself the+ scroller is not inside itself, so it keeps the normal ring. */+.hb-page .stage :focus-visible{outline-offset:-3px}.hb-player{display:flex;flex-wrap:wrap;align-items:center;gap:10px}.hb-play,.hb-toggle{box-sizing:border-box;display:inline-flex;align-items:center;gap:10px;min-height:44px;padding:0 18px;border:1px solid rgba(255,255,255,.18);border-radius:8px;background:none;color:var(--hb-ink);font:500 15px/1 var(--hb-font);cursor:pointer}@@ -101,7 +105,7 @@ body.hb [hidden]{display:none!important}.hb-setting-name{margin:0;font:600 17px/1.3 var(--hb-font);color:var(--hb-ink)}.hb-hint{margin:0;font:400 14px/1.45 var(--hb-font);color:var(--hb-muted)}.hb-page .seg{display:flex;flex-wrap:wrap;gap:8px}-.hb-page .seg button{box-sizing:border-box;min-height:44px;padding:0 16px;border:1px solid var(--hb-edge);border-radius:8px;background:none;+.hb-page .seg button{box-sizing:border-box;min-height:44px;min-width:44px;padding:0 16px;border:1px solid var(--hb-edge);border-radius:8px;background:none;color:var(--hb-soft);font:500 15px/1.2 var(--hb-font);cursor:pointer;transition:background-color .15s,border-color .15s,color .15s}.hb-page .seg button.on,.hb-page .seg button[aria-pressed="true"]{border-color:var(--hb-accent);color:var(--hb-accent);background:color-mix(in srgb,var(--hb-accent) 12%,transparent)}diff --git a/tests/pages.test.js b/tests/pages.test.jsindex fc4eaa5c..21ee1f91 100644--- a/tests/pages.test.js+++ b/tests/pages.test.js@@ -1,4 +1,4 @@-// Static checks for the guided-steps demo pages, their READMEs and the home page.+// Static checks for the guided-steps demo pages, their READMEs, the home page and the shared stylesheet.// Run from the repo root: node --test "tests/*.test.js"const test = require('node:test');const assert = require('node:assert/strict');@@ -20,6 +20,16 @@ const between = (s, from, to) => {return s.slice(start, end < 0 ? undefined : end);};const decode = s => s.replace(/"/g, '"').replace(/'/g, "'").replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&');+// The rules of a stylesheet as { selector, body }: comments removed, rules inside @media and @supports included.+const cssRules = css => [...css.replace(/\/\*[\s\S]*?\*\//g, '').matchAll(/([^{}]+)\{([^{}]*)\}/g)]+ .map(m => ({ selector: m[1].trim(), body: m[2] }));+// [ids, classes + attributes + pseudo-classes, elements] of a selector that has no functional pseudo-class.+const specificity = selector => [+ (selector.match(/#[\w-]+/g) || []).length,+ (selector.match(/\.[\w-]+|\[[^\]]*\]|(?<!:):(?!:)[\w-]+/g) || []).length,+ (selector.replace(/\.[\w-]+|#[\w-]+|\[[^\]]*\]|::?[\w-]+/g, ' ').match(/[a-zA-Z][\w-]*/g) || []).length+];+const outranks = (a, b) => { for (let i = 0; i < 3; i++) if (a[i] !== b[i]) return a[i] > b[i]; return false; };const demos = fs.readdirSync(ANIM, { withFileTypes: true }).filter(c => c.isDirectory()).flatMap(c =>fs.readdirSync(path.join(ANIM, c.name), { withFileTypes: true }).filter(d => d.isDirectory())@@ -40,6 +50,29 @@ const CONVERTED = [ENTRANCE_EXIT, '05-text-typography'];// The Pause button of a loop page, exactly as in the template; {MODE} is '' (the page pauses itself) or '="css"'.const PAUSE = '<button class="hb-play" type="button" id="btn-pause" data-hb-pause{MODE} 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>';+// The Show me and Reset buttons of a do-it page, exactly as in the template (Reset only where the demo has a state to reset).+const SHOW_ME = '<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>';+const RESET = '<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>';++// Show me belongs to do-it pages and Reset to do-it and loop pages, each exactly as in the template: the shared script picks+// what to press on arrival by the button it finds, and Reset brings back what the visitor changed. `html` is the whole page+// and `player` its player bar.+function assertShowMeAndReset(kind, html, player) {+ const bar = player.replace(/>\s+</g, '><'); // the player bar without the white space between its elements+ if (kind === 'do') {+ assert.equal(count(html, 'data-hb-demo'), 1, 'one Show me control');+ assert.ok(bar.includes(SHOW_ME), 'the Show me button is in the player bar exactly as in the template');+ } else {+ assert.equal(count(html, 'data-hb-demo'), 0, 'only a do-it page has data-hb-demo');+ }+ if (!html.includes('data-hb-reset')) return;+ assert.ok(kind === 'do' || kind === 'loop', 'only a do-it or loop page has data-hb-reset');+ assert.equal(count(html, 'data-hb-reset'), 1, 'at most one Reset');+ assert.ok(bar.includes(RESET), 'the Reset button is in the player bar exactly as in the template');+ // On a do-it page Reset comes right after Show me: on phones two buttons that sit next to each other share one row.+ if (kind === 'do') assert.ok(bar.includes(SHOW_ME + RESET), 'on a do-it page the Reset button comes right after Show me');+}+for (const cat of CONVERTED) {test(`every ${cat} demo uses the guided-steps page`, () => {const inCategory = demos.filter(d => d.cat === cat);@@ -102,10 +135,8 @@ for (const d of steps) {'a loop has no Replay, Loop or autoplay');}if (kind === 'do') {- assert.equal(count(html, 'data-hb-demo'), 1, 'one Show me control');- assert.ok(player.includes('<button class="hb-play" type="button" id="btn-demo" data-hb-demo>'), 'Show me is in the player bar');- assert.ok(count(html, 'data-hb-reset') <= 1 && count(player, 'data-hb-reset') === count(html, 'data-hb-reset'),- 'at most one Reset, in the player bar');+ // A do-it page shows itself once on arrival: the shared script presses Show me when the body asks for autoplay.+ assert.ok(body[2], 'a do-it page runs Show me once on arrival (data-hb-autoplay on the body)');for (const other of ['data-hb-replay', 'data-hb-loop', 'data-hb-pause', 'data-hb-autoscroll', 'data-hb-top']) {assert.equal(count(html, other), 0, `a do-it page has no ${other}`);}@@ -120,6 +151,7 @@ for (const d of steps) {assert.equal(count(html, other), 0, `a scroll page has no ${other}`);}}+ assertShowMeAndReset(kind, html, player);for (const marker of ['data-hb-loop', 'data-hb-slowmo', 'data-hb-pause', 'data-hb-demo', 'data-hb-reset', 'data-hb-autoscroll', 'data-hb-top']) {assert.ok(count(html, marker) <= 1, `at most one ${marker}`);assert.equal(count(player, marker), count(html, marker), `${marker} is in the player bar`);@@ -225,11 +257,65 @@ for (const d of steps) {});}+// The Show me and Reset rules run on the player bar of every page above. These cases run them on made-up player bars, so+// each kind is covered before a page of that kind exists.+test('Show me belongs to do-it pages and Reset to do-it and loop pages, each exactly as in the template', () => {+ const PAUSE_BUTTON = PAUSE.replace('{MODE}', '');+ const REPLAY = '<button class="hb-play" type="button" id="btn-play" data-hb-replay>Replay</button>';+ const PLAY = '<button class="hb-play" type="button" id="btn-scroll" data-hb-autoscroll>Play</button>';+ const TOP = '<button class="hb-play" type="button" id="btn-top" data-hb-top>Back to top</button>';+ const SLOW = '<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>';+ const barOf = (...items) => `<div class="hb-player">\n ${items.join('\n ')}\n </div>`;+ const check = (kind, html) => assertShowMeAndReset(kind, html, between(html, '<div class="hb-player">', '</div>'));+ const ok = (kind, ...items) => assert.doesNotThrow(() => check(kind, barOf(...items)), `a ${kind} page with ${items.length} controls passes`);+ const notOk = (message, kind, html) => assert.throws(() => check(kind, html), err => err.message.startsWith(message), `${kind}: ${message}`);++ ok('do', SHOW_ME);+ ok('do', SHOW_ME, RESET);+ ok('do', SHOW_ME, RESET, SLOW);+ ok('loop', PAUSE_BUTTON, RESET, SLOW); // a loop may have a Reset too: Pause, Reset, Slow motion+ ok('loop', PAUSE_BUTTON, SLOW);+ ok('once', REPLAY, SLOW);+ ok('scroll', PLAY, TOP);++ for (const kind of ['once', 'loop', 'scroll']) notOk('only a do-it page has data-hb-demo', kind, barOf(SHOW_ME, REPLAY));+ for (const kind of ['once', 'scroll']) notOk('only a do-it or loop page has data-hb-reset', kind, barOf(REPLAY, RESET));++ notOk('one Show me control', 'do', barOf(RESET));+ notOk('one Show me control', 'do', barOf(SHOW_ME, SHOW_ME.replace('id="btn-demo"', 'id="btn-demo-2"')));+ notOk('the Show me button is in the player bar exactly as in the template', 'do', barOf(SHOW_ME.replace('Show me</button>', 'Play</button>')));+ notOk('on a do-it page the Reset button comes right after Show me', 'do', barOf(SHOW_ME, SLOW, RESET));+ notOk('on a do-it page the Reset button comes right after Show me', 'do', barOf(RESET, SHOW_ME));+ for (const [kind, first] of [['do', SHOW_ME], ['loop', PAUSE_BUTTON]]) {+ notOk('the Reset button is in the player bar exactly as in the template', kind, barOf(first, RESET.replace('class="hb-play"', 'class="hb-reset"')));+ notOk('the Reset button is in the player bar exactly as in the template', kind, barOf(first, RESET.replace('id="btn-reset"', 'id="reset"')));+ notOk('the Reset button is in the player bar exactly as in the template', kind, `<div class="stage">${RESET}</div>${barOf(first)}`);+ notOk('at most one Reset', kind, barOf(first, RESET, RESET.replace('id="btn-reset"', 'id="btn-reset-2"')));+ }+});+test('every guided-steps page links the same version of the shared files', () => {const versions = new Set(steps.flatMap(d => [...pageOf(d).matchAll(/demo-page\.(?:css|js)\?v=(\d+)/g)].map(m => m[1])));assert.equal(versions.size, 1, `versions in use: ${[...versions].join(', ')}`);});+const sharedRules = cssRules(read(path.join(ROOT, 'assets/css/demo-page.css')));+const rulesFor = selector => sharedRules.filter(rule => rule.selector.split(',').some(part => part.trim() === selector));++test('the shared stylesheet draws the focus ring inward inside the stage, beating the site ring', () => {+ // The stage clips what sticks out of it, so the site ring (drawn 3px outside) is cut off on anything focusable inside it.+ const site = sharedRules.filter(rule => /^body\.hb[^,]*:focus-visible$/.test(rule.selector));+ assert.equal(site.length, 1, 'the site focus ring rule (body.hb :focus-visible) is in the stylesheet');+ const inward = '.hb-page .stage :focus-visible';+ assert.ok(rulesFor(inward).some(rule => /(^|;)\s*outline-offset:\s*-3px\s*(;|$)/.test(rule.body)), 'a focus ring inside the stage is drawn 3px inward');+ assert.ok(outranks(specificity(inward), specificity(site[0].selector)),+ `${inward} (${specificity(inward)}) must outrank ${site[0].selector} (${specificity(site[0].selector)})`);+});++test('the shared stylesheet keeps choice buttons at least 44px wide', () => {+ assert.ok(rulesFor('.hb-page .seg button').some(rule => /(^|;)\s*min-width:\s*44px\s*(;|$)/.test(rule.body)), 'a choice button is at least 44px wide');+});+test('the home page uses Schibsted Grotesk and the new intro line', () => {assert.ok(HOME.includes("url('assets/fonts/schibsted-latin.woff2')"), 'Latin font file');assert.ok(HOME.includes("url('assets/fonts/schibsted-latin-ext.woff2')"), 'Latin Extended font file');diff --git a/tools/check-pages.mjs b/tools/check-pages.mjsindex c59bd186..f370d8ee 100644--- a/tools/check-pages.mjs+++ b/tools/check-pages.mjs@@ -9,9 +9,10 @@// script warns about settings that have no label or value). On plays-once pages the desktop run also checks that// Replay and a setting change visibly move the stage; on loop pages it checks that the stage moves, that Pause stops// it and Play starts it again (twice over), and the reduced-motion run checks that the loop starts paused. On do-it-// pages the desktop and reduced-motion runs press Show me and check that it visibly moves the stage (the desktop run-// waits for the run on arrival to end first), and the reduced-motion run checks that nothing moves before it is-// pressed. On scroll pages the desktop and reduced-motion runs check that the box scrolls by itself on arrival (not+// pages every press of Show me is recorded from the start of the document: the desktop run checks that the page pressed+// it exactly once on arrival and waits for that run to end, the reduced-motion run checks that it pressed it not at+// all; both then check that the stage is at rest, that pressing Show me visibly moves it and that the run brings it+// back to rest. On scroll pages the desktop and reduced-motion runs check that the box scrolls by itself on arrival (not// under reduced motion), that Back to top returns it to the top and stops it, that Play scrolls it and that a real// wheel turn over the box stops Play. The 320px phone runs the page checks (overflow, small targets, chips, README)// and takes screenshots, and nothing more.@@ -96,6 +97,15 @@ const CHECK = `(() => {return { kind: document.body.dataset.hbKind || '', loop: loop ? loop.checked : null, switches, problems };})()`;+// Runs at the start of every document, before the page's own scripts: records each press of a Show me button, whether it+// came from the visitor or from the page script, so the press on arrival can be counted exactly whatever the run's length.+const RECORD_SHOW_ME = `(() => {+ window.__hbShowMe = [];+ document.addEventListener('click', e => {+ if (e.target.closest && e.target.closest('[data-hb-demo]')) window.__hbShowMe.push({ trusted: e.isTrusted });+ }, true);+})()`;+const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));const withTimeout = (promise, ms) => Promise.race([promise, sleep(ms).then(() => { throw new Error('timeout'); })]);@@ -249,18 +259,48 @@ async function loopProblems(reduced) {return problems;}-// Do-it pages: Show me must visibly move the stage within 1.6 s, and under reduced motion the page must not move by-// itself. Show me runs on arrival, so the desktop check first waits for that run to end.+// True when two stage captures (base64 PNG) show the same picture. After a full-page capture, headless Chrome can leave+// a couple of faint stale pixels at the edge of something that scaled up and came back (two pixels, 26 of 765 apart, were+// seen); a real leftover differs in far more pixels and far more strongly. So pixels that differ by 64 or less (summed+// over red, green and blue) do not count, and neither do the first few that differ more.+async function samePicture(a, b) {+ if (a === b) return true;+ return evaluate(`new Promise(done => {+ const load = src => new Promise(resolve => { const img = new Image(); img.onload = () => resolve(img); img.src = 'data:image/png;base64,' + src; });+ Promise.all([load(${JSON.stringify(a)}), load(${JSON.stringify(b)})]).then(([x, y]) => {+ if (x.width !== y.width || x.height !== y.height) { done(false); return; }+ const read = img => { const c = document.createElement('canvas'); c.width = img.width; c.height = img.height; const g = c.getContext('2d'); g.drawImage(img, 0, 0); return g.getImageData(0, 0, c.width, c.height).data; };+ const p = read(x), q = read(y);+ let visible = 0;+ for (let i = 0; i < p.length; i += 4) if (Math.abs(p[i] - q[i]) + Math.abs(p[i + 1] - q[i + 1]) + Math.abs(p[i + 2] - q[i + 2]) > 64) visible++;+ done(visible <= Math.max(8, Math.round(x.width * x.height / 10000)));+ });+ })`);+}++// Do-it pages: the page presses Show me once by itself on arrival and not at all under reduced motion (the presses are+// recorded from the start of the document, so this holds for a run of any length). Show me runs on arrival, so the+// desktop check first waits for that run to end. The stage must then be at rest, Show me must visibly move it within+// 1.6 s, and the run must bring it back to rest.async function demoProblems(reduced) {if (!(await evaluate(`!!document.querySelector('[data-hb-demo]')`))) return ['no Show me button to check'];const problems = [];- if (reduced && await stageChanges(1500)) problems.push('the stage moves by itself under reduced motion');+ const presses = await evaluate(`(window.__hbShowMe || []).length`);+ if (reduced && presses) problems.push('Show me is pressed on arrival under reduced motion');+ if (!reduced && presses !== 1) problems.push(presses ? `Show me is pressed ${presses} times on arrival, not once` : 'Show me is not pressed on arrival');if (!reduced) await sleep(5000);+ if (await stageChanges(reduced ? 1500 : 800)) {+ problems.push(reduced ? 'the stage moves by itself under reduced motion' : 'the stage is not at rest after the run on arrival (it keeps moving by itself)');+ return problems;+ }const rest = await stageShot();await evaluate(`document.querySelector('[data-hb-demo]').click()`);let moved = false;for (let i = 0; i < 8 && !moved; i++) { await sleep(200); moved = (await stageShot()) !== rest; }- if (!moved) problems.push('Show me does not visibly move the stage');+ if (!moved) { problems.push('Show me does not visibly move the stage'); return problems; }+ let back = false;+ for (let i = 0; i < 32 && !back; i++) { await sleep(250); back = await samePicture(await stageShot(), rest); }+ if (!back) problems.push('the run does not bring the stage back to rest');return problems;}@@ -312,6 +352,7 @@ try {await send('Page.enable');await send('Runtime.enable');await send('Log.enable');+ await send('Page.addScriptToEvaluateOnNewDocument', { source: RECORD_SHOW_ME });for (const page of pages) {const parts = page.replace(/\/$/, '').split('/');