-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
__Added:__ - π¦ NPM package `npm i fps-m`; - π¨βπ» Drop-in script versions; __Changed:__ - π¨βπ» Minor optimizations; - π Documentation update
- Loading branch information
1 parent
288cfd9
commit 7f7bb16
Showing
10 changed files
with
338 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
*.md | ||
.DS_Store | ||
.eslintcache | ||
.eslintrc | ||
.github | ||
.meteor | ||
.npm | ||
.versions | ||
cover.jpg | ||
cover.png | ||
node_modules | ||
npm-debug.log* | ||
package.js | ||
test/ | ||
test/* | ||
|
||
fps-meter.js | ||
fps-meter.html | ||
fps-meter-drop-in.js | ||
fps-meter-drop-in.min.js | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ [email protected] | |
[email protected] | ||
[email protected] | ||
[email protected] | ||
ostrio:[email protected].0 | ||
ostrio:[email protected].1 | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Start with: | ||
// (new FPSMeter({ui: true})).start(); | ||
|
||
if (!window.requestAnimationFrame) { | ||
window.requestAnimationFrame = (() => { | ||
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) { | ||
window.setTimeout(callback, 1000 / 60); | ||
}; | ||
})(); | ||
} | ||
|
||
const getTime = () => { | ||
return (window.performance && window.performance.now) ? window.performance.now() : +new Date(); | ||
}; | ||
|
||
class FPSMeter { | ||
constructor(opts) { | ||
this.ui = opts.ui || false; | ||
this.fps = 0; | ||
this.isRunning = false; | ||
this.defaultStyles = 'z-index:999;position:fixed;bottom:0;left:0;padding:10px;font-weight:600;font-style:normal;font-size:12px;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",Courier,monospace,sans-serif'; | ||
} | ||
|
||
measure() { | ||
const time = getTime(); | ||
window.requestAnimationFrame(() => { | ||
const _fps = Math.round((1 / (getTime() - time)) * 1000); | ||
|
||
this.fps = _fps; | ||
|
||
if (this.ui && this.element) { | ||
let i = 4 - `${_fps}`.length; | ||
let pad = ''; | ||
|
||
while (i > 0) { | ||
pad += ' '; | ||
i--; | ||
} | ||
|
||
this.element.innerHTML = `${_fps}${pad}fps`; | ||
|
||
switch (false) { | ||
case !(_fps < 7): | ||
this.element.style.color = '#FFF'; | ||
this.element.style.backgroundColor = '#FF4500'; | ||
break; | ||
case !(_fps < 25): | ||
this.element.style.color = '#FF4500'; | ||
this.element.style.backgroundColor = '#000'; | ||
break; | ||
case !(_fps < 40): | ||
this.element.style.color = 'orange'; | ||
this.element.style.backgroundColor = '#000'; | ||
break; | ||
case !(_fps > 70): | ||
this.element.style.color = '#0f0'; | ||
this.element.style.backgroundColor = '#000'; | ||
break; | ||
default: | ||
this.element.style.color = '#018801'; | ||
this.element.style.backgroundColor = '#000'; | ||
break; | ||
} | ||
} | ||
|
||
if (this.isRunning) { | ||
this.measure(); | ||
} | ||
}); | ||
} | ||
|
||
start() { | ||
if (!this.isRunning) { | ||
this.isRunning = true; | ||
|
||
if (this.ui === true) { | ||
this.element = document.createElement('div'); | ||
this.element.style = this.defaultStyles; | ||
document.body.appendChild(this.element); | ||
} | ||
|
||
this.measure(); | ||
} | ||
} | ||
|
||
pause() { | ||
this.isRunning = false; | ||
} | ||
|
||
resume() { | ||
if (!this.isRunning) { | ||
this.isRunning = true; | ||
this.measure(); | ||
} | ||
} | ||
|
||
toggle() { | ||
this.isRunning ? this.pause() : this.resume(); | ||
} | ||
|
||
stop() { | ||
if (this.isRunning) { | ||
this.isRunning = false; | ||
if (this.ui === true && this.element) { | ||
this.element.remove(); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
if (!window.requestAnimationFrame) { | ||
window.requestAnimationFrame = (() => { | ||
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) { | ||
window.setTimeout(callback, 1000 / 60); | ||
}; | ||
})(); | ||
} | ||
|
||
const getTime = () => { | ||
return (window.performance && window.performance.now) ? window.performance.now() : +new Date(); | ||
}; | ||
|
||
module.exports = class FPSMeter { | ||
constructor(opts) { | ||
this.ui = opts.ui || false; | ||
this.fps = 0; | ||
this.isRunning = false; | ||
this.defaultStyles = 'z-index:999;position:fixed;bottom:0;left:0;padding:10px;font-weight:600;font-style:normal;font-size:12px;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",Courier,monospace,sans-serif'; | ||
} | ||
|
||
measure() { | ||
const time = getTime(); | ||
window.requestAnimationFrame(() => { | ||
const _fps = Math.round((1 / (getTime() - time)) * 1000); | ||
|
||
this.fps = _fps; | ||
|
||
if (this.ui && this.element) { | ||
let i = 4 - `${_fps}`.length; | ||
let pad = ''; | ||
|
||
while (i > 0) { | ||
pad += ' '; | ||
i--; | ||
} | ||
|
||
this.element.innerHTML = `${_fps}${pad}fps`; | ||
|
||
switch (false) { | ||
case !(_fps < 7): | ||
this.element.style.color = '#FFF'; | ||
this.element.style.backgroundColor = '#FF4500'; | ||
break; | ||
case !(_fps < 25): | ||
this.element.style.color = '#FF4500'; | ||
this.element.style.backgroundColor = '#000'; | ||
break; | ||
case !(_fps < 40): | ||
this.element.style.color = 'orange'; | ||
this.element.style.backgroundColor = '#000'; | ||
break; | ||
case !(_fps > 70): | ||
this.element.style.color = '#0f0'; | ||
this.element.style.backgroundColor = '#000'; | ||
break; | ||
default: | ||
this.element.style.color = '#018801'; | ||
this.element.style.backgroundColor = '#000'; | ||
break; | ||
} | ||
} | ||
|
||
if (this.isRunning) { | ||
this.measure(); | ||
} | ||
}); | ||
} | ||
|
||
start() { | ||
if (!this.isRunning) { | ||
this.isRunning = true; | ||
|
||
if (this.ui === true) { | ||
this.element = document.createElement('div'); | ||
this.element.style = this.defaultStyles; | ||
document.body.appendChild(this.element); | ||
} | ||
|
||
this.measure(); | ||
} | ||
} | ||
|
||
pause() { | ||
this.isRunning = false; | ||
} | ||
|
||
resume() { | ||
if (!this.isRunning) { | ||
this.isRunning = true; | ||
this.measure(); | ||
} | ||
} | ||
|
||
toggle() { | ||
this.isRunning ? this.pause() : this.resume(); | ||
} | ||
|
||
stop() { | ||
if (this.isRunning) { | ||
this.isRunning = false; | ||
if (this.ui === true && this.element) { | ||
this.element.remove(); | ||
} | ||
} | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.