/* THE USHER * * A row of cards asks a stranger to choose before they know what any of it is. * A salesperson would not do that: they point at one thing, say what it is, * then point at the next. This walks each row of cards left to right — scan, * lift, settle, next — so somebody who has never heard of NEUON AI is led * through in the order the row was written, rather than left to pick. * * Three rules keep it a guide rather than a nuisance: * * ONE ROW AT A TIME. Whichever row is nearest the middle of the screen, and * only that one. Eight rows all pointing at once is a fairground. * * IT STOPS WHEN THEY TAKE OVER. Touch the row, swipe it, work the arrows or * tab into it, and the usher goes quiet for a while. Being led by the elbow * after you have started looking for yourself is irritating. * * IT DOES NOT DRAG THE PAGE ABOUT. It will nudge the row sideways to bring * the card it is pointing at into view, because pointing at something off * screen is pointless — but it never touches the page's own scroll. */ (function () { if (matchMedia('(prefers-reduced-motion: reduce)').matches) return; var HOLD = 900; // how long one card is lit — matches the CSS animation var GAP = 240; // a beat between cards var REST = 4200; // and a longer one at the end of a row before going again var QUIET = 7000; // how long to leave them alone after they touch it var rails = []; function setup() { document.querySelectorAll('[data-crail]').forEach(function (el) { var track = el.querySelector('[data-crailtrack]'); if (!track) return; var cards = Array.prototype.slice.call(track.querySelectorAll('.acard')); if (cards.length < 2) return; // one card is not a tour rails.push({ el: el, track: track, cards: cards, i: 0, quietUntil: 0, lit: null, selfScroll: false }); }); if (!rails.length) return; /* Anything the person does to a row hands it to them. `scroll` covers the arrows and a swipe, pointerdown a tap, focusin the keyboard. */ rails.forEach(function (r) { function hush() { /* the usher nudges the row itself, and that fires `scroll` too — if that counted as the person taking over it would shut itself up on its own move, one card in */ if (r.selfScroll) return; r.quietUntil = Date.now() + QUIET; clear(r); } r.track.addEventListener('scroll', hush, { passive: true }); r.track.addEventListener('pointerdown', hush, { passive: true }); r.el.addEventListener('focusin', hush); /* deliberately NOT pointerover: a cursor crossing the row on its way somewhere else is not somebody taking over, and treating it as such meant a mouse parked anywhere near a row silenced it for good */ }); tick(); } function clear(r) { if (r.lit) { r.lit.classList.remove('is-usher'); r.lit = null; } } /* the row nearest the middle of the screen, if any of it is on screen */ function nearest() { var mid = window.innerHeight / 2, best = null, bestD = Infinity; for (var i = 0; i < rails.length; i++) { var r = rails[i], b = r.el.getBoundingClientRect(); if (b.bottom < 40 || b.top > window.innerHeight - 40) continue; var d = Math.abs((b.top + b.bottom) / 2 - mid); if (d < bestD) { bestD = d; best = r; } } return best; } var current = null, timer = null; function tick() { var next = nearest(); /* the row we were on has scrolled away, or another is now more central */ if (current && current !== next) { clear(current); current = null; } current = next; if (!current) { schedule(400); return; } if (Date.now() < current.quietUntil) { clear(current); schedule(600); return; } var r = current; clear(r); if (r.i >= r.cards.length) { r.i = 0; schedule(REST); return; } var card = r.cards[r.i++]; r.lit = card; card.classList.add('is-usher'); bring(r, card); schedule(HOLD + GAP, function () { if (r.lit === card) { card.classList.remove('is-usher'); r.lit = null; } }); } function schedule(ms, before) { clearTimeout(timer); timer = setTimeout(function () { if (before) before(); tick(); }, ms); } /* Bring the card into the row's own view if it is off the end. This scrolls the TRACK, never the page — a row that yanks the whole window about while somebody is reading something else is the fastest way to make a person leave. */ function bring(r, card) { var t = r.track.getBoundingClientRect(); var c = card.getBoundingClientRect(); if (c.left >= t.left - 4 && c.right <= t.right + 4) return; // already there var from = r.track.scrollLeft; var to = from + (c.left - t.left) - 24; r.selfScroll = true; try { r.track.scrollTo({ left: to, behavior: 'smooth' }); } catch (e) { r.track.scrollLeft = to; } /* A smooth scroll is driven by animation frames, and a tab that is not being painted never gets one — the request is accepted and simply never happens. On a phone, where one card fills the row, that left the usher pointing at cards nobody could see. So: check, and if nothing moved, jump. Also covers a smooth scroll cancelled by something else. */ clearTimeout(r.checkTimer); r.checkTimer = setTimeout(function () { if (Math.abs(r.track.scrollLeft - from) < 2 && Math.abs(to - from) > 8) { r.track.scrollLeft = to; } }, 420); /* long enough for a smooth scroll to finish firing its events, short enough that a real swipe a moment later still counts */ clearTimeout(r.selfTimer); r.selfTimer = setTimeout(function () { r.selfScroll = false; }, 1000); } /* Wait for layout, and on a timer rather than a frame: a tab that has never painted never gets a frame, and the row positions would never be read. */ if (document.readyState === 'complete') setTimeout(setup, 200); else window.addEventListener('load', function () { setTimeout(setup, 200); }); })();