forked from terkelg/zuckerberg.smile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
64 lines (55 loc) · 1.67 KB
/
main.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
const centerEl = document.querySelector('.center');
const spriteEl = document.querySelector('.sprite');
const valueEl = document.querySelector('.value');
const sliderEl = document.querySelector('.mood__slider');
let width, height, frame = 0;
resize();
window.addEventListener('resize', resize);
function resize(){
height = centerEl.clientWidth/2;
// width = centerEl.clientWidth;
width = height * 540/960 ;
// height = width * 601/338;
spriteEl.style.height = `${height}px`;
spriteEl.style.width = `${width}px`;
// spriteEl.style.backgroundPositionY = `-${height*frame}px`;
spriteEl.style.backgroundPositionX = `-${width*frame}px`;
}
const playSound = throttle(() => {
const sound = new Audio('assets/click.mp3');
sound.play();
}, 20, true);
const onSliderInput = e => {
frame = sliderEl.value;
// spriteEl.style.backgroundPositionY = `-${height*frame}px`;
spriteEl.style.backgroundPositionX = `-${width*frame}px`;
valueEl.textContent = (map(frame, 0, 30, 0, 1) * 10 / 10).toFixed(2);
playSound();
}
sliderEl.addEventListener('input', onSliderInput);
// ===== Helpers
function map (value, start1, stop1, start2, stop2) {
return ((value - start1) / (stop1 - start1)) * (stop2 - start2) + start2
}
function throttle(fn, interval, callFirst) {
let wait = false;
let callNow = false;
return () => {
callNow = callFirst && !wait;
let context = this;
let args = arguments;
if (!wait) {
wait = true;
setTimeout(() => {
wait = false;
if (!callFirst) {
return fn.apply(context, args);
}
}, interval);
}
if (callNow) {
callNow = false;
return fn.apply(this, arguments);
}
};
}