-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject3.js
63 lines (46 loc) · 1.76 KB
/
project3.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
const playingClass = 'playing',
crashRide = document.getElementById('crash-ride'),
hiHatTop = document.getElementById('hihat-top');
const animateCrashOrRide = () => {
crashRide.style.transform = 'rotate(0deg) scale(1.5)';
};
const animateHiHatClosed = () => {
hiHatTop.style.top = '171px';
};
const playSound = e => {
const keyCode = e.keyCode,
keyElement = document.querySelector(`div[data-key="${keyCode}"]`);
if (!keyElement) return;
const audioElement = document.querySelector(`audio[data-key="${keyCode}"]`);
audioElement.currentTime = 0;
audioElement.play();
switch (keyCode) {
case 69:
case 82:
animateCrashOrRide();
break;
case 75:
animateHiHatClosed();
break;
}
keyElement.classList.add(playingClass);
};
const removeCrashRideTransition = e => {
if (e.propertyName !== 'transform') return;
e.target.style.transform = 'rotate(-7.2deg) scale(1.5)';
};
const removeHiHatTopTransition = e => {
if (e.propertyName !== 'top') return;
e.target.style.top = '166px';
};
const removeKeyTransition = e => {
if (e.propertyName !== 'transform') return;
e.target.classList.remove(playingClass)
};
const drumKeys = Array.from(document.querySelectorAll('.key'));
drumKeys.forEach(key => {
key.addEventListener('transitionend', removeKeyTransition);
});
crashRide.addEventListener('transitionend', removeCrashRideTransition);
hiHatTop.addEventListener('transitionend', removeHiHatTopTransition);
window.addEventListener('keydown', playSound);