-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
48 lines (32 loc) · 868 Bytes
/
script.js
File metadata and controls
48 lines (32 loc) · 868 Bytes
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
document.body.addEventListener('keyup', event => {
playSound(event.code.toLowerCase())
})
document.querySelector('.composer button')
.addEventListener('click', event => {
let song = document.querySelector('#input').value
let songArrays = song.split('')
playCompose(songArrays)
})
function playSound(sound) {
let audioElement = document.querySelector(`#s_${sound}`)
let keyElement = document.querySelector(`div[data-key="${sound}"]`)
if(audioElement) {
audioElement.currentTime = 0
audioElement.play()
}
if(keyElement) {
keyElement.classList.add('active')
setTimeout(() => {
keyElement.classList.remove('active')
}, 300)
}
}
function playCompose(songArrays) {
let wait = 0
for(let songItem of songArrays) {
setTimeout(() => {
playSound(`key${songItem}`)
}, wait)
wait += 270
}
}