-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
68 lines (54 loc) · 2.41 KB
/
script.js
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
const track = document.getElementById("image-track");
const heading = document.getElementById("heading");
window.onload = e => {
const words = heading.getElementsByClassName("word");
for(i = 0; i < words.length; i++) {
const word = words[i];
/** Percentage representing the individual word's location in the list of words */
const wordRegion = ((i + 0.5) / words.length) * 100;
word.style.opacity = fuzzyMatch(wordRegion, 0, words.length) / 100;
}
}
/**
* Calculate how much an an item matches the reference as a percentage
* @param {float} itemPercent - Must be 0.0 - 100.0
* @param {float} reference - Must be 0.0 - 100.0
* @param {int} numMatches - Number of items to fuzz-match
* @returns {float} - Percentage of matching for this current item
*/
function fuzzyMatch(itemPercent, reference, numMatches) {
const delta = Math.abs(itemPercent - reference);
const quadraticDelta = (Math.sqrt(numMatches) * delta * delta / (100 / numMatches));
return Math.max(100 - quadraticDelta, 0);
}
window.onmousedown = e => {
track.dataset.mouseDownAt = e.clientX;
}
window.onmouseup = e => {
track.dataset.mouseDownAt = "0";
track.dataset.percentScroll = track.dataset.percentage;
}
window.onmousemove = e => {
if (track.dataset.mouseDownAt === "0") return;
const mouseDelta = parseFloat(track.dataset.mouseDownAt) - e.clientX;
const maxDelta = window.innerWidth / 1.8;
const percentage = (mouseDelta / maxDelta) * -100;
let nextPercentage = parseFloat(track.dataset.percentScroll) + percentage;
nextPercentage = nextPercentage < -100 ? -100 :
nextPercentage > 0 ? 0 : nextPercentage;
track.dataset.percentage = nextPercentage;
track.animate({
transform: `translate(${nextPercentage}%, -50%)`
}, {duration: 1200, fill: "forwards"})
for (const image of track.getElementsByClassName("image")) {
image.animate({ objectPosition: `${nextPercentage + 100}% 50%` },
{ duration: 1200, fill:"forwards" });
}
const words = heading.getElementsByClassName("word")
for(i = 0; i < words.length; i++) {
const word = words[i];
/** Percentage representing the individual word's location in the list of words */
const wordRegion = ((i + 0.5) / words.length) * 100;
word.style.opacity = fuzzyMatch(wordRegion, -nextPercentage, words.length) / 200;
}
}