-
Notifications
You must be signed in to change notification settings - Fork 46
/
fps_controller.ts
39 lines (32 loc) · 1.02 KB
/
fps_controller.ts
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
//https://stackoverflow.com/questions/19764018/controlling-fps-with-requestanimationframe
export class FPSController{
fpsInterval:number
startTime:number;
now:number;
then:number;
elapsed:number;
enabled:boolean = false;
constructor(target_fps:number){
this.fpsInterval = 1000 / target_fps;
this.then = Date.now();
this.startTime = this.then;
}
UpdateTargetFPS(target_fps:number)
{
this.fpsInterval = 1000 / target_fps;
}
IsFrameReady():boolean{
this.now = Date.now();
this.elapsed = this.now - this.then;
// if enough time has elapsed, draw the next frame
if (this.elapsed > this.fpsInterval)
{
// Get ready for next frame by setting then=now, but also adjust for your
// specified fpsInterval not being a multiple of RAF's interval (16.7ms)
this.then = this.now - (this.elapsed % this.fpsInterval);
return true;
}
else
return false;
}
}