4
0

motion.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* lynx-share.com: motion. Loaded last on every page; every page reads the same without it.
  2. The inline script in <head> sets html.mo unless the visitor asked for reduced motion (or ?shot, or the browser has
  3. no IntersectionObserver). Entrances at the top of a page are plain CSS animations (site.css, "motion"). This script
  4. adds what CSS cannot: reveals further down (.rv is put only on what is still below the fold, so nothing a visitor
  5. can see is ever hidden and re-shown), numbers that count up once, the glass header, and tilt + glare on screenshots
  6. for mouse pointers. Only IntersectionObserver and pointer events: no scroll handlers. */
  7. (() => {
  8. const html = document.documentElement, $$ = (s, r) => [...(r || document).querySelectorAll(s)];
  9. if (!html.classList.contains('mo')) return;
  10. /* what reveals, in document order; grids reveal child by child (staggered), everything else as one block */
  11. const GRIDS = '.shots,.whyrow,.frow,.cols,.media,.dlgrid,.policy,.fixes';
  12. const REVEAL = '.sec>.wrap:not(.split)>:not(' + GRIDS + ',script),.split>*,.shots>figure,.whyrow>*,.frow>*,.cols>div,.media>*,.dlos,.policy>div,' +
  13. '.flow>li,.rel>header,.rel>div,.wcard,.faq section,.ctype,.cgroup>h3,.pc,.fx';
  14. const DUR = 450, STEP = 45, MAXSTEP = 7;
  15. function start(){
  16. /* glass header once the page has scrolled: a marker at the very top leaves the viewport */
  17. const top = document.querySelector('.top');
  18. if (top){
  19. const mark = document.createElement('i');
  20. mark.style.cssText = 'position:absolute;top:0;left:0;width:1px;height:10px;pointer-events:none';
  21. document.body.prepend(mark);
  22. new IntersectionObserver(e => top.classList.toggle('scrolled', !e[0].isIntersecting)).observe(mark);
  23. html.classList.add('hd');
  24. }
  25. /* reveals */
  26. const done = el => { el.classList.remove('rv', 'rv-l', 'in'); el.style.removeProperty('--d'); };
  27. const io = new IntersectionObserver(es => {
  28. let n = 0;
  29. es.forEach(e => {
  30. const el = e.target;
  31. if (!e.isIntersecting && e.boundingClientRect.top > 0) return; // still below
  32. io.unobserve(el);
  33. if (!e.isIntersecting) return done(el); // jumped past it: just show it
  34. const d = Math.min(n++, MAXSTEP) * STEP;
  35. el.style.setProperty('--d', d + 'ms'); el.classList.add('in');
  36. setTimeout(done, d + DUR + 40, el);
  37. });
  38. }, {rootMargin: '0px 0px -5% 0px'});
  39. const fold = innerHeight * .92;
  40. $$(REVEAL).forEach(el => {
  41. if (el.getBoundingClientRect().top < fold || el.parentElement.closest('.rv')) return;
  42. el.classList.add('rv'); if (el.matches('.rel>header')) el.classList.add('rv-l');
  43. io.observe(el);
  44. });
  45. /* for scripts that change what is shown (the catalogue filter): show everything inside root now */
  46. window.LYNX_MOTION = {flush: root => $$('.rv', root).forEach(el => { io.unobserve(el); done(el); })};
  47. /* numbers count up once: <b data-count="130">130</b>, or a sentence that holds the number */
  48. const count = el => {
  49. const to = el.dataset.count, re = new RegExp('(^|\\D)' + to + '(?!\\d)'), walk = document.createTreeWalker(el, 4);
  50. for (let n; (n = walk.nextNode());){
  51. const m = re.exec(n.data); if (!m) continue;
  52. const at = m.index + m[1].length, a = n.data.slice(0, at), b = n.data.slice(at + to.length);
  53. const lo = Math.max(Math.pow(10, to.length - 1), Math.round(to * .3)), t0 = performance.now(); // same digit count: no layout shift
  54. if (lo >= +to) return;
  55. const step = t => { const p = Math.min(1, (t - t0) / 900); n.data = a + Math.round(lo + (to - lo) * (1 - Math.pow(1 - p, 3))) + b; if (p < 1) requestAnimationFrame(step); };
  56. return requestAnimationFrame(step);
  57. }
  58. };
  59. const co = new IntersectionObserver(es => es.forEach(e => { if (e.isIntersecting){ co.unobserve(e.target); count(e.target); } }), {threshold: .6});
  60. $$('[data-count]').forEach(el => co.observe(el));
  61. /* muted looping clips (<video class="lp" preload="none">): they play only while on screen, so nothing is fetched
  62. before they scroll into view; visitors who asked for reduced motion (no html.mo) keep the still poster */
  63. const lo = new IntersectionObserver(es => es.forEach(e => {
  64. const v = e.target;
  65. if (e.isIntersecting){ const p = v.play(); if (p && p.catch) p.catch(() => {}); } else v.pause();
  66. }), {rootMargin: '100px 0px'});
  67. $$('video.lp').forEach(v => lo.observe(v));
  68. /* the hero's glow rests while the hero is off screen */
  69. const hero = document.querySelector('.hero');
  70. if (hero) new IntersectionObserver(e => hero.classList.toggle('off', !e[0].isIntersecting)).observe(hero);
  71. /* wiki sidebar: a group's links slide in when it opens */
  72. document.addEventListener('toggle', e => {
  73. const d = e.target;
  74. if (d.open && d.closest && d.closest('.wnav')){ d.classList.remove('op'); void d.offsetWidth; d.classList.add('op'); }
  75. }, true);
  76. /* tilt + glare on the hero picture and the screenshots: mouse only, one frame at a time */
  77. if (matchMedia('(hover:hover) and (pointer:fine)').matches){
  78. let cur = null, ev = null, raf = 0;
  79. const apply = () => {
  80. raf = 0; if (!cur) return;
  81. const r = cur.getBoundingClientRect(), x = (ev.clientX - r.left) / r.width - .5, y = (ev.clientY - r.top) / r.height - .5, s = cur.style;
  82. s.setProperty('--ry', (x * 5).toFixed(2) + 'deg'); s.setProperty('--rx', (-y * 5).toFixed(2) + 'deg');
  83. s.setProperty('--gx', Math.round(x * r.width) + 'px'); s.setProperty('--gy', Math.round(y * r.height) + 'px');
  84. };
  85. document.addEventListener('pointermove', e => {
  86. let el = e.pointerType === 'mouse' && e.target.closest ? e.target.closest('.screen,.shot') : null;
  87. if (el && el.closest('.wiki,.lightbox')) el = null; // wiki articles stay calm
  88. if (el !== cur){
  89. if (cur){ cur.classList.remove('tilt'); cur.style.removeProperty('--rx'); cur.style.removeProperty('--ry'); }
  90. cur = el; if (cur) cur.classList.add('tilt');
  91. }
  92. if (cur){ ev = e; raf = raf || requestAnimationFrame(apply); }
  93. }, {passive: true});
  94. }
  95. /* smooth anchor scrolling, but not for the jump to an address's own #anchor while the page loads */
  96. const smooth = () => setTimeout(() => html.classList.add('sm'), 400);
  97. document.readyState === 'complete' ? smooth() : addEventListener('load', smooth);
  98. }
  99. /* another language: wait until its text is in (the page is hidden until then), so the layout measured is the final one */
  100. if (html.classList.contains('i18n-wait')) document.addEventListener('lynx:lang', start, {once: true}); else start();
  101. })();