Repository navigation
Expand file tree
/
Copy pathmotion.js
More file actions
1303 lines (1238 loc) · 60.3 KB
/
Copy pathmotion.js
File metadata and controls
1303 lines (1238 loc) · 60.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* AI Literacy Foundation: motion.js
- Scroll-triggered reveal via IntersectionObserver
- Count-up for status numbers
- Rotating word in hero headlines
- Header scroll-state toggle
- Mobile nav: close on link click + close on Escape
- Respects prefers-reduced-motion
*/
(function () {
'use strict';
var prefersReduced = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
// Welcome / full-view toggle: when ?view=full is in the URL on mobile,
// hide the mobile-landing and show the rich desktop-home content.
try {
if ((new URLSearchParams(window.location.search)).get('view') === 'full') {
document.body.classList.add('view-full');
}
} catch (e) { /* old browser, ignore */ }
// Mobile nav extras: close menu when a nav link is tapped, close on Escape
var navEl = document.getElementById('nav');
var navToggle = document.getElementById('navToggle');
if (navEl && navToggle) {
// Inject EN/DA language toggle into the mobile nav (only visible when nav opens on mobile).
// Not on the error page: it is served for any missing address, so its "twin" is missing too.
if (!document.querySelector('.error-page')) try {
var path = window.location.pathname;
var isDa = path.indexOf('/da/') === 0 || path === '/da' || path.indexOf('/da/') !== -1;
// Compute partner URL: EN <-> DA mirror
var partnerHref;
if (isDa) {
partnerHref = path.replace('/da/', '/');
if (partnerHref === '/') partnerHref = '/index.html';
} else {
// EN -> DA. Strip leading slash, prepend /da/
var rel = path.replace(/^\//, '');
if (rel === '' || rel === 'index.html') rel = 'index.html';
partnerHref = '/da/' + rel;
}
var langWrap = document.createElement('div');
langWrap.className = 'nav-lang-mobile';
langWrap.setAttribute('role', 'group');
langWrap.setAttribute('aria-label', isDa ? 'Sprog' : 'Language');
// Active language is a <span>, not a dead-link <a href="#">. Prevents browser navigation,
// hash-pollution in the URL, and a confusing focus target for keyboard users.
langWrap.innerHTML = isDa
? '<a href="' + partnerHref + '">EN</a><span aria-hidden="true">·</span><span class="active" aria-current="page">DA</span>'
: '<span class="active" aria-current="page">EN</span><span aria-hidden="true">·</span><a href="' + partnerHref + '">DA</a>';
// Keep the visitor's place across the switch: ?view=full and #section survive it.
var langLink = langWrap.querySelector('a');
var syncLang = function () {
if (langLink) langLink.setAttribute('href', partnerHref + window.location.search + window.location.hash);
};
syncLang();
/* the section can change after load (an in-page link), so refresh it then and on use */
window.addEventListener('hashchange', syncLang);
if (langLink) langLink.addEventListener('click', syncLang, true);
navEl.appendChild(langWrap);
} catch (e) { /* ignore */ }
// The menu sits before its toggle in the page, so after opening it Tab would skip it.
// Move focus to the first item (only for a keyboard press; a tap leaves focus alone).
navToggle.setAttribute('aria-controls', navEl.id || 'nav');
// The icon becomes a close cross while the menu is open (it used to turn sideways).
var togglePath = navToggle.querySelector('path');
var syncIcon = function () {
var open = navToggle.getAttribute('aria-expanded') === 'true';
togglePath.setAttribute('d', open ? 'M6 6l12 12M18 6L6 18' : 'M4 7h16M4 12h16M4 17h16');
};
if (togglePath) {
syncIcon(); // the menu may already be open if it was tapped before this script ran
if ('MutationObserver' in window) new MutationObserver(syncIcon).observe(navToggle, { attributes: true, attributeFilter: ['aria-expanded'] });
}
navToggle.addEventListener('click', function (e) {
if (e.detail !== 0) return; // a mouse or touch click; keyboard activation reports 0
// Focus moves once the menu is really visible (with reduced motion that takes a frame or two).
var tries = 0;
requestAnimationFrame(function go() {
if (!navEl.classList.contains('is-open')) return;
var first = navEl.querySelector('a');
if (!first) return;
if (getComputedStyle(first).visibility === 'visible' || ++tries > 20) { first.focus(); return; }
requestAnimationFrame(go);
});
});
navEl.querySelectorAll('a').forEach(function (link) {
link.addEventListener('click', function () {
if (navEl.classList.contains('is-open')) {
navEl.classList.remove('is-open');
navToggle.setAttribute('aria-expanded', 'false');
}
});
});
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && navEl.classList.contains('is-open')) {
navEl.classList.remove('is-open');
navToggle.setAttribute('aria-expanded', 'false');
navToggle.focus();
}
});
// Close the open mobile menu on a tap/click outside it (all pages)
document.addEventListener('click', function (e) {
if (!navEl.classList.contains('is-open')) return;
if (navEl.contains(e.target) || navToggle.contains(e.target)) return;
navEl.classList.remove('is-open');
navToggle.setAttribute('aria-expanded', 'false');
});
}
// Inject back-to-top button (no per-page HTML needed)
var scrollTopBtn = null;
// On phones and tablets the button shows only while the reader scrolls back up, so it never
// sits on the text being read. Laptops keep it in view below the first screens.
var topWide = window.matchMedia ? window.matchMedia('(min-width: 1100px) and (hover: hover) and (pointer: fine)') : null;
var topLastY = window.scrollY || 0, topTurnY = topLastY, topDir = 0;
(function() {
scrollTopBtn = document.createElement('button');
scrollTopBtn.className = 'scroll-top';
scrollTopBtn.type = 'button';
scrollTopBtn.setAttribute('aria-label', (document.documentElement.lang || '').indexOf('da') === 0 ? 'Til toppen' : 'Back to top');
scrollTopBtn.innerHTML = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 19V5M5 12l7-7 7 7"/></svg>';
document.body.appendChild(scrollTopBtn);
scrollTopBtn.addEventListener('click', function(e) {
window.scrollTo({ top: 0, behavior: prefersReduced ? 'auto' : 'smooth' });
// The button hides itself at the top, so a keyboard user would lose their place.
// Hand focus to the start of the page content (keyboard activation reports detail 0).
if (e.detail !== 0) return;
var top = document.getElementById('main') || document.querySelector('main');
if (!top) return;
if (!top.hasAttribute('tabindex')) { top.setAttribute('tabindex', '-1'); top.style.outline = 'none'; }
try { top.focus({ preventScroll: true }); } catch (err) { top.focus(); }
});
})();
// Header scroll state + scroll progress bar + chapter indicator + back-to-top
var header = document.querySelector('.site-header');
var progressBar = document.querySelector('.scroll-progress');
var chapter = document.querySelector('.chapter-indicator');
var chapterNum = chapter ? chapter.querySelector('.chapter-indicator__current') : null;
var chapterLabel = chapter ? chapter.querySelector('.chapter-indicator__label') : null;
var chapterSections = chapter ? Array.prototype.slice.call(document.querySelectorAll('[data-chapter]')) : [];
function onScroll() {
if (header) {
if (window.scrollY > 12) header.classList.add('is-scrolled');
else header.classList.remove('is-scrolled');
}
if (progressBar) {
var scrollable = document.documentElement.scrollHeight - window.innerHeight;
var pct = scrollable > 0 ? (window.scrollY / scrollable) * 100 : 0;
progressBar.style.transform = 'scaleX(' + (Math.max(0, Math.min(100, pct)) / 100) + ')';
if (window.scrollY > 80) progressBar.classList.add('is-active');
else progressBar.classList.remove('is-active');
}
if (scrollTopBtn) {
var ty = window.scrollY, tdir = ty > topLastY ? 1 : (ty < topLastY ? -1 : 0);
if (tdir && tdir !== topDir) { topTurnY = topLastY; topDir = tdir; } /* measured from where the direction last turned */
if (ty <= 600) scrollTopBtn.classList.remove('is-visible');
else if (!topWide || topWide.matches || document.activeElement === scrollTopBtn) scrollTopBtn.classList.add('is-visible');
else if (ty - topTurnY < -16) scrollTopBtn.classList.add('is-visible');
else if (ty - topTurnY > 16) scrollTopBtn.classList.remove('is-visible');
topLastY = ty;
}
if (chapter && chapterSections.length) {
var trigger = window.innerHeight * 0.4;
var active = null;
for (var i = 0; i < chapterSections.length; i++) {
var rect = chapterSections[i].getBoundingClientRect();
if (rect.top <= trigger) active = chapterSections[i];
}
if (active) {
chapter.classList.add('is-visible');
var num = active.getAttribute('data-chapter');
var label = active.getAttribute('data-chapter-label') || '';
if (chapterNum && chapterNum.textContent !== num) chapterNum.textContent = num;
if (chapterLabel && chapterLabel.textContent !== label) chapterLabel.textContent = label;
} else {
chapter.classList.remove('is-visible');
}
}
}
onScroll();
window.addEventListener('scroll', onScroll, { passive: true });
window.addEventListener('resize', onScroll, { passive: true });
// ---- Count-up animation ----
function countUp(el) {
var target = parseInt(el.getAttribute('data-count'), 10);
if (isNaN(target) || target === 0) {
el.textContent = target || 0;
return;
}
if (prefersReduced) {
el.textContent = target.toLocaleString();
return;
}
var duration = 1400;
var start = performance.now();
function tick(now) {
if (el.getAttribute('data-count-done')) return; // finished early, for example for printing
var t = Math.min((now - start) / duration, 1);
var eased = 1 - Math.pow(1 - t, 3);
var value = Math.round(target * eased);
el.textContent = value.toLocaleString();
if (t < 1) requestAnimationFrame(tick);
else el.textContent = target.toLocaleString();
}
el.textContent = '0';
requestAnimationFrame(tick);
}
// ---- Rotating word ----
function measureWords(el, words) {
var measurer = document.createElement('span');
var s = getComputedStyle(el);
measurer.style.position = 'absolute';
measurer.style.visibility = 'hidden';
measurer.style.whiteSpace = 'nowrap';
measurer.style.fontFamily = s.fontFamily;
measurer.style.fontSize = s.fontSize;
measurer.style.fontWeight = s.fontWeight;
measurer.style.fontStyle = s.fontStyle;
measurer.style.letterSpacing = s.letterSpacing;
measurer.style.fontVariant = s.fontVariant;
document.body.appendChild(measurer);
var widths = words.map(function (w) {
measurer.textContent = w;
return Math.ceil(measurer.getBoundingClientRect().width);
});
document.body.removeChild(measurer);
return widths;
}
function setupRotator(el) {
var words;
try { words = JSON.parse(el.getAttribute('data-words')); }
catch (e) { return; }
if (!Array.isArray(words) || words.length < 2) return;
// A word at the end of a line keeps the width of the widest word, so nothing reflows on
// a swap. A word in the middle of a sentence ("We think in public.") would leave a gap
// before the rest of the sentence, so there the width follows each word and the rest of
// the line glides to its new place. Measured after the fonts load, again on resize.
var after = el.nextSibling;
var fluid = !!(after && after.nodeType === 3 && /[^\s.,;:!?]/.test(after.textContent));
var widths = null;
var i = 0;
var doLock = function () {
widths = measureWords(el, words);
if (fluid) {
if (prefersReduced) return;
el.classList.add('rotator--fluid');
el.style.width = widths[i] + 'px';
} else {
var max = Math.max.apply(null, widths);
if (max > 0) el.style.minWidth = max + 'px';
}
};
if (document.fonts && document.fonts.ready) {
document.fonts.ready.then(doLock);
} else {
doLock();
}
var resizeTimer = null;
window.addEventListener('resize', function () {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(doLock, 150);
});
if (prefersReduced) return;
var holdMs = 2400; // how long each word holds
var swapMs = 360; // exit duration
var started = false;
function next() {
el.classList.add('is-out');
setTimeout(function () {
i = (i + 1) % words.length;
el.textContent = words[i];
el.classList.remove('is-out');
el.classList.add('is-in');
if (fluid && widths) {
// the slot resizes while the new word is still unseen, then the word fades in
el.style.width = widths[i] + 'px';
setTimeout(function () { el.classList.remove('is-in'); }, 220);
return;
}
requestAnimationFrame(function () {
requestAnimationFrame(function () {
el.classList.remove('is-in');
});
});
}, swapMs);
}
function startCycle() {
if (started) return;
started = true;
// Initial delay lets any hero entrance animation settle
// Reduced motion switched on during the visit holds the current word.
var rq = window.matchMedia('(prefers-reduced-motion: reduce)');
setTimeout(function loop() {
if (!rq.matches) next();
setTimeout(loop, holdMs + swapMs);
}, 1400);
}
if ('IntersectionObserver' in window) {
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (e) {
if (e.isIntersecting) {
startCycle();
io.disconnect();
}
});
}, { threshold: 0.3 });
io.observe(el);
} else {
startCycle();
}
}
document.querySelectorAll('.rotator').forEach(setupRotator);
// ---- Scroll reveal + count-up triggers ----
var revealTargets = document.querySelectorAll('[data-reveal]');
var countTargets = document.querySelectorAll('[data-count]');
if (prefersReduced || !('IntersectionObserver' in window)) {
revealTargets.forEach(function (el) { el.classList.add('is-revealed'); });
countTargets.forEach(function (el) {
var n = parseInt(el.getAttribute('data-count'), 10);
el.textContent = isNaN(n) ? '0' : n.toLocaleString();
});
return;
}
var revealObserver = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
entry.target.classList.add('is-revealed');
revealObserver.unobserve(entry.target);
}
});
}, { rootMargin: '0px 0px -8% 0px', threshold: 0 });
/* threshold 0: reveal as soon as the element crosses the line. A share of the element
(it was 8%) can never be reached by a section many screens tall, which left the whole
Programs section blank on small and landscape phones. */
revealTargets.forEach(function (el) { revealObserver.observe(el); });
var countObserver = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
countUp(entry.target);
countObserver.unobserve(entry.target);
}
});
}, { threshold: 0.5 });
// Start every counter at 0 straight away. Its section reveals earlier than the counter
// triggers, so the final figure used to show for a moment, snap to 0 and count up again.
// Without JavaScript the real figures stay in the page.
countTargets.forEach(function (el) {
var n = parseInt(el.getAttribute('data-count'), 10);
if (!isNaN(n) && n > 0) el.textContent = '0';
countObserver.observe(el);
});
// Printing shows every section without scrolling, so the counters must print their real figures.
window.addEventListener('beforeprint', function () {
countTargets.forEach(function (el) {
var n = parseInt(el.getAttribute('data-count'), 10);
el.setAttribute('data-count-done', '1');
el.textContent = isNaN(n) ? '0' : n.toLocaleString();
countObserver.unobserve(el);
});
});
})();
/* ===== MEMO SHARE: copy-link fallback for Instagram (and anywhere) ===== */
(function () {
'use strict';
document.querySelectorAll('.memo-share__link[data-copy]').forEach(function (btn) {
btn.addEventListener('click', function (e) {
e.preventDefault();
var url = btn.getAttribute('data-copy');
var tip = btn.querySelector('.memo-share__tooltip');
var done = function () {
if (!tip) return;
tip.classList.add('is-visible');
setTimeout(function () { tip.classList.remove('is-visible'); }, 1800);
};
// Older route, also the fallback when the clipboard API refuses (in-app browsers).
// "Copied" shows only when the copy really happened.
var legacy = function () {
var ta = document.createElement('textarea');
ta.value = url; ta.setAttribute('readonly', '');
ta.style.position = 'absolute'; ta.style.left = '-9999px';
document.body.appendChild(ta); ta.select();
var copied = false;
try { copied = document.execCommand('copy'); } catch (err) { /* silent */ }
document.body.removeChild(ta);
if (copied) done();
};
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(url).then(done).catch(legacy);
} else {
legacy();
}
});
});
})();
/* Edge field (desktop only).
Etched contour lines with stippled shading in the outer margins, never over the
reading column. Drifts slowly, flows with scroll, ripples around the cursor.
Where the hero leaves its right side empty, the same plate widens into that
space, and the cursor there raises a ring of warm lines.
Off on touch, narrow screens, Save-Data and print; one still frame under
reduced motion. Self-contained: a failure here cannot affect anything else. */
(function () {
'use strict';
try {
if (!window.matchMedia || !window.requestAnimationFrame) return;
var conn = navigator.connection;
if (conn && conn.saveData) return;
var mq = window.matchMedia('(min-width: 1100px) and (hover: hover) and (pointer: fine)');
var rq = window.matchMedia('(prefers-reduced-motion: reduce)');
/* 2D simplex noise (after Gustavson), seeded so the plate is the same on every visit. */
var perm = new Uint8Array(512);
var GX = [1, -1, 1, -1, 1, -1, 0, 0], GY = [1, 1, -1, -1, 0, 0, 1, -1];
(function () {
var p = new Uint8Array(256), i, j, tmp, s = 20260427;
for (i = 0; i < 256; i++) p[i] = i;
for (i = 255; i > 0; i--) {
s = (Math.imul(s, 1664525) + 1013904223) >>> 0;
j = s % (i + 1); tmp = p[i]; p[i] = p[j]; p[j] = tmp;
}
for (i = 0; i < 512; i++) perm[i] = p[i & 255];
})();
var F2 = 0.5 * (Math.sqrt(3) - 1), G2 = (3 - Math.sqrt(3)) / 6;
function noise(xin, yin) {
var s = (xin + yin) * F2, i = Math.floor(xin + s), j = Math.floor(yin + s);
var t0 = (i + j) * G2, x0 = xin - (i - t0), y0 = yin - (j - t0);
var i1 = x0 > y0 ? 1 : 0, j1 = 1 - i1;
var x1 = x0 - i1 + G2, y1 = y0 - j1 + G2, x2 = x0 - 1 + 2 * G2, y2 = y0 - 1 + 2 * G2;
var ii = i & 255, jj = j & 255, n = 0, g, q;
q = 0.5 - x0 * x0 - y0 * y0;
if (q > 0) { g = perm[ii + perm[jj]] & 7; q *= q; n += q * q * (GX[g] * x0 + GY[g] * y0); }
q = 0.5 - x1 * x1 - y1 * y1;
if (q > 0) { g = perm[ii + i1 + perm[jj + j1]] & 7; q *= q; n += q * q * (GX[g] * x1 + GY[g] * y1); }
q = 0.5 - x2 * x2 - y2 * y2;
if (q > 0) { g = perm[ii + 1 + perm[jj + 1]] & 7; q *= q; n += q * q * (GX[g] * x2 + GY[g] * y2); }
return 70 * n;
}
var CELL = 12, DOT_STEP = 2, PARALLAX = 0.35;
var LEVELS = [-0.7, -0.52, -0.34, -0.16, 0.02, 0.2, 0.38, 0.56, 0.74];
var ACCENT = 4; /* index of the one terracotta line */
var INK = 'rgba(21, 20, 15, 0.16)', CLAY = 'rgba(176, 84, 51, 0.46)', DOTS = 'rgba(21, 20, 15, 0.13)';
/* Hero zone: distance kept from the text, narrowest zone worth drawing, its fades,
and the peak and warm ink under the cursor. */
var GAP = 72, MIN_ZONE = 320, FADE_IN = 120, FADE_TOP = 48, FADE_OUT = 140;
var PEAK_R = 150, SUMMIT = 0.83, RING = 26, WARM_R = 220;
var PULL = 0.55; /* a partial pull: the terrain bends into rings instead of drawing a target */
var TEXT = 'h1, h2, p, a, button, .btn, .hero-mark, .aei-eyebrow, .eyebrow';
/* Home, What We Do, AI Economic Index and Memos, in English and Danish: the heroes
that leave their right side empty. */
var ZONE_PAGES = /^\/(da\/)?((index|work|ai-economic-index|memos)(\.html)?)?$/;
var canvas = null, ctx = null, raf = 0, active = false;
var W = 0, H = 0, dpr = 1, band = 0, cols = 0, rows = 0, half = 0;
var field = null, xs = null;
var t = 0, last = 0, lastScroll = 0;
var printing = false;
var mx = -9999, my = -9999, px = -9999, py = -9999, lens = 0, lensTarget = 0;
var zone = null, zmask = null, watched = null;
var hc = 0, hj0 = 0, hj1 = -1, near = null, zp = null;
var hero = 0, inside = false, shown = false, pending = 0, relay = 0;
function measure() {
W = window.innerWidth; H = window.innerHeight;
dpr = Math.min(window.devicePixelRatio || 1, 1.5);
canvas.width = Math.round(W * dpr); canvas.height = Math.round(H * dpr);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
/* The band fades out just before the left edge of the widest text column on this page. */
var left = W, k, els = document.querySelectorAll('main .wrap, main .wrap--wide, main .wrap--data, main .wrap--narrow');
for (k = 0; k < els.length; k++) {
var r = els[k].getBoundingClientRect();
if (r.width < 240) continue;
var pl = parseFloat(getComputedStyle(els[k]).paddingLeft) || 0;
left = Math.min(left, r.left + pl);
}
if (left === W) left = Math.max(0, (W - 1180) / 2 + 40);
band = Math.max(0, Math.min(460, left - 12));
measureZone();
layout();
}
/* Sample columns: the left band, then the right band, mirrored. On a page with a
hero zone the right band's columns run on leftwards across the zone, so the
lines there join the band's lines exactly. */
function layout() {
var n = Math.ceil(band / CELL) + 1, list = [], k;
hc = 0;
if (zone) {
hc = Math.ceil((W - zone.l) / CELL) - n + 1;
hc += hc & 1; /* an even count keeps the right band's stipple on the same columns */
if (hc < 2) { hc = 0; zone = null; }
}
for (k = 0; k < n; k++) list.push(k * CELL);
for (k = n + hc - 1; k >= 0; k--) list.push(W - k * CELL);
xs = list; cols = list.length; half = n; rows = Math.ceil(H / CELL) + 1;
field = new Float32Array(cols * rows);
}
/* The hero zone: from 72 px right of the hero's text and buttons to the right band,
over the height of the section that holds the h1. Kept in page coordinates, so it
scrolls with the section while the canvas stays fixed. */
function measureZone() {
zone = null;
try { findZone(); } catch (err) { zone = null; } /* without a zone the margins draw as before */
}
function findZone() {
if (!ZONE_PAGES.test(location.pathname)) return;
var hs = document.querySelectorAll('main h1'), h = null, k, r;
for (k = 0; k < hs.length && !h; k++) if (hs[k].getBoundingClientRect().width) h = hs[k];
var sec = h && h.closest ? h.closest('section') : null;
/* Not on Roadmap (its globe sits there) or Partner (its own field). */
if (!sec || sec.classList.contains('hero--globe') || document.querySelector('.pt-hero')) return;
if (sec !== watched && window.ResizeObserver) {
/* the hero changes size when its text reflows: measure again then */
watched = sec;
new ResizeObserver(function () { queue(120); }).observe(sec);
}
var sy = window.scrollY || 0, right = 0, els = sec.querySelectorAll(TEXT);
for (k = 0; k < els.length; k++) {
r = els[k].getBoundingClientRect();
if (!r.width || !r.height) continue;
right = Math.max(right, /^(H1|H2|P)$/.test(els[k].tagName) ? textRight(els[k], r) : r.right);
}
var l = Math.ceil((right + GAP) * dpr) / dpr, end = W - band; /* on a whole device pixel */
if (!right || end - l < MIN_ZONE) return;
r = sec.getBoundingClientRect();
var top = r.top + sy, bot = r.bottom + sy, ht = h.getBoundingClientRect().top + sy;
/* Anything else in the section that reaches into the zone (the stat cards on the
AI Economic Index page) ends it 24 px short. */
els = sec.getElementsByTagName('*');
if (els.length > 400) return;
for (k = 0; k < els.length; k++) {
var e = els[k];
if (e.closest(TEXT) || e.querySelector(TEXT)) continue;
r = e.getBoundingClientRect();
if (!r.width || !r.height || r.right <= l || r.left >= end) continue;
if (r.top + sy >= ht) bot = Math.min(bot, r.top + sy - 24);
else top = Math.max(top, r.bottom + sy + 24);
}
if (bot - top < FADE_TOP + FADE_OUT + 60) return;
zone = { l: l, top: top, bot: bot };
paintZoneMask();
}
/* Right edge of the text itself, not of its box. A rotating word can be wider than
the one showing now, so its line is allowed for the widest word. */
function textRight(el, box) {
var rg = document.createRange(), right = 0, k, rects;
rg.selectNodeContents(el);
rects = rg.getClientRects();
for (k = 0; k < rects.length; k++) if (rects[k].width) right = Math.max(right, rects[k].right);
var rot = el.querySelector('.rotator[data-words]');
if (rot) {
var rr = rot.getBoundingClientRect(), line = rr.right;
for (k = 0; k < rects.length; k++) {
var mid = (rects[k].top + rects[k].bottom) / 2;
if (mid > rr.top && mid < rr.bottom) line = Math.max(line, rects[k].right);
}
line += Math.max(0, widestWord(rot) - rr.width);
right = Math.max(right, Math.min(line, box.right)); /* past the box it wraps, inside the box */
}
return right || box.right;
}
function widestWord(el) {
var words, w = 0, k;
try { words = JSON.parse(el.getAttribute('data-words')); } catch (e) { return 0; }
if (!words || !words.length) return 0;
var m = document.createElement('span'), s = getComputedStyle(el);
m.style.cssText = 'position:absolute;left:0;top:0;visibility:hidden;white-space:nowrap';
m.style.fontFamily = s.fontFamily; m.style.fontSize = s.fontSize; m.style.fontWeight = s.fontWeight;
m.style.fontStyle = s.fontStyle; m.style.letterSpacing = s.letterSpacing;
document.body.appendChild(m);
for (k = 0; k < words.length; k++) { m.textContent = words[k]; w = Math.max(w, m.getBoundingClientRect().width); }
document.body.removeChild(m);
return Math.ceil(w);
}
/* The margin bands' fade as a gradient on context c, shifted left by ox. */
function bandMask(c, ox) {
var e1 = Math.min(0.49, band * 0.45 / W), e2 = Math.min(0.5, band / W);
var g = c.createLinearGradient(-ox, 0, W - ox, 0);
g.addColorStop(0, 'rgba(0,0,0,1)'); g.addColorStop(e1, 'rgba(0,0,0,1)'); g.addColorStop(e2, 'rgba(0,0,0,0)');
g.addColorStop(1 - e2, 'rgba(0,0,0,0)'); g.addColorStop(1 - e1, 'rgba(0,0,0,1)'); g.addColorStop(1, 'rgba(0,0,0,1)');
return g;
}
/* The zone's mask, painted once per measure at full resolution: in over 120 px from
its left edge, in at the top, out over 140 px at the bottom, with the right band's
own fade laid in so the plate joins the band without a seam. */
function paintZoneMask() {
var zw = W - zone.l, zh = zone.bot - zone.top, g;
if (!zmask) zmask = document.createElement('canvas');
zmask.width = canvas.width - Math.round(zone.l * dpr); zmask.height = Math.round(zh * dpr);
var m = zmask.getContext('2d');
m.setTransform(dpr, 0, 0, dpr, 0, 0);
g = m.createLinearGradient(0, 0, FADE_IN, 0);
g.addColorStop(0, 'rgba(0,0,0,0)'); g.addColorStop(1, 'rgba(0,0,0,1)');
m.fillStyle = g; m.fillRect(0, 0, zw + 1, zh + 1);
m.globalCompositeOperation = 'destination-in';
g = m.createLinearGradient(0, 0, 0, zh);
g.addColorStop(0, 'rgba(0,0,0,0)'); g.addColorStop(FADE_TOP / zh, 'rgba(0,0,0,1)');
g.addColorStop(1 - FADE_OUT / zh, 'rgba(0,0,0,1)'); g.addColorStop(1, 'rgba(0,0,0,0)');
m.fillStyle = g; m.fillRect(0, 0, zw + 1, zh + 1);
m.globalCompositeOperation = 'source-over';
m.fillStyle = bandMask(m, zone.l); m.fillRect(0, 0, zw + 1, zh + 1);
}
/* The bands' fade, exactly as drawn without a zone, inside one device-pixel rectangle. */
function fade(x, y, w, h) {
if (w <= 0 || h <= 0) return;
ctx.save(); ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.beginPath(); ctx.rect(x, y, w, h); ctx.clip();
ctx.setTransform(dpr, 0, 0, dpr, 0, 0); ctx.fillRect(0, 0, W, H);
ctx.restore();
}
function sample(scrollY) {
var yo = scrollY * PARALLAX, a = t * 0.010, b = t * 0.006, c = t * 0.014;
var lr2 = 2 * 110 * 110, useLens = lens > 0.002;
var pr2 = 2 * PEAK_R * PEAK_R, usePeak = hero > 0.002, h0 = half, h1 = half + hc;
for (var j = 0; j < rows; j++) {
var y = j * CELL, fy = y + yo, k = j * cols, wide = j >= hj0 && j <= hj1;
for (var i = 0; i < cols; i++) {
if (i === h0 && !wide) i = h1; /* zone columns only in the rows the zone covers */
var x = xs[i];
var v = 0.68 * noise(x / 330 + a, fy / 330 + b) + 0.32 * noise(x / 150 - c + 31.7, fy / 150 + 7.3);
if (useLens) {
var dx = x - px, dy = y - py, d2 = dx * dx + dy * dy;
if (d2 < 9 * lr2) v += 0.6 * lens * Math.exp(-d2 / lr2);
}
if (usePeak) {
/* A cone just above the top level, its sides crossing a level every RING px,
blended into the plate over about PEAK_R: rings that run on into the terrain.
A plain bump would flatten into a plateau above the top level. */
var ex = x - px, ey = y - py, e2 = ex * ex + ey * ey;
if (e2 < 9 * pr2) v += PULL * hero * Math.exp(-e2 / pr2) * (SUMMIT - 0.18 * Math.sqrt(e2) / RING - v);
}
field[k + i] = v;
}
}
}
/* Each segment goes to one of three paths: the canvas path for the margin bands (the
same path as on a page without a zone, so the bands render exactly as before), zp for
the zone, or, while the cursor warms the zone, near for those within reach of it. */
function contour(level) {
var r2 = (WARM_R + 2 * CELL) * (WARM_R + 2 * CELL), z1 = half + hc;
for (var j = 0; j < rows - 1; j++) {
var y0 = j * CELL, y1 = y0 + CELL, wide = j >= hj0 && j < hj1;
for (var i = 0; i < cols - 1; i++) {
if (i === half - 1) continue; /* the gap between the two bands */
if (i === half && hc && !wide) { i += hc - 1; continue; } /* zone cells only where it is on screen */
var x0 = xs[i], x1 = xs[i + 1];
var a = field[j * cols + i], b = field[j * cols + i + 1];
var c = field[(j + 1) * cols + i + 1], d = field[(j + 1) * cols + i];
var idx = (a > level ? 8 : 0) | (b > level ? 4 : 0) | (c > level ? 2 : 0) | (d > level ? 1 : 0);
if (idx === 0 || idx === 15) continue;
var p = near && (x0 - px) * (x0 - px) + (y0 - py) * (y0 - py) < r2 ? near : i >= half && i < z1 ? zp : ctx;
var tx = x0 + (x1 - x0) * (level - a) / (b - a); /* top edge */
var ry = y0 + CELL * (level - b) / (c - b); /* right edge */
var bx = x0 + (x1 - x0) * (level - d) / (c - d); /* bottom edge */
var ly = y0 + CELL * (level - a) / (d - a); /* left edge */
switch (idx) {
case 1: case 14: p.moveTo(x0, ly); p.lineTo(bx, y1); break;
case 2: case 13: p.moveTo(bx, y1); p.lineTo(x1, ry); break;
case 3: case 12: p.moveTo(x0, ly); p.lineTo(x1, ry); break;
case 4: case 11: p.moveTo(tx, y0); p.lineTo(x1, ry); break;
case 5: case 10:
/* Saddle: the asymptotic decider picks which diagonal stays connected. */
if ((a - level) * (c - level) - (b - level) * (d - level) >= 0) {
p.moveTo(tx, y0); p.lineTo(x1, ry); p.moveTo(x0, ly); p.lineTo(bx, y1);
} else {
p.moveTo(x0, ly); p.lineTo(tx, y0); p.moveTo(bx, y1); p.lineTo(x1, ry);
}
break;
case 6: case 9: p.moveTo(tx, y0); p.lineTo(bx, y1); break;
case 7: case 8: p.moveTo(x0, ly); p.lineTo(tx, y0); break;
}
}
}
}
/* scrollY moves the plate (0 for the still frame); pageY places the hero zone. */
function draw(scrollY, pageY) {
ctx.globalCompositeOperation = 'source-over';
ctx.clearRect(0, 0, W, H);
/* The grid rows the zone covers on screen now; none once it has scrolled away. */
var zt = zone ? zone.top - pageY : 0, zb = zone ? zone.bot - pageY : 0, on = zb > 0 && zt < H;
hj0 = on ? Math.max(0, Math.floor(zt / CELL)) : 0;
hj1 = on ? Math.min(rows - 1, Math.ceil(zb / CELL)) : -1;
sample(scrollY);
/* Stipple: dots on the high ground, like the shading on an engraved plate. */
ctx.fillStyle = DOTS;
for (var j = 0; j < rows; j += DOT_STEP) {
var y = j * CELL, wide = j >= hj0 && j <= hj1;
for (var i = 0; i < cols; i += DOT_STEP) {
if (!wide && i >= half && i < half + hc) { i += hc - DOT_STEP; continue; }
var v = field[j * cols + i];
if (v > 0.3) { var s = v > 0.55 ? 1.6 : 1.1; ctx.fillRect(xs[i] - s / 2, y - s / 2, s, s); }
}
}
var warmOn = on && hero > 0.002, P2 = window.Path2D;
near = warmOn && P2 ? new P2() : null;
zp = on && P2 ? new P2() : ctx;
ctx.lineWidth = 0.9; ctx.strokeStyle = warmOn && !near ? warmInk() : INK; ctx.beginPath();
for (var l = 0; l < LEVELS.length; l++) if (l !== ACCENT) contour(LEVELS[l]);
ctx.stroke();
if (zp !== ctx) ctx.stroke(zp);
if (near) { ctx.strokeStyle = warmInk(); ctx.stroke(near); near = null; }
ctx.lineWidth = 1.15; ctx.strokeStyle = CLAY; ctx.beginPath();
zp = on && P2 ? new P2() : ctx;
contour(LEVELS[ACCENT]);
ctx.stroke();
if (zp !== ctx) ctx.stroke(zp);
zp = null;
/* Fade every mark out before it reaches the text. */
ctx.globalCompositeOperation = 'destination-in';
ctx.fillStyle = bandMask(ctx, 0);
if (on) {
/* The bands' fade above, below and left of the zone, the zone's mask inside it,
all on whole device pixels so they meet exactly. Nothing is repainted on scroll. */
var cw = canvas.width, x = cw - zmask.width, y = Math.round(zt * dpr), y1 = y + zmask.height;
fade(0, 0, cw, y);
fade(0, y1, cw, canvas.height - y1);
fade(0, y, x, zmask.height);
ctx.save(); ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.beginPath(); ctx.rect(x, y, zmask.width, zmask.height); ctx.clip();
ctx.drawImage(zmask, x, y);
ctx.restore();
} else ctx.fillRect(0, 0, W, H);
ctx.globalCompositeOperation = 'source-over';
shown = on;
}
/* Ink lines near the cursor warm to terracotta and are back to the normal ink by
WARM_R; the warmth eases in and out with the peak. */
function warmInk() {
var g = ctx.createRadialGradient(px, py, 0, px, py, WARM_R);
g.addColorStop(0, warm(hero)); g.addColorStop(0.5, warm(hero * 0.4)); g.addColorStop(1, INK);
return g;
}
function warm(f) {
return 'rgba(' + Math.round(21 + 155 * f) + ',' + Math.round(20 + 64 * f) + ',' +
Math.round(15 + 36 * f) + ',' + (0.16 + 0.39 * f).toFixed(3) + ')';
}
/* 1 while the cursor is over the zone, short of where the right band's own lens takes over. */
function aim(sy) {
if (!zone || !inside) return 0;
var y = my + sy;
return mx >= zone.l && mx < W - band - 40 && y >= zone.top && y < zone.bot ? 1 : 0;
}
function frame(now) {
raf = 0;
if (!active) return;
raf = requestAnimationFrame(frame);
if (last && now - last < 33) return; /* about 30 frames a second is plenty for ink */
var dt = last ? Math.min((now - last) / 1000, 0.1) : 0;
last = now;
var sy = window.scrollY || 0, dy = Math.abs(sy - lastScroll);
lastScroll = sy;
t += dt + Math.min(dy, 80) * 0.004; /* the plate flows a little faster while you scroll */
lens += (lensTarget - lens) * 0.08;
hero += (aim(sy) - hero) * 0.08; /* checked every frame: the zone moves under a still cursor */
if (px > -9000) { px += (mx - px) * 0.18; py += (my - py) * 0.18; }
draw(sy, sy);
}
function onMove(e) {
if (e.pointerType && e.pointerType !== 'mouse') return;
mx = e.clientX; my = e.clientY; inside = true;
if (px < -9000) { px = mx; py = my; }
lensTarget = (mx < band + 40 || mx > W - band - 40) ? 1 : 0;
}
function onLeave() { lensTarget = 0; inside = false; }
function onResize() { sync(); queue(300); }
function onVis() { if (document.hidden) halt(); else sync(); }
/* Fonts, a resize or a change in the hero's size move its text: measure the zone again.
The canvas keeps its size, so nothing is cleared. */
function relayout() {
relay = 0;
if (!ctx || printing || !mq.matches || canvas.style.display === 'none') return;
measureZone();
layout();
if (!active && rq.matches && !document.hidden) draw(0, window.scrollY || 0);
}
function queue(ms) { clearTimeout(relay); relay = setTimeout(relayout, ms); }
/* Under reduced motion the plate stays still, but the zone's window follows its section. */
function onScroll() {
if (active || pending || !zone || !ctx || !rq.matches || printing || document.hidden || !mq.matches) return;
pending = requestAnimationFrame(function () {
pending = 0;
var y = window.scrollY || 0;
if (zone && (shown || (zone.bot - y > 0 && zone.top - y < H))) draw(0, y);
});
}
function build() {
canvas = document.createElement('canvas');
canvas.className = 'edge-field';
canvas.setAttribute('aria-hidden', 'true');
canvas.style.cssText = 'position:fixed;top:0;left:0;width:100%;height:100%;z-index:-1;pointer-events:none;opacity:0;transition:opacity 1.6s ease';
var st = document.createElement('style');
st.textContent = '@media print{.edge-field{display:none!important}}';
document.head.appendChild(st);
document.body.appendChild(canvas);
ctx = canvas.getContext('2d');
}
function halt() {
active = false;
if (raf) { cancelAnimationFrame(raf); raf = 0; }
}
function start() {
if (!canvas) build();
if (!ctx) return;
canvas.style.display = '';
measure();
if (rq.matches) { halt(); hero = 0; draw(0, window.scrollY || 0); }
else if (!active) { active = true; last = 0; lastScroll = window.scrollY || 0; raf = requestAnimationFrame(frame); }
requestAnimationFrame(function () { if (canvas) canvas.style.opacity = '1'; });
}
function hide() {
halt();
if (canvas) { canvas.style.opacity = '0'; canvas.style.display = 'none'; }
}
function sync() {
if (printing) { halt(); return; }
if (mq.matches && !document.hidden) start();
else if (!mq.matches) hide();
else halt();
}
function init() {
window.addEventListener('pointermove', onMove, { passive: true });
document.documentElement.addEventListener('pointerleave', onLeave);
window.addEventListener('blur', onLeave);
window.addEventListener('resize', onResize, { passive: true });
window.addEventListener('scroll', onScroll, { passive: true });
document.addEventListener('visibilitychange', onVis);
window.addEventListener('beforeprint', function () { printing = true; halt(); });
window.addEventListener('afterprint', function () { printing = false; sync(); });
var watch = function (q) { if (q.addEventListener) q.addEventListener('change', sync); else if (q.addListener) q.addListener(sync); };
watch(mq); watch(rq);
sync();
if (document.fonts && document.fonts.ready) document.fonts.ready.then(function () { queue(0); });
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
} catch (err) { /* decorative only */ }
})();
/* Edge field (phones, tablets and narrow windows).
The same etched plate as the desktop margins, reshaped for a narrow screen:
a thin frame of contour lines along the edges and under the header, and a
deeper band at the bottom that rises like a tide while the page scrolls and
settles when it stops. A touch sends rings out from the finger; they show in
the frame as they reach it. Never behind the reading column. One still frame
under reduced motion; nothing under Save-Data or print. Self-contained. */
(function () {
'use strict';
try {
if (!window.matchMedia || !window.requestAnimationFrame) return;
var conn = navigator.connection;
if (conn && conn.saveData) return;
var desk = window.matchMedia('(min-width: 1100px) and (hover: hover) and (pointer: fine)');
var rq = window.matchMedia('(prefers-reduced-motion: reduce)');
var perm = new Uint8Array(512);
var GX = [1, -1, 1, -1, 1, -1, 0, 0], GY = [1, 1, -1, -1, 0, 0, 1, -1];
(function () {
var p = new Uint8Array(256), i, j, tmp, s = 20261109;
for (i = 0; i < 256; i++) p[i] = i;
for (i = 255; i > 0; i--) {
s = (Math.imul(s, 1664525) + 1013904223) >>> 0;
j = s % (i + 1); tmp = p[i]; p[i] = p[j]; p[j] = tmp;
}
for (i = 0; i < 512; i++) perm[i] = p[i & 255];
})();
var F2 = 0.5 * (Math.sqrt(3) - 1), G2 = (3 - Math.sqrt(3)) / 6;
function noise(xin, yin) {
var s = (xin + yin) * F2, i = Math.floor(xin + s), j = Math.floor(yin + s);
var t0 = (i + j) * G2, x0 = xin - (i - t0), y0 = yin - (j - t0);
var i1 = x0 > y0 ? 1 : 0, j1 = 1 - i1;
var x1 = x0 - i1 + G2, y1 = y0 - j1 + G2, x2 = x0 - 1 + 2 * G2, y2 = y0 - 1 + 2 * G2;
var ii = i & 255, jj = j & 255, n = 0, g, q;
q = 0.5 - x0 * x0 - y0 * y0;
if (q > 0) { g = perm[ii + perm[jj]] & 7; q *= q; n += q * q * (GX[g] * x0 + GY[g] * y0); }
q = 0.5 - x1 * x1 - y1 * y1;
if (q > 0) { g = perm[ii + i1 + perm[jj + j1]] & 7; q *= q; n += q * q * (GX[g] * x1 + GY[g] * y1); }
q = 0.5 - x2 * x2 - y2 * y2;
if (q > 0) { g = perm[ii + 1 + perm[jj + 1]] & 7; q *= q; n += q * q * (GX[g] * x2 + GY[g] * y2); }
return 70 * n;
}
var CELL = 12, PARALLAX = 0.35, TOP_DEPTH = 40, BASE_DEPTH = 66, TIDE_MAX = 120;
/* Half the levels and lighter ink than the desktop plate: on a narrow screen the frame
sits close to the text, so it stays quiet. */
var LEVELS = [-0.7, -0.34, 0.02, 0.38, 0.74], ACCENT = 2;
var INK = 'rgba(21, 20, 15, 0.14)', CLAY = 'rgba(176, 84, 51, 0.46)', DOTS = 'rgba(21, 20, 15, 0.09)';
var canvas = null, ctx = null, mask = null, mctx = null, raf = 0, active = false, printing = false;
var W = 0, H = 0, dpr = 1, side = 16, headBottom = 0, cols = 0, rows = 0, field = null;
var t = 0, last = 0, lastScroll = 0, speed = 0, depth = BASE_DEPTH, maskDepth = -1, calmSince = 0;
var ripples = [], dirty = false, pending = 0;
var layoutW = -1;
function measure() {
var w = window.innerWidth, h = window.innerHeight, d = Math.min(window.devicePixelRatio || 1, 2);
if (w === W && h === H && d === dpr && field) return; /* nothing changed: keep the bitmap */
W = w; H = h; dpr = d;
canvas.width = Math.round(W * dpr); canvas.height = Math.round(H * dpr);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
mask.width = canvas.width; mask.height = canvas.height;
mctx.setTransform(dpr, 0, 0, dpr, 0, 0);
cols = Math.ceil(W / CELL) + 1; rows = Math.ceil(H / CELL) + 1;
field = new Float32Array(cols * rows);
maskDepth = -1;
if (W === layoutW) return; /* a toolbar showing or hiding changes only the height */
layoutW = W;
var left = W, els = document.querySelectorAll('main .wrap, main .wrap--wide, main .wrap--data, main .wrap--narrow'), k;
for (k = 0; k < els.length; k++) {
var r = els[k].getBoundingClientRect();
if (r.width < 200) continue;
left = Math.min(left, r.left + (parseFloat(getComputedStyle(els[k]).paddingLeft) || 0));
}
side = Math.max(10, Math.min(44, (left === W ? 20 : left) - 2));
var hd = document.querySelector('.site-header');
headBottom = hd ? Math.max(0, Math.min(140, hd.getBoundingClientRect().bottom)) : 0;
}
function buildMask(d) {
maskDepth = d;
mctx.globalCompositeOperation = 'source-over';
mctx.clearRect(0, 0, W, H);
mctx.globalCompositeOperation = 'lighter';
var g = mctx.createLinearGradient(0, 0, side, 0);
g.addColorStop(0, 'rgba(0,0,0,1)'); g.addColorStop(1, 'rgba(0,0,0,0)');
mctx.fillStyle = g; mctx.fillRect(0, 0, side, H);
g = mctx.createLinearGradient(W, 0, W - side, 0);
g.addColorStop(0, 'rgba(0,0,0,1)'); g.addColorStop(1, 'rgba(0,0,0,0)');
mctx.fillStyle = g; mctx.fillRect(W - side, 0, side, H);
var tEnd = headBottom + TOP_DEPTH;
g = mctx.createLinearGradient(0, 0, 0, tEnd);
g.addColorStop(0, 'rgba(0,0,0,1)'); g.addColorStop(Math.min(0.99, headBottom / tEnd), 'rgba(0,0,0,0.85)'); g.addColorStop(1, 'rgba(0,0,0,0)');
mctx.fillStyle = g; mctx.fillRect(0, 0, W, tEnd);
g = mctx.createLinearGradient(0, H, 0, H - d);
g.addColorStop(0, 'rgba(0,0,0,1)'); g.addColorStop(1, 'rgba(0,0,0,0)');
mctx.fillStyle = g; mctx.fillRect(0, H - d, W, d);
mctx.globalCompositeOperation = 'source-over';
}
function sample(scrollY, now) {
var yo = scrollY * PARALLAX, a = t * 0.012, b = t * 0.007, c = t * 0.016, k = 0, n = ripples.length;
for (var j = 0; j < rows; j++) {
var y = j * CELL, fy = y + yo;
for (var i = 0; i < cols; i++, k++) {
var x = i * CELL;
var v = 0.66 * noise(x / 150 + a, fy / 150 + b) + 0.34 * noise(x / 68 - c + 17.3, fy / 68 + 4.1);
for (var r = 0; r < n; r++) {
var rp = ripples[r], age = (now - rp.t) / 1000;
var dx = x - rp.x, dy = y - rp.y, dist = Math.sqrt(dx * dx + dy * dy) - (30 + 320 * age);
if (dist > -60 && dist < 60) v += 1.1 * (1 - age / 1.8) * Math.exp(-dist * dist / 392);
}
field[k] = v;
}
}
}
function inFrame(x, y) {
return x < side + CELL || x > W - side - CELL || y < headBottom + TOP_DEPTH + CELL || y > H - depth - CELL;
}
function contour(level) {
for (var j = 0; j < rows - 1; j++) {