-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
149 lines (118 loc) · 4.42 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
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
const toPercent = (num) => {
return num ? (num * 100) + '%' : 0;
};
const toInvPercent = (num) => {
return num ? (100 - (num * 100)) + '%' : '100%';
};
const lerp = (v1, v2, amt) => {
amt = amt < 0 ? 0 : amt;
amt = amt > 1 ? 1 : amt;
return v1 + (v2 - v1) * amt;
};
(async () => {
// state
let day = 0;
let playing = true;
let previousDay = -1;
const speed = 0.2;
const animSpeed = 0.5;
// get data
const twelvePlusData = await (await fetch('twelvePlus.json')).json();
const twelveToFifteenData = await (await fetch('twelveToFifteen.json')).json();
const sixteenPlusData = await (await fetch('sixteenPlus.json')).json();
const fiftyPlusData = await (await fetch('fiftyPlus.json')).json();
const seventyPlusData = await (await fetch('seventyPlus.json')).json();
let data = twelvePlusData;
// get elems
const dayDisplay = document.getElementById('day');
const control = document.getElementById('control');
const playButton = document.getElementById('play');
const twelvePlusElem = document.getElementById('twelvePlus');
const twelveToFifteenElem = document.getElementById('twelveToFifteen');
const sixteenPlusElem = document.getElementById('sixteenPlus');
const fiftyPlusElem = document.getElementById('fiftyPlus');
const seventyPlusElem = document.getElementById('seventyPlus');
const states = ["nsw", "vic", "qld", "act", "wa", "sa", "tas", "nt"];
let horses = {};
for (var i = 0; i < states.length; i++) {
horses[states[i]] = document.getElementById(states[i]);
}
let reset = (newDay, setPlaying) => {
// set control minmax
control.min = 0;
control.max = data.length - 1;
control.value = newDay || 0;
day = newDay || 0;
previousDay = -1;
if(setPlaying) {
playButton.className = 'playing';
playing = true;
}
};
reset();
// event listeners
control.addEventListener('input', () => {
day = parseFloat(control.value);
});
playButton.addEventListener('click', (e) => {
e.preventDefault();
playing = !playing;
playButton.className = playing ? 'playing': 'paused';
if(day >= data.length - 1) reset(null, true);
});
const removeToggleClass = () => {
[...document.querySelectorAll('.data_selector div')].forEach(ii => {
ii.className = '';
});
};
const handler = (newData) => (e) => {
e.preventDefault();
removeToggleClass();
e.target.className = 'active';
const currentDate = data[Math.floor(day)].date;
const newDateIndex = newData.findIndex(ii => ii.date === currentDate);
data = newData;
if(newDateIndex !== -1) {
reset(newDateIndex);
} else {
reset();
}
};
twelvePlusElem.addEventListener('click', handler(twelvePlusData));
twelveToFifteenElem.addEventListener('click', handler(twelveToFifteenData));
sixteenPlusElem.addEventListener('click', handler(sixteenPlusData));
fiftyPlusElem.addEventListener('click', handler(fiftyPlusData));
seventyPlusElem.addEventListener('click', handler(seventyPlusData));
// anim loop
function step() {
// increment the frame
if(playing) {
if(day < data.length - 1) {
day = day + speed;
control.value = day;
} else {
playing = false;
playButton.className = 'paused';
}
}
// Don't update if nothing has changed
if(previousDay === day) {
window.requestAnimationFrame(step)
return;
};
const frame = Math.round((day / speed) * animSpeed) % 6;
const dd = data[Math.floor(day)];
const nd = data[Math.floor(day) + 1] || dd;
const rt = day - Math.floor(day);
dayDisplay.textContent = luxon.DateTime.fromISO(dd.date).toLocaleString(luxon.DateTime.DATE_MED);
for (var i = 0; i < states.length; i++) {
var state = states[i];
horses[state].className = `horse frame${frame}`;
horses[state].style.left = toPercent(lerp(dd[state+"_second_percent"], nd[state+"_second_percent"], rt));
horses[state].style.right = toInvPercent(lerp(dd[state+"_first_percent"], nd[state+"_first_percent"], rt));
}
previousDay = day;
window.requestAnimationFrame(step);
}
window.requestAnimationFrame(step);
})();