-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrapsManager.js
More file actions
73 lines (61 loc) · 1.97 KB
/
Copy pathFrapsManager.js
File metadata and controls
73 lines (61 loc) · 1.97 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var frapsManager = {
redrawInterval: null,
culcFPSInterval: null,
started: false,
lastRedrawTime: null,
FPS: 0, //actual value frames per seconds. refreshes every 1000 millisec
tbfr: null, //actual time between frame redraw
context: null,//context where will be draw
object: null,//object, wich will be draw
showFPS: false,
timeBetweenRedrawing: null,
init: function (context, object, needCountFPS, showFPS){
this.context = context;
this.object = object;
this.showFPS = showFPS;
this.timeBetweenRedrawing = 1000/(needCountFPS ? needCountFPS : 20);// frame will try to update every this.timeBetweenRedrawing miliseconds
},
setObject: function(object){
this.object = object;
},
start: function(){
if(this.started) return;
this.started = true;
var self = this;
this.redrawInterval = setInterval(function(){self.redraw();}, this.timeBetweenRedrawing);
this.culcFPSInterval = setInterval(function(){self.culcFPS();}, 1000);
},
/* stop: function(){
this.started = false;
clearInterval(this.redrawInterval);
clearInterval(this.culcFPSInterval);
},
*/
redraw: function(){
this.clearScreen();
this.object.draw(this.context);
if(this.showFPS){
this.drawFPS();
this.culcTbfr();
}
},
clearScreen: function(){
var canvas = this.context.canvas;
this.context.clearRect(0, 0, canvas.width, canvas.height);
},
//culculates time between frame redrawing in milliseconds (does not work if time will be bigger then 999 milliseconds)
culcTbfr: function(){
var now = Date.now();
this.tbfr = now - this.lastRedrawTime;
this.lastRedrawTime = now;
},
culcFPS: function(){
this.FPS = 1000/this.tbfr;
},
drawFPS: function(){
this.context.textBaseline = "top";
this.context.font = consts.FPS_FONT;
this.context.fillStyle = consts.FPS_FONT_COLOR;
this.context.fillText("FPS: "+Math.round(this.FPS), this.context.canvas.width - 55, this.context.canvas.height - 20);
}
}