Last active
November 11, 2018 22:43
-
-
Save mhdSid/6fbe171ad238ecf3e7cb89a83705a3b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public fadeInAudioElementVolume (start: number, end: number, duration: number, callback?: Function): void { | |
const delta: number = end - start; | |
let startTime: number = null; | |
let t: number; | |
let factor: number; | |
let frame: any; | |
const tweenLoop: FrameRequestCallback = (time?: number) => { | |
if (!time) { | |
time = new Date().getTime(); | |
} | |
if (startTime === null) { | |
startTime = time; | |
} | |
t = time - startTime; | |
factor = t / duration; | |
this.setVolume(start + delta * factor); // or audioElement.volume = start + delta * factor; | |
if (t < duration && this.getVolume() < end) { // or audioElement.volume < end | |
frame = window.requestAnimationFrame(tweenLoop); | |
return; | |
} | |
window.cancelAnimationFrame(frame); | |
if (typeof callback === 'function') { | |
callback(); | |
} | |
tweenLoop = delta = startTime = t = time = start = frame = null; | |
}; | |
end = Math.round(end); | |
this.setVolume(0); // or audioElement.volume = 0; | |
frame = window.requestAnimationFrame(tweenLoop); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment