forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
59 lines (50 loc) · 1.47 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
const beatPeriod = 1000
const tempoKeys = (new Array(9)).fill(0)
const keys = Array.from(document.querySelectorAll('.key'))
const audios = Array.from(document.querySelectorAll('audio'))
let beat = 1
let tempo = () => {
tempoKeys.forEach((key, idx) => {
// if the current key is set, play it if it matches the beat
if (key >= beat) {
keys[idx].classList.add('playing-' + beat)
audios[idx].currentTime = 0
audios[idx].play()
}
})
beat = beat % 3 + 1
}
Array.from(document.querySelectorAll('.key-container')).forEach(
(container, idx) => container.addEventListener('click', () => {
const spans = container.querySelectorAll('.key-repeat span')
let i = tempoKeys[idx]
// update key
if (i !== 0) {
spans[i - 1].classList.remove('active')
}
if (i !== 3) {
spans[i].classList.add('active')
// set key
tempoKeys[idx]++
} else {
// reset key
tempoKeys[idx] = 0
}
})
)
keys.forEach(
key => key.addEventListener('transitionend', e => {
if (e.propertyName !== 'transform') return
['playing', 'playing-1', 'playing-2', 'playing-3'].forEach(
c => e.target.classList.remove(c)
)
})
)
window.addEventListener('keydown', e => {
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`)
if (!audio) return
audio.currentTime = 0
audio.play()
document.querySelector(`div[data-key="${e.keyCode}"]`).classList.add('playing')
})
window.setInterval(tempo, beatPeriod)