-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (32 loc) · 1.13 KB
/
index.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
const allowedEvents = ['transitionstart', 'animationstart']
const startingFrequency = 600
const endingFrequency = 8000
let running = false
function generateAudio(duration) {
if (running) return
running = true
const context = new (window.AudioContext || window.webkitAudioContext)()
const osc = context.createOscillator()
const now = context.currentTime
osc.type = 'sine'
osc.frequency.setValueAtTime(startingFrequency, now)
osc.frequency.exponentialRampToValueAtTime(endingFrequency, now + duration)
osc.connect(context.destination)
osc.start()
osc.stop(context.currentTime + duration)
setTimeout(() => {
running = false
}, duration * 1000)
}
function animationListener(e) {
const target = e.target
if (allowedEvents.indexOf(e.type) === -1) return
const styles = getComputedStyle(target)
const duration =
e.type === 'transitionstart' ? styles.transitionDuration : styles.animationDuration
const seconds = Number(duration.replace('s', ''))
if (isNaN(seconds)) return
generateAudio(seconds)
}
document.addEventListener('animationstart', animationListener, false)
document.addEventListener('transitionstart', animationListener, false)