forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence-01.js
More file actions
49 lines (40 loc) · 942 Bytes
/
sequence-01.js
File metadata and controls
49 lines (40 loc) · 942 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
49
/* global Audio, requestAnimationFrame */
const initialTime = (new Date()).getTime()
let lastTime
let timeDiff = 0
const fps = 60
let seqIndex = 0
let currentStatePlaying = false
const HIHAT = new Audio('sounds/hihat.wav')
const KICK = new Audio('sounds/kick.wav')
function startOrStop (e) {
if (e.keyCode === 13) {
currentStatePlaying = !currentStatePlaying
}
draw()
}
function draw () {
const sequence = [
[KICK, HIHAT],
[HIHAT],
[HIHAT],
[KICK, HIHAT]
]
setTimeout(function () {
if (currentStatePlaying) {
requestAnimationFrame(draw)
}
const now = (new Date()).getTime()
if (lastTime) {
timeDiff = (now - initialTime) % 300
if (timeDiff < 20) {
sequence[seqIndex % sequence.length].forEach((audio) => {
audio.play()
})
seqIndex++
}
}
lastTime = now
}, 1000 / fps)
}
window.addEventListener('keydown', startOrStop)