fps-emitter/index.js

97 строки
3.0 KiB
JavaScript
Исходник Обычный вид История

2016-07-06 21:36:04 +03:00
// --------------------------------------------------------------
2016-07-06 21:34:33 +03:00
//
// Microsoft Edge fps-emitter
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files(the ""Software""),
// to deal in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
2016-07-06 21:36:04 +03:00
// --------------------------------------------------------------
2016-06-29 23:49:18 +03:00
'use strict'
/* global performance */
2016-07-06 00:19:35 +03:00
var DEFAULT_UPDATE_INTERVAL = 1000
2016-06-29 23:49:18 +03:00
var raf = require('raf')
var EE = require('events').EventEmitter
var inherits = require('inherits')
inherits(FpsEmitter, EE)
2016-07-06 00:19:35 +03:00
function FpsEmitter (interval) {
2016-06-29 23:49:18 +03:00
if (!(this instanceof FpsEmitter)) {
2016-07-06 00:19:35 +03:00
return new FpsEmitter(interval)
2016-06-29 23:49:18 +03:00
}
EE.call(this)
2016-07-06 00:19:35 +03:00
this.setUpdateInterval(interval)
2016-06-29 23:49:18 +03:00
// avoid functions-within-functions, use bound functions for perf
this.__onTimer = this.__onTimer.bind(this)
this.__onRaf = this.__onRaf.bind(this)
this.__fps = 0
this.__total = 0
this.__samples = 0
2016-07-06 00:19:35 +03:00
this.__lastSample = this.__lastSampleBatch = performance.now()
2016-06-29 23:49:18 +03:00
// it's possible for raf to be called twice in a frame, hence setInterval(..., 16)
setInterval(this.__onTimer, 16)
}
FpsEmitter.prototype.__onTimer = function () {
raf(this.__onRaf)
}
FpsEmitter.prototype.__onRaf = function () {
// update the new timestamp and total intervals recorded
var newTS = performance.now()
2016-07-06 00:19:35 +03:00
this.__samples++
this.__total += newTS - this.__lastSample
if (newTS - this.__lastSampleBatch >= this.__interval) {
// calculate the rolling average
var fps = 1000 / (this.__total / this.__samples)
// clamp to 60, use ~~ as a fast Math.floor()
fps = fps > 60 ? 60 : ~~fps
if (this.__fps !== fps) {
// emit when changed
this.__fps = fps
this.emit('update', fps)
}
// reset
this.__total = 0
this.__samples = 0
this.__lastSampleBatch = newTS
2016-06-29 23:49:18 +03:00
}
2016-07-06 00:19:35 +03:00
this.__lastSample = newTS
}
FpsEmitter.prototype.setUpdateInterval = function (interval) {
this.__interval = typeof interval === 'number' && interval > 0
? interval : DEFAULT_UPDATE_INTERVAL
2016-06-29 23:49:18 +03:00
}
FpsEmitter.prototype.get = function () {
return this.__fps
}
module.exports = FpsEmitter