-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcustom.js
57 lines (43 loc) · 1.47 KB
/
custom.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
function startExample () {
const audioContext = new window.AudioContext()
// setup canvas
const canvas = document.createElement('canvas')
canvas.width = window.innerWidth
canvas.height = window.innerHeight
document.body.appendChild(canvas)
// setup audio element
const audioElement = document.createElement('audio')
audioElement.autoplay = true
audioElement.src = 'audio.mp3'
document.body.appendChild(audioElement)
// create source from html5 audio element
const source = audioContext.createMediaElementSource(audioElement)
// attach oscilloscope
const scope = new Oscilloscope(source)
// reconnect audio output to speakers
source.connect(audioContext.destination)
const ctx = canvas.getContext('2d')
ctx.lineWidth = 3
ctx.shadowBlur = 4
ctx.shadowColor = 'white'
// custom animation loop
function drawLoop () {
ctx.clearRect(0, 0, canvas.width, canvas.height)
const centerX = canvas.width / 2
const centerY = canvas.height / 2
// draw circle
ctx.beginPath()
ctx.arc(centerX, centerY, 100, 0, 2 * Math.PI, false)
ctx.fillStyle = 'yellow'
ctx.fill()
// draw three oscilloscopes in different positions and colors
ctx.strokeStyle = 'lime'
scope.draw(ctx, 0, 0, centerX, centerY)
ctx.strokeStyle = 'cyan'
scope.draw(ctx, centerX, 0, centerX, centerY)
ctx.strokeStyle = 'red'
scope.draw(ctx, 0, centerY, undefined, centerY)
window.requestAnimationFrame(drawLoop)
}
drawLoop()
}