-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcocoon_fps_webview.js
More file actions
executable file
·132 lines (120 loc) · 4.64 KB
/
cocoon_fps_webview.js
File metadata and controls
executable file
·132 lines (120 loc) · 4.64 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
(function()
{
if (navigator.isCocoon) return false;
/**
* A class that represents a Frames Per Second counter. It relies in Timer() to do the time calculation. We will use this fallback if Cocoon.Timer is not available.
*/
Timer = function() {
this.reset();
return this;
};
Timer.prototype = {
currentTimeInMillis : 0,
lastTimeInMillis : 0,
elapsedTimeInMillis : 0,
elapsedTimeInSeconds : 0,
accumTimeInMillis : 0,
reset : function() {
this.currentTimeInMillis = this.lastTimeInMillis = new Date().getTime();
this.accumTimeInMillis = this.elapsedTimeInMillis = this.elapsedTimeInSeconds = 0;
},
update : function() {
this.currentTimeInMillis = new Date().getTime();
this.elapsedTimeInMillis = this.currentTimeInMillis - this.lastTimeInMillis;
this.elapsedTimeInSeconds = this.elapsedTimeInMillis / 1000.0;
this.lastTimeInMillis = this.currentTimeInMillis;
this.accumTimeInMillis += this.elapsedTimeInMillis;
}
};
/**
* Use this function to show the FPS Counter on the screen when using a web view.
* You should call Object.enable(); in order to show the FPS Counter and Object.disable() to remove it from the screen.
* @function
*/
var FPSCounter = function() {
this.timer = (typeof(window.Cocoon) !== "undefined" && typeof(window.Cocoon.Timer) !== "undefined") ? new Cocoon.Timer() : new Timer();
this.reset();
return this;
};
FPSCounter.prototype = {
timer : 0,
fps : 0,
averageFPS : 0,
fpsIntervalId : null,
fpsDiv : null,
fpsDivParentDiv : null,
reset : function()
{
this.timer.reset();
this.fps = 0;
this.averageFPS = 0;
},
update : function()
{
this.timer.update();
this.fps++;
if (this.timer.accumTimeInMillis >= 1000) {
this.averageFPS = ((1000 * this.fps / this.timer.accumTimeInMillis * 100) | 0) / 100;
this.fps = 0;
this.timer.reset();
}
},
/**
* Enable Cocoon FPS overlay.
* @function
*/
enable : function(color){
var textColor = "white";
var fpsCounter = new FPSCounter();
var topOrBottom = true;
var leftOrRight = false;
this.fpsDivParentDiv = document.createElement("div");
this.fpsDivParentDiv.style.cssText = "position:absolute; " + (topOrBottom ? "top" : "bottom") + ":0%; width:100%; height:10%;z-index:99999;";
this.fpsDivParentDiv.innerHTML = "<div id='Cocoon_fpsDiv' style='background:black; float:" + (leftOrRight ? "left" : "right") + "; color:" + textColor + "; margin-" +(leftOrRight ? "left" : "right")+ ":8px;'></div>";
document.body.appendChild(this.fpsDivParentDiv);
this.fpsDiv = document.getElementById("Cocoon_fpsDiv");
this.fpsIntervalId = setInterval(function()
{
fpsCounter.update();
this.fpsDiv.innerHTML = "< WV FPS: " + (fpsCounter.averageFPS) + " >";
}.bind(this), 1000 / 60);
// If we are inside Cocoon webview, pause the Cocoon loop
if (typeof(window.ext) !== "undefined" && typeof(window.ext.IDTK_APP) !== "undefined") {
window.ext.IDTK_APP.makeCallAsync("forward", "window.ext.IDTK_APP.makeCall('pause');");
}
},
/**
* Disable the Cocoon FPS overlay.
* @function
*/
disable : function()
{
if (this.fpsIntervalId)
{
clearInterval(this.fpsIntervalId);
this.fpsIntervalId = undefined;
}
if (this.fpsDivParentDiv)
{
document.body.removeChild(this.fpsDivParentDiv);
thisfpsDivParentDiv = undefined;
}
this.fpsDiv = undefined;
}
};
/**
* When the page is loaded, add the FPS Counter to the web view.
* @ignore
*/
window.addEventListener("load", function() {
// If Cocoon Extensions are available, then add the FPS Counter to the Cocoon.App namespace.
if ((!!window.Cocoon) && (!!window.Cocoon.App)) {
window.Cocoon.App.FPSCounter = new FPSCounter();
window.Cocoon.App.FPSCounter.enable();
}else{
// We can use the FPS Counter without the Cocoon Extensions:
var counter = new FPSCounter();
counter.enable();
}
});
})();