-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.ts
38 lines (29 loc) · 1.16 KB
/
demo.ts
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
import { createMetronome, clock, MetronomeConfig } from "./src"
const form = document.querySelector("form")! as HTMLFormElement
const enabled = document.querySelector("input[name=enabled]")! as HTMLInputElement
const visualizer = document.getElementById("visualizer")! as HTMLInputElement
function onBeat(number: number) {
visualizer.querySelector('.active')?.classList.remove('active')
visualizer.children[number]?.classList.add('active')
}
function configure() {
config.tempo = Number(form.tempo.value)
config.meter = String(form.meter.value).split('/').map(Number)
metronome.configure(config)
visualizer.innerHTML = ''
for (let i = 0; i < Number(config.meter![0]); i++) {
const div = document.createElement('div')
div.classList.add(i === 0 ? 'green' : 'orange')
visualizer.appendChild(div)
}
}
enabled.addEventListener("change", () => {
enabled.checked ? metronome.start() : metronome.stop()
})
form.addEventListener("input", configure)
const metronome = createMetronome({ onBeat })
let config: MetronomeConfig = {}
configure()
setTimeout(async () => {
document.getElementById('offset')!.innerText = (await clock.sync()).toString()
}, 100)