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.

fix: the arrival press of Show me is skipped when the visitor has already acted in the stage

matina7d8f7855e97
diff --git a/assets/js/demo-page.js b/assets/js/demo-page.js
index 2703662a..a8623713 100644
--- a/assets/js/demo-page.js
+++ b/assets/js/demo-page.js
@@ -1,9 +1,10 @@
/* Animation Handbook — shared behaviour for the guided-steps demo pages.
* Fills in "Your settings", Copy prompt, the README's "What it is" and "Similar
* animations", plays the demo on arrival, replays it when a setting changes, runs
- * Pause and CSS slow motion on loop pages, presses Show me on do-it pages and tells
- * the page when the visitor takes over ("hb:input"), and follows reduced motion
- * (Loop and Slow motion greyed out, loops start paused).
+ * Pause and CSS slow motion on loop pages, presses Show me on do-it pages (not on arrival
+ * when the visitor has already acted in the stage) and tells the page when the visitor
+ * 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 pure helpers are exported for tests/demo-page.test.js. */
(function (root, factory) {
@@ -352,12 +353,18 @@
// Do-it pages: the visitor's own press, key, wheel, touch or click inside the stage is sent as "hb:input", so the page
// can stop a Show me run that is under way and leave the visitor in control. Click is there for an activation that comes
// with no pointer or key event (assistive technology); a press sends one for the press and then one for the click.
+ // The same input, or focus arriving in the stage (a Tab into a field), also means the visitor got there before the Show me
+ // press on arrival, and that press is then skipped, so it cannot wipe or replace what they are doing.
+ var visitorActed = false;
function setUpVisitorInput() {
['pointerdown', 'keydown', 'wheel', 'touchstart', 'click'].forEach(function (type) {
stage.addEventListener(type, function (e) {
- if (e.isTrusted) doc.dispatchEvent(new win.CustomEvent('hb:input', { detail: { type: type } }));
+ if (!e.isTrusted) return;
+ visitorActed = true;
+ doc.dispatchEvent(new win.CustomEvent('hb:input', { detail: { type: type } }));
}, { capture: true, passive: true });
});
+ stage.addEventListener('focusin', function (e) { if (e.isTrusted) visitorActed = true; });
}
// Scroll pages: Play scrolls the box from where it is to its end at a steady speed (the whole box in about six
@@ -458,7 +465,7 @@
else if (reduce && reduce.addListener) reduce.addListener(followReducedMotion);
if (doc.body.hasAttribute('data-hb-autoplay')) {
win.setTimeout(function () {
- if (demoCtl) { if (!(reduce && reduce.matches)) demoCtl.click(); return; }
+ if (demoCtl) { if (!(reduce && reduce.matches) && !visitorActed) demoCtl.click(); return; }
if (scrollCtl) { if (!(reduce && reduce.matches)) scrollCtl.click(); return; }
if (loopCtl && !(reduce && reduce.matches)) {
// After Back or a reload a browser can bring Loop back already on; press Replay so the demo still starts.
diff --git a/tests/demo-page.test.js b/tests/demo-page.test.js
index a4db01c0..f9ac0fd6 100644
--- a/tests/demo-page.test.js
+++ b/tests/demo-page.test.js
@@ -1,4 +1,4 @@
-// Unit tests for the pure helpers in assets/js/demo-page.js.
+// Unit tests for the pure helpers in assets/js/demo-page.js, and for boot() run against a stand-in page.
// Run from the repo root: node --test "tests/*.test.js"
const test = require('node:test');
const assert = require('node:assert/strict');
@@ -100,3 +100,77 @@ test('motionNote says what reduced motion changes in the player bar', () => {
assert.equal(DP.motionNote({ pause: true }), 'It starts paused because your device is set to reduce motion.');
assert.equal(DP.motionNote({}), '');
});
+
+// boot() takes the document and window it works on, so a stand-in page with just enough DOM can stand for a real one. It holds
+// a stage and, by kind, the buttons boot() looks for. Events are sent to the stage by hand (with isTrusted set as a browser
+// would), timers wait in a list until the test runs them, and every hb:input the script sends is kept.
+function standInPage(kind, reduced) {
+ const timers = [], sent = [];
+ const node = () => ({
+ attrs: {}, handlers: {}, children: [], clicks: 0, textContent: '', innerHTML: '', classList: { add() {}, remove() {}, toggle() {} },
+ addEventListener(type, fn) { (this.handlers[type] = this.handlers[type] || []).push(fn); },
+ emit(type, event) { (this.handlers[type] || []).forEach(fn => fn(event)); },
+ setAttribute(name, value) { this.attrs[name] = String(value); },
+ getAttribute(name) { return name in this.attrs ? this.attrs[name] : null; },
+ hasAttribute(name) { return name in this.attrs; },
+ removeAttribute(name) { delete this.attrs[name]; },
+ appendChild(child) { this.children.push(child); return child; },
+ insertAdjacentElement() {}, contains(other) { return other === this; }, click() { this.clicks++; }
+ });
+ const stage = node(), player = node(), prompt = node(), demo = node(), play = node(), top = node();
+ const wanted = { '.hb-prompt': prompt, '.stage': stage, '.hb-player': player, '[data-hb-demo]': kind === 'do' && demo,
+ '[data-hb-autoscroll]': kind === 'scroll' && play, '[data-hb-top]': kind === 'scroll' && top };
+ const page = { querySelector: selector => wanted[selector] || null };
+ const body = node();
+ body.setAttribute('data-hb-kind', kind);
+ body.setAttribute('data-hb-autoplay', '');
+ const doc = { body, querySelector: selector => (selector === '.hb-page' ? page : null), createElement: node, dispatchEvent: event => sent.push(event) };
+ const win = {
+ matchMedia: () => ({ matches: !!reduced, addEventListener() {} }),
+ setTimeout: (fn, ms) => timers.push({ fn, ms }),
+ clearTimeout() {}, location: { protocol: 'http:' }, console,
+ CustomEvent: class { constructor(type, init) { this.type = type; this.detail = init && init.detail; } }
+ };
+ DP.boot(doc, win);
+ return { stage, player, demo, sent, runTimers: () => timers.forEach(t => t.fn()), timerDelays: () => timers.map(t => t.ms) };
+}
+const visitor = { isTrusted: true }, script = { isTrusted: false };
+
+test('a do-it page presses Show me about 400 ms after load when nobody has acted', () => {
+ const page = standInPage('do', false);
+ assert.ok(page.timerDelays().includes(400));
+ page.runTimers();
+ assert.equal(page.demo.clicks, 1);
+});
+
+test('the Show me press on arrival is skipped after the visitor pressed, keyed, scrolled or touched the stage', () => {
+ for (const type of ['pointerdown', 'keydown', 'wheel', 'touchstart', 'click']) {
+ const page = standInPage('do', false);
+ page.stage.emit(type, visitor);
+ page.runTimers();
+ assert.equal(page.demo.clicks, 0, type);
+ assert.deepEqual(page.sent.map(e => e.type), ['hb:input'], type);
+ }
+});
+
+test('the Show me press on arrival is skipped after focus arrives in the stage, and focus sends no hb:input', () => {
+ const page = standInPage('do', false);
+ page.stage.emit('focusin', visitor);
+ page.runTimers();
+ assert.equal(page.demo.clicks, 0);
+ assert.equal(page.sent.length, 0);
+});
+
+test('input and focus that the page made itself do not count as the visitor acting', () => {
+ const page = standInPage('do', false);
+ for (const type of ['pointerdown', 'keydown', 'wheel', 'touchstart', 'click', 'focusin']) page.stage.emit(type, script);
+ page.runTimers();
+ assert.equal(page.demo.clicks, 1);
+ assert.equal(page.sent.length, 0);
+});
+
+test('a do-it page presses nothing on arrival under reduced motion', () => {
+ const page = standInPage('do', true);
+ page.runTimers();
+ assert.equal(page.demo.clicks, 0);
+});