Created
December 16, 2022 15:01
-
-
Save positlabs/4da1f1f61a30c462c77c44db6889706a to your computer and use it in GitHub Desktop.
Register a callback for when a video frame is updated
This file contains hidden or 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
/* | |
Register a callback for when a video frame is updated | |
Since video playback generally isn't 60fps, this can be used as a render optimization | |
new FrameUpdateMonitor(videoElement, callback) | |
*/ | |
export class FrameUpdateMonitor { | |
private _active: boolean = false | |
public set active (bool){ | |
this._active = bool | |
if(this._active) this.onFrame() | |
} | |
public get active(){ | |
return this._active | |
} | |
video: HTMLVideoElement | |
lastTime: number = -1 | |
callback: Function | |
constructor(video: HTMLVideoElement, callback: Function) { | |
this.video = video | |
this.callback = callback | |
this.onFrame() | |
} | |
private onFrame() { | |
if (this.active) requestAnimationFrame(this.onFrame.bind(this)) | |
var time = this.video.currentTime | |
// console.log('onFrame', this.active, time, this.lastTime) | |
if (time !== this.lastTime) { | |
this.lastTime = time | |
this.callback(time) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment