/* The shine, travelling down the page. * * Every pressable thing gets the sweep, but they do not all light at once. * Elements are grouped into rows by where they actually sit on the page, and * each row starts a beat after the one above it — so the light walks down the * page and a person waits for it to reach the thing they are looking at. * Within a row it runs left to right, the way the card game does it. * * The starter pack is left alone: it has its own shine already. */ (function () { if (matchMedia('(prefers-reduced-motion: reduce)').matches) return; /* What takes the sweep. The public site's list is the default; Backstage sets window.NEU_SHINE before this loads to name its own, because the two have no classes in common and neither should carry the other's. */ var CFG = window.NEU_SHINE || {}; var CARDS = (CFG.cards || [ '.acard', // chapter page cards '.rcard', // the film reel '.ocard', // orbit cards '.clogo-wrap', // credential logos ]).join(','); var BUTTONS = (CFG.buttons || [ '.btn', '.gpick__btn', '.rail__arrow', '.reelopen__go', '.herowatch', '.sectmore', ]).join(','); /* The headlines. They take a different treatment from the cards — the light runs through the letters rather than across a box — so they are listed separately and tagged with their own class. */ var TITLES = (CFG.titles || [ '.slide__title', // every section heading on the main page '.herotitle', // "YOUR WORLD, UNDERSTOOD BY AI." '.overtitle', '.grain__heading', '.pack__title', '.pack__h', '.blk__h', '.chero__h', // the opening of every chapter page '.rail__title', '.founders__h', '.awards__h', '.awards__sub', '.demos__h', '.jobs__h', '.jobs__h2', '.prow__title', ]).join(','); var SKIP = CFG.skip || ['.pack', '.tut']; /* how long a row waits behind the row above it, and how long a card waits behind the card to its left */ var ROW_STEP = 0.42; var COL_STEP = 0.11; function skipped(el) { for (var i = 0; i < SKIP.length; i++) if (el.closest(SKIP[i])) return true; return false; } function apply(list, cls) { if (!list) return []; var els = Array.prototype.slice.call(document.querySelectorAll(list)) .filter(function (el) { return !skipped(el); }); if (!els.length) return []; /* group by vertical position — a "row" is anything whose top is within half a card of the row's first member, which survives ragged layouts */ var withPos = els.map(function (el) { var r = el.getBoundingClientRect(); return { el: el, top: r.top + window.scrollY, left: r.left, h: r.height }; }).sort(function (a, b) { return a.top - b.top || a.left - b.left; }); var rows = []; withPos.forEach(function (item) { var row = rows[rows.length - 1]; if (row && Math.abs(item.top - row.top) < Math.max(40, item.h * 0.5)) { row.items.push(item); } else { rows.push({ top: item.top, items: [item] }); } }); rows.forEach(function (row, ri) { row.items.sort(function (a, b) { return a.left - b.left; }); row.items.forEach(function (item, ci) { item.el.classList.add('shine'); // only give it a positioning context if it does not already have one if (getComputedStyle(item.el).position === 'static') item.el.classList.add('shine--rel'); if (cls) item.el.classList.add(cls); item.el.style.setProperty('--shine-delay', (ri * ROW_STEP + ci * COL_STEP).toFixed(2) + 's'); }); }); return els; } /* the headlines: same staggering, different class, different gate */ function applyTitles() { if (!TITLES) return []; var els = Array.prototype.slice.call(document.querySelectorAll(TITLES)) .filter(function (el) { return !skipped(el); }); els.forEach(function (el, i) { el.classList.add('tshine'); /* the band is a second copy of the words drawn over the real ones, and it reads them from here — the CSS cannot get at an element's text */ el.setAttribute('data-shine', el.textContent.replace(/\s+/g, ' ').trim()); if (getComputedStyle(el).position === 'static') el.classList.add('tshine--rel'); /* headlines are one to a section, so they stagger down the page rather than across a row — and they are slow, because a headline sweeping as often as a button is a headline nobody can read */ if (!el.style.getPropertyValue('--shine-delay')) { el.style.setProperty('--shine-delay', (i * 0.55 % 4.4).toFixed(2) + 's'); } }); return els; } function run() { var all = apply(CARDS, '').concat(apply(BUTTONS, 'shine--btn')); var titles = applyTitles(); /* only the ones on screen actually animate */ if (!window.IntersectionObserver) { all.forEach(function (el) { el.classList.add('is-lit'); }); titles.forEach(function (el) { el.classList.add('is-shining'); }); return; } /* Light whatever is already on screen straight away, before the observer is asked. The observer normally delivers a first callback of its own, but a tab that is throttled or was never focused may not get one for a long time — and until it does, nothing on the page shines at all. This is one bounding-box read per element and it removes that dependency. */ function inView(el) { var r = el.getBoundingClientRect(); return r.bottom > -80 && r.top < (window.innerHeight || 0) + 80; } all.forEach(function (el) { if (inView(el)) el.classList.add('is-lit'); }); titles.forEach(function (el) { if (inView(el)) el.classList.add('is-shining'); }); var delivered = false; var io = new IntersectionObserver(function (entries) { delivered = true; entries.forEach(function (e) { e.target.classList.toggle('is-lit', e.isIntersecting); }); }, { rootMargin: '80px' }); all.forEach(function (el) { io.observe(el); }); /* `is-lit` on a headline already means "the cursor is on it" — the torch reads that class. The headlines get a gate of their own. */ var tio = new IntersectionObserver(function (entries) { delivered = true; entries.forEach(function (e) { e.target.classList.toggle('is-shining', e.isIntersecting); }); }, { rootMargin: '80px' }); titles.forEach(function (el) { tio.observe(el); }); /* AND A FALLBACK, FOR WHEN THE OBSERVER STAYS SILENT. Some engines throttle IntersectionObserver in a tab that has never been focused, and it can go a long time without delivering anything. When that happens only what was on screen at load ever shines and the rest of the page is dead. This does the same job off the scroll event, and it switches itself off the moment the observer proves it is working. */ var ticking = false; window.addEventListener('scroll', function () { if (delivered || ticking) return; ticking = true; setTimeout(function () { ticking = false; if (delivered) return; all.forEach(function (el) { el.classList.toggle('is-lit', inView(el)); }); titles.forEach(function (el) { el.classList.toggle('is-shining', inView(el)); }); }, 200); }, { passive: true }); } /* Wait for layout to settle, so the row grouping is measured against real positions rather than a half-built page — but on a timer, not a frame. A tab opened in the background never paints, and waiting on a frame there meant the shine was simply never set up. */ var started = false; function start() { if (started) return; started = true; setTimeout(run, 60); } if (document.readyState === 'complete') start(); else window.addEventListener('load', start); // and re-group after a resize, since the rows will have moved var rez; window.addEventListener('resize', function () { clearTimeout(rez); rez = setTimeout(function () { document.querySelectorAll('.shine, .tshine').forEach(function (el) { el.style.removeProperty('--shine-delay'); }); run(); }, 300); }); /* ---- the kick a control gives as it opens something ---- */ var POP = (CFG.cards || []).concat(CFG.buttons || []).join(',') || '.btn, .acard, .rcard, .ocard, .gpick__btn, .reelopen__go, .clogo-wrap'; document.addEventListener('pointerdown', function (e) { var hit = e.target.closest(POP); if (!hit || hit.classList.contains('is-popping')) return; hit.classList.remove('is-popping'); void hit.offsetWidth; hit.classList.add('is-popping'); setTimeout(function () { hit.classList.remove('is-popping'); }, 440); }, { passive: true }); })();