Skip to content

Instantly share code, notes, and snippets.

@vasiliy-pdk
Created November 6, 2015 21:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasiliy-pdk/70e0f54a20c88e31f6b8 to your computer and use it in GitHub Desktop.
Save vasiliy-pdk/70e0f54a20c88e31f6b8 to your computer and use it in GitHub Desktop.
The simplest FPS meter for playcanvas
pc.script.create('fpsCounter', function (app) {
// Creates a new FpsCounter instance
var FpsCounter = function (entity) {
this.entity = entity; // The scene object
this.fpsCount = 0;
this.counterEl = null;
this.insertBeforeElId = "application-canvas";
};
FpsCounter.prototype = {
// Called once after all resources are loaded and before the first update
initialize: function () {
this.counterEl = this.initCounterEl();
},
// Called every frame, dt is time in seconds since last update
update: function (dt) {
this.fpsCount = this.culculateFpsCount(dt);
this.setCounterValue(this.fpsCount);
},
initCounterEl: function() {
var insertBeforeEl = document.getElementById(this.insertBeforeElId);
var el = document.createElement('div');
document.body.insertBefore(el, insertBeforeEl);
// @TODO: better use css class with all this rules instead of
// hardcoding them here
el.style.position = "fixed";
el.style.top = "20px";
el.style.left = "20px";
el.style.width = "100px";
el.style.height = "50px";
el.style.zIndex = 1000;
document.body.insertBefore(el, insertBeforeEl);
return el;
},
setCounterValue: function(newVal) {
this.counterEl.innerHTML = "<h1>" + newVal + "</h1>";
},
culculateFpsCount: function(dt) {
var fpsCount = (1/dt).toFixed(2);
return fpsCount;
}
};
return FpsCounter;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment