Repository navigation
Expand file tree
/
Copy pathresearch.js
More file actions
59 lines (53 loc) · 1.98 KB
/
Copy pathresearch.js
File metadata and controls
59 lines (53 loc) · 1.98 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
/* The animation assets contain the supplied answers. This is a visual replay,
not an evaluator or a recording of a model's reasoning. */
(function () {
'use strict';
var cards = Array.prototype.slice.call(document.querySelectorAll('[data-sample]'));
if (!cards.length) return;
var motion = window.matchMedia('(prefers-reduced-motion: reduce)');
var observer = null;
function animations(card) {
var svg = card.querySelector('svg');
return svg && svg.getAnimations ? svg.getAnimations({ subtree: true }) : [];
}
function play(card) {
card.classList.add('sample-running');
if (motion.matches) return;
animations(card).forEach(function (animation) {
animation.currentTime = 0;
animation.play();
});
}
cards.forEach(function (card) {
var button = card.querySelector('.sample-replay');
button.hidden = motion.matches;
button.addEventListener('click', function () { play(card); });
});
function updateMotion() {
if (observer) observer.disconnect();
cards.forEach(function (card) {
card.querySelector('.sample-replay').hidden = motion.matches;
});
if (motion.matches) return;
if (!('IntersectionObserver' in window)) {
cards.forEach(play);
return;
}
observer = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (!entry.isIntersecting) {
animations(entry.target).forEach(function (animation) { animation.pause(); });
} else if (!entry.target.classList.contains('sample-running')) {
play(entry.target);
} else {
animations(entry.target).forEach(function (animation) {
if (animation.playState === 'paused' && animation.currentTime < animation.effect.getComputedTiming().endTime) animation.play();
});
}
});
}, { threshold: 0.15 });
cards.forEach(function (card) { observer.observe(card); });
}
updateMotion();
motion.addEventListener('change', updateMotion);
})();