var Class = require('../utils/Class'); var NOOP = require('../utils/NOOP'); var RequestAnimationFrame = new Class({ initialize: function RequestAnimationFrame(){ this.isRunning = false ; this.callback = NOOP; this.tick = 0; this.isSetTimeOut = false ; this.timeOutID = null ; this.lastTime = 0; _AN_Write_target('target', this, false , 0); var _this = this; this.step = function step(){ var timestamp = window.performance.now(); _this.lastTime = _this.tick; _this.tick = timestamp; _this.callback(timestamp); _this.timeOutID = window.requestAnimationFrame(step); } ; this.stepTimeout = function stepTimeout(){ var d = Date.now(); var delay = Math.min(Math.max(_AN_Read_target('target', _this) * 2 + _this.tick - d, 0), _AN_Read_target('target', _this)); _this.lastTime = _this.tick; _this.tick = d; _this.callback(d); _this.timeOutID = _AN_Call_settimeout('setTimeout', window, stepTimeout, delay); } ; } , start: function (callback, forceSetTimeOut, targetFPS){ if (this.isRunning) { return ; } this.callback = callback; this.isSetTimeOut = forceSetTimeOut; _AN_Write_target('target', this, false , targetFPS); this.isRunning = true ; this.timeOutID = (forceSetTimeOut)? _AN_Call_settimeout('setTimeout', window, this.stepTimeout, 0): window.requestAnimationFrame(this.step); } , stop: function (){ this.isRunning = false ; if (this.isSetTimeOut) { clearTimeout(this.timeOutID); } else { window.cancelAnimationFrame(this.timeOutID); } } , destroy: function (){ this.stop(); this.callback = NOOP; } } ); module.exports = RequestAnimationFrame;