/
clock.ts
51 lines (40 loc) · 1.19 KB
/
clock.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
40
41
42
43
44
45
46
47
48
49
50
51
/*
Project: Arena game
Author: Copyright (C) 2015, Atanas Laskov
License: BSD license, see LICENSE.md for more details.
http://www.atanaslaskov.com/arena/
*/
// Animation clock
//
class Clock{
public clock: number; // Absolute time
public dt: number; // Time delta
public fps: number; // Frames per second
private fpsUpdateTime: number;
constructor() {
this.clock = window.performance.now();
this.fpsUpdateTime = 0;
// Reset clock when focus returns to the game window, or an FPS glitch will occur
window.addEventListener("focus", () => {
this.clock = window.performance.now();
});
}
public tick() {
var t = window.performance.now();
this.dt = (t - this.clock) * 0.001; // Sec
if( this.dt <= 0 ) {
this.dt = 0.001;
}
this.fps = 1.0 / this.dt;
this.clock = t;
// Every 0.5 seconds, show an updated figure
//
this.fpsUpdateTime -= this.dt;
if( this.fpsUpdateTime <= 0 ) {
this.fpsUpdateTime = 0.5;
document.getElementById("fps").innerHTML = "Frame Rate: " + Math.round(this.fps) + "fps";
}
// Return the time difference, because it is what we need for animation
return this.dt;
}
}