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: the shared script runs on pages without a prompt and drives the mode switch (Normal, See inside, Compare)

matind44c6ff1330f
diff --git a/assets/js/demo-page.js b/assets/js/demo-page.js
index 06b7885a..62a32c46 100644
--- a/assets/js/demo-page.js
+++ b/assets/js/demo-page.js
@@ -6,6 +6,8 @@
* takes over ("hb:input"), and follows reduced motion (Loop and Slow motion greyed out,
* loops start paused).
* On scroll pages Play scrolls the box, Back to top jumps to its top and the visitor's own input stops it.
+ * The mode switch (Normal, See inside, Compare) marks the stage with data-hb-mode, shows that mode's note and sends
+ * "hb:mode", so the page draws the mode. Pages without a prompt (the idea lessons) get everything but the prompt parts.
* Opened from disk (file:), the page's links to folders open each folder's index.html instead of its listing.
* The pure helpers are exported for tests/demo-page.test.js. */
(function (root, factory) {
@@ -219,8 +221,8 @@
function boot(doc, win) {
var page = doc.querySelector('.hb-page');
- var promptEl = page && page.querySelector('.hb-prompt');
- if (!promptEl) return;
+ if (!page) return;
+ var promptEl = page.querySelector('.hb-prompt');
var tryStep = page.querySelector('.hb-try');
var chipsBox = page.querySelector('.hb-chips-box');
@@ -235,6 +237,7 @@
var demoCtl = page.querySelector('[data-hb-demo]');
var scrollCtl = page.querySelector('[data-hb-autoscroll]');
var topCtl = page.querySelector('[data-hb-top]');
+ var modesCtl = page.querySelector('[data-hb-modes]');
var scroller = page.querySelector('[data-hb-scroller]') || stage;
var reduce = win.matchMedia ? win.matchMedia('(prefers-reduced-motion: reduce)') : null;
var promptText = text(promptEl);
@@ -482,6 +485,29 @@
});
}
+ // See inside and Compare: the mode switch above the stage. Pressing a mode presses its button (one at a time), marks the
+ // stage with data-hb-mode, shows the note for that mode (Normal has none) and sends "hb:mode", so the page draws the mode.
+ // Nothing scrolls and nothing takes focus, and a running Play goes on: the switch sits outside the stage, whose input
+ // stops Play.
+ function setUpModes() {
+ var buttons = Array.prototype.slice.call(modesCtl.querySelectorAll('[data-hb-mode]'));
+ var notes = Array.prototype.slice.call(page.querySelectorAll('[data-hb-for]'));
+ function choose(mode) {
+ buttons.forEach(function (b) {
+ var on = b.getAttribute('data-hb-mode') === mode;
+ b.classList.toggle('on', on);
+ b.setAttribute('aria-pressed', String(on));
+ });
+ notes.forEach(function (n) { n.hidden = n.getAttribute('data-hb-for') !== mode; });
+ if (stage) stage.setAttribute('data-hb-mode', mode);
+ doc.dispatchEvent(new win.CustomEvent('hb:mode', { detail: { mode: mode } }));
+ }
+ buttons.forEach(function (b) {
+ b.addEventListener('click', function () { choose(b.getAttribute('data-hb-mode')); });
+ });
+ if (stage) stage.setAttribute('data-hb-mode', 'normal');
+ }
+
// While the device asks for reduced motion, Loop and Slow motion are switched off and cannot be switched on, a
// loop starts paused, and a note in the player bar says why. Replay and Play still work. A scroll page has nothing to
// switch off, and its note says that the effects follow the scroll without animating. A page where reduced motion turns
@@ -513,13 +539,14 @@
}
if (win.location.protocol === 'file:') openFoldersAsPages();
- setUpPrompt();
+ if (promptEl) setUpPrompt();
if (copyBtn) setUpCopy();
if (tryStep) ['input', 'change', 'click'].forEach(function (type) { tryStep.addEventListener(type, onSettingsChange); });
if (slowCtl) slowCtl.addEventListener('change', replay);
if (pauseCtl) pauseCtl.addEventListener('click', function () { setPaused(!paused); });
if (demoCtl && stage) setUpVisitorInput();
if (scrollCtl && scroller) setUpScroll();
+ if (modesCtl) setUpModes();
if (slowCtl && stage && stage.getAnimations && slowCtl.getAttribute('data-hb-slowmo') === 'css') {
slowCtl.addEventListener('change', function () { if (!slowing) slowStage(); });
if (slowCtl.checked) slowStage();
diff --git a/tests/demo-page.test.js b/tests/demo-page.test.js
index 5495d20f..2bc9e339 100644
--- a/tests/demo-page.test.js
+++ b/tests/demo-page.test.js
@@ -128,7 +128,10 @@ test('motionNote says what reduced motion changes in the player bar', () => {
// `copy` ({ prompt, controls, clipboard }) to add a Copy prompt button, text in the prompt, a Try it step holding `controls` (if
// any) and the window's clipboard; no other test gets any of them. A link test passes `links` ({ protocol, hrefs }): the page is
// served over that protocol (http: otherwise) and holds one link for each href.
-function standInPage(kind, reduced, bodyAttributes, copy, links) {
+// `extra` ({ modes, noPrompt }): modes adds the mode switch (Normal, See inside, Compare) and one note for See inside and one
+// for Compare; noPrompt leaves the prompt out, as on an idea lesson.
+function standInPage(kind, reduced, bodyAttributes, copy, links, extra) {
+ const opts = extra || {};
const timers = [], sent = [];
const node = () => ({
attrs: {}, handlers: {}, children: [], clicks: 0, textContent: '', innerHTML: '', classList: { add() {}, remove() {}, toggle() {} },
@@ -142,13 +145,28 @@ function standInPage(kind, reduced, bodyAttributes, copy, links) {
insertAdjacentElement() {}, contains(other) { return other === this; }, click() { this.clicks++; }
});
const stage = node(), player = node(), prompt = node(), demo = node(), play = node(), top = node();
+ const modeButtons = opts.modes ? ['normal', 'inside', 'compare'].map(mode => {
+ const b = node();
+ b.setAttribute('data-hb-mode', mode);
+ b.setAttribute('aria-pressed', String(mode === 'normal'));
+ return b;
+ }) : [];
+ const notes = opts.modes ? ['inside', 'compare'].map(mode => {
+ const p = node();
+ p.setAttribute('data-hb-for', mode);
+ p.hidden = true;
+ return p;
+ }) : [];
+ const modes = opts.modes && Object.assign(node(), { querySelectorAll: selector => (selector === '[data-hb-mode]' ? modeButtons : []) });
// The button has no .hb-copy-label inside it, so its own text is the label.
const button = copy && Object.assign(node(), { querySelector: () => null });
if (copy) prompt.textContent = copy.prompt;
- const wanted = { '.hb-prompt': prompt, '.stage': stage, '.hb-player': player, '[data-hb-demo]': kind === 'do' && demo,
+ const wanted = { '.hb-prompt': !opts.noPrompt && prompt, '.stage': stage, '.hb-player': player, '[data-hb-demo]': kind === 'do' && demo,
'[data-hb-autoscroll]': kind === 'scroll' && play, '[data-hb-top]': kind === 'scroll' && top, '.hb-copy': button,
- '.hb-try': copy && copy.controls && Object.assign(node(), { querySelectorAll: () => copy.controls }) };
- const page = { querySelector: selector => wanted[selector] || null };
+ '.hb-try': copy && copy.controls && Object.assign(node(), { querySelectorAll: () => copy.controls }),
+ '[data-hb-modes]': modes };
+ const page = { querySelector: selector => wanted[selector] || null,
+ querySelectorAll: selector => (selector === '[data-hb-for]' ? notes : []) };
const body = node();
body.setAttribute('data-hb-kind', kind);
body.setAttribute('data-hb-autoplay', '');
@@ -165,7 +183,8 @@ function standInPage(kind, reduced, bodyAttributes, copy, links) {
};
if (copy) Object.assign(win, { navigator: { clipboard: copy.clipboard }, getComputedStyle: () => ({ display: '' }) });
DP.boot(doc, win);
- return { doc, win, stage, player, demo, button, anchors, sent, runTimers: () => timers.forEach(t => t.fn()), timerDelays: () => timers.map(t => t.ms) };
+ return { doc, win, stage, player, demo, play, button, anchors, sent, modeButtons, notes,
+ runTimers: () => timers.forEach(t => t.fn()), timerDelays: () => timers.map(t => t.ms) };
}
const visitor = { isTrusted: true }, script = { isTrusted: false };
@@ -256,6 +275,67 @@ test('a scroll page with an empty sentence of its own gets the usual note, and o
assert.equal(standInPage('scroll', false, { 'data-hb-motion-note': 'The layers stay still while the box scrolls' }).player.children.length, 0);
});
+test('a page with no prompt, such as an idea lesson, still gets its player: a scroll page scrolls on arrival and notes reduced motion', () => {
+ const page = standInPage('scroll', false, {}, null, null, { noPrompt: true });
+ page.runTimers();
+ assert.equal(page.play.clicks, 1);
+ const reduced = standInPage('scroll', true, {}, null, null, { noPrompt: true });
+ assert.deepEqual(reduced.player.children.map(c => c.className), ['hb-player-note']);
+});
+
+test("the mode switch presses one mode at a time, marks the stage, shows only that mode's note and tells the page", () => {
+ const page = standInPage('scroll', false, {}, null, null, { modes: true });
+ assert.equal(page.stage.getAttribute('data-hb-mode'), 'normal');
+ const [normal, inside, compare] = page.modeButtons;
+ const pressed = () => page.modeButtons.map(b => b.getAttribute('aria-pressed'));
+ const shown = () => page.notes.map(n => !n.hidden);
+ const told = () => page.sent.filter(e => e.type === 'hb:mode').map(e => e.detail.mode);
+ assert.deepEqual(told(), [], 'nothing is sent at boot');
+ inside.emit('click', visitor);
+ assert.deepEqual(pressed(), ['false', 'true', 'false']);
+ assert.equal(page.stage.getAttribute('data-hb-mode'), 'inside');
+ assert.deepEqual(shown(), [true, false]);
+ compare.emit('click', visitor);
+ assert.deepEqual(pressed(), ['false', 'false', 'true']);
+ assert.deepEqual(shown(), [false, true]);
+ normal.emit('click', visitor);
+ assert.deepEqual(pressed(), ['true', 'false', 'false']);
+ assert.deepEqual(shown(), [false, false]);
+ assert.equal(page.stage.getAttribute('data-hb-mode'), 'normal');
+ assert.deepEqual(told(), ['inside', 'compare', 'normal']);
+});
+
+test('a page without the mode switch gets no data-hb-mode and sends no hb:mode', () => {
+ const page = standInPage('scroll', false);
+ page.runTimers();
+ assert.equal(page.stage.getAttribute('data-hb-mode'), null);
+ assert.equal(page.sent.filter(e => e.type === 'hb:mode').length, 0);
+});
+
+// The stand-in's class lists do nothing, so this test gives the three mode buttons lists that remember what was toggled.
+test('the mode switch gives the class on to the pressed mode only', () => {
+ const page = standInPage('scroll', false, {}, null, null, { modes: true });
+ const classes = page.modeButtons.map(b => {
+ const set = new Set();
+ b.classList = { toggle: (name, force) => (force ? set.add(name) : set.delete(name)) };
+ return set;
+ });
+ const [normal, inside, compare] = page.modeButtons;
+ const marked = () => classes.map(set => set.has('on'));
+ inside.emit('click', visitor);
+ assert.deepEqual(marked(), [false, true, false]);
+ compare.emit('click', visitor);
+ assert.deepEqual(marked(), [false, false, true]);
+ normal.emit('click', visitor);
+ assert.deepEqual(marked(), [true, false, false]);
+});
+
+// The home page loads the shared script too and has no .hb-page: boot() returns before it touches anything else.
+test('a document with no .hb-page, such as the home page, is skipped without touching anything else', () => {
+ const doc = { querySelector: () => null };
+ assert.doesNotThrow(() => DP.boot(doc, { document: doc }));
+});
+
// Opened from disk, Chrome shows a folder's listing for a link to the folder, so the links of the page must name index.html there.
test('opened from disk, every link of the page to a folder opens its index.html; over http(s) no link changes', () => {
const hrefs = ['../../../', '../bounce-in/', '../scale-in/', '../blur-in/?from=home#hb-prompt-title', 'README.md', '#top',