-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
demo.js
60 lines (50 loc) · 1.89 KB
/
demo.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
import CDGraphics from '../index.js'
const audioUrl = 'YOUR_MP3_FILE.mp3'
const cdgUrl = 'YOUR_CDG_FILE.cdg'
const cdg = new CDGraphics()
document.addEventListener('DOMContentLoaded', () => {
const audio = document.getElementById('audio')
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
let frameId
const doRender = time => {
const frame = cdg.render(time, { forceKey: forceKeyCheckbox.checked })
if (!frame.isChanged) return
createImageBitmap(frame.imageData)
.then(bitmap => {
ctx.clearRect(0, 0, canvas.clientWidth, canvas.clientHeight)
ctx.imageSmoothingEnabled = false
ctx.drawImage(bitmap, 0, 0, canvas.clientWidth, canvas.clientHeight)
// for demo UI only
if (showContentBoundsCheckbox.checked) {
showContentBounds(frame.contentBounds)
}
})
}
// render loop
const pause = () => cancelAnimationFrame(frameId)
const play = () => {
frameId = requestAnimationFrame(play)
doRender(audio.currentTime)
}
// follow audio events (depending on your app, not all are strictly necessary)
audio.addEventListener('play', play)
audio.addEventListener('pause', pause)
audio.addEventListener('ended', pause)
audio.addEventListener('seeked', () => doRender(audio.currentTime))
// download and load cdg file
fetch(cdgUrl)
.then(response => response.arrayBuffer())
.then(buffer => {
cdg.load(buffer)
audio.src = audioUrl // pre-load audio
})
// for demo UI only
const forceKeyCheckbox = document.getElementById('forceKey')
const showContentBoundsCheckbox = document.getElementById('showContentBounds')
const showContentBounds = ((scale) => (contentBounds) => {
const [x1, y1, x2, y2] = contentBounds
ctx.strokeStyle = 'green'
ctx.strokeRect(x1 * scale, y1 * scale, (x2 - x1) * scale, (y2 - y1) * scale)
})(canvas.clientWidth / 300)
})