This repository has been archived by the owner on Mar 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
eruda-fps.js
221 lines (185 loc) · 5.56 KB
/
eruda-fps.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Modified from https://github.com/mrdoob/stats.js/
;(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory)
} else if (typeof module === 'object' && module.exports) {
module.exports = factory()
} else {
root.erudaFps = factory()
}
})(this, function() {
var w = window.innerWidth
return function(eruda) {
var WIDTH = w,
HEIGHT = 192,
TEXT_X = 12,
TEXT_Y = 8,
GRAPH_X = 12,
GRAPH_Y = 40,
GRAPH_WIDTH = w - 24,
GRAPH_HEIGHT = 142,
STEP = 2,
NAME = 'FPS'
var util = eruda.util,
Settings = eruda.Settings,
round = Math.round,
Tool = eruda.Tool
var Fps = Tool.extend({
name: 'fps',
init: function($el, container) {
this.callSuper(Tool, 'init', arguments)
this._style = util.evalCss(
[
'.eruda-fps {padding: 10px !important;}',
'canvas {width: 100%; border: 1px solid var(--border);}'
].join('.eruda-fps ')
)
this._container = container
this._isRunning = false
this._beginTime = util.now()
this._prevTime = this._beginTime
this._frames = 0
this._min = Infinity
this._max = 0
this._alwaysActivated = true
this._appendTpl()
this._initCanvas()
this._initCfg()
},
show: function() {
this._start()
this.callSuper(Tool, 'show', arguments)
},
hide: function() {
if (!this._alwaysActivated) this._stop()
this.callSuper(Tool, 'hide', arguments)
},
destroy: function() {
this._stop()
util.evalCss.remove(this._style)
this.callSuper(Tool, 'destroy', arguments)
this._rmCfg()
},
_rmCfg: function() {
var cfg = this.config
var settings = this._container.get('settings')
if (!settings) return
settings.remove(cfg, 'alwaysActivated').remove('Fps')
},
_start: function() {
if (this._isRunning) return
var self = this
this._isRunning = true
function loop() {
if (!self._isRunning) return
self._update()
requestAnimationFrame(loop)
}
loop()
},
_stop: function() {
this._isRunning = false
this._beginTime = util.now()
this._prevTime = this._beginTime
this._frames = 0
},
_appendTpl: function() {
this._$el.html('<canvas></canvas>')
this._canvas = this._$el.find('canvas').get(0)
this._ctx = this._canvas.getContext('2d')
},
_initCanvas: function() {
var canvas = this._canvas
var ctx = this._ctx
var curTheme = util.evalCss.getCurTheme()
var BACK_COLOR = curTheme.background
var FORE_COLOR = curTheme.accent
canvas.width = WIDTH
canvas.height = HEIGHT
ctx.font = 'bold 18px Helvetica,Arial,sans-serif'
ctx.textBaseline = 'top'
ctx.fillStyle = BACK_COLOR
ctx.fillRect(0, 0, WIDTH, HEIGHT)
ctx.fillStyle = FORE_COLOR
ctx.fillText(NAME, TEXT_X, TEXT_Y)
ctx.fillRect(GRAPH_X, GRAPH_Y, GRAPH_WIDTH, GRAPH_HEIGHT)
ctx.fillStyle = BACK_COLOR
ctx.globalAlpha = 0.9
ctx.fillRect(GRAPH_X, GRAPH_Y, GRAPH_WIDTH, GRAPH_HEIGHT)
},
_initCfg: function() {
var container = this._container
var cfg = (this.config = Settings.createCfg('fps', {
alwaysActivated: true
}))
if (!cfg.get('alwaysActivated')) this._alwaysActivated = false
var self = this
cfg.on('change', function(key, val) {
switch (key) {
case 'alwaysActivated':
self._alwaysActivated = val
return
}
})
var settings = container.get('settings')
settings
.text('Fps')
.switch(cfg, 'alwaysActivated', 'Always Activated')
.separator()
},
_update: function() {
this._frames++
var prevTime = this._prevTime,
time = util.now()
// Only update one time per second.
if (time > prevTime + 1000) {
this._draw((this._frames * 1000) / (time - prevTime), 100)
this._prevTime = time
this._frames = 0
}
this._beginTime = time
},
_draw: function(val, maxVal) {
this._min = Math.min(this._min, val)
this._max = Math.max(this._max, val)
var curTheme = util.evalCss.getCurTheme()
var BACK_COLOR = curTheme.background
var FORE_COLOR = curTheme.accent
var min = this._min
var max = this._max
var canvas = this._canvas
var ctx = this._ctx
ctx.fillStyle = BACK_COLOR
ctx.globalAlpha = 1
ctx.fillRect(0, 0, WIDTH, GRAPH_Y)
ctx.fillStyle = FORE_COLOR
ctx.fillText(
round(val) + '' + NAME + ' (' + round(min) + '-' + round(max) + ')',
TEXT_X,
TEXT_Y
)
ctx.drawImage(
canvas,
GRAPH_X + STEP,
GRAPH_Y,
GRAPH_WIDTH - STEP,
GRAPH_HEIGHT,
GRAPH_X,
GRAPH_Y,
GRAPH_WIDTH - STEP,
GRAPH_HEIGHT
)
ctx.fillRect(GRAPH_X + GRAPH_WIDTH - STEP, GRAPH_Y, STEP, GRAPH_HEIGHT)
ctx.fillStyle = BACK_COLOR
ctx.globalAlpha = 0.9
ctx.fillRect(
GRAPH_X + GRAPH_WIDTH - STEP,
GRAPH_Y,
STEP,
round((1 - val / maxVal) * GRAPH_HEIGHT)
)
}
})
return new Fps()
}
})