UNPKG

raf-timer

Version:

An abstraction around requestionAnimationFrame.

240 lines (191 loc) 7.55 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define([], factory); else if(typeof exports === 'object') exports["raf-timer"] = factory(); else root["raf-timer"] = factory(); })(this, function() { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); Object.defineProperty(exports, "__esModule", { value: true }); var _performance = __webpack_require__(1); var _performance2 = _interopRequireDefault(_performance); var _requestAnimationFramePolyfill = __webpack_require__(2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var numCallbacks = 0; var callbackID = 0; var animationFrameID = null; var callbacks = {}; var runCallbacks = function runCallbacks(dt) { animationFrameID = null; for (var key in callbacks) { callbacks[key](dt); delete callbacks[key]; } }; var Timer = function () { function Timer() { var deltaTimeLimit = arguments.length <= 0 || arguments[0] === undefined ? 0.25 : arguments[0]; _classCallCheck(this, Timer); this.microTime = (0, _performance2.default)(); this.deltaTime = 0; this.deltaTimeLimit = deltaTimeLimit; this.animationFrameID = null; } _createClass(Timer, [{ key: 'cancelFrame', value: function cancelFrame() { delete callbacks[this.animationFrameID]; if (numCallbacks === 0) { (0, _requestAnimationFramePolyfill.cancelAnimationFrame)(animationFrameID); animationFrameID = null; } } }, { key: 'nextFrame', value: function nextFrame(callback) { var id = callbackID++; numCallbacks++; callbacks[id] = callback; this.animationFrameID = id; if (animationFrameID === null) { animationFrameID = (0, _requestAnimationFramePolyfill.requestAnimationFrame)(runCallbacks); } } }, { key: 'step', value: function step() { var dt = (0, _performance2.default)() - this.microTime; this.deltaTime = Math.max(0, Math.min(this.deltaTimeLimit, dt / 1000)); return this.microTime += dt; } }, { key: 'delta', get: function get() { return this.deltaTime; } }, { key: 'fps', get: function get() { return this.deltaTime === 0 ? 0 : 1 / this.deltaTime; } }, { key: 'time', get: function get() { return this.microTime; } }]); return Timer; }(); exports.default = Timer; /***/ }, /* 1 */ /***/ function(module, exports) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); // @license http://opensource.org/licenses/MIT // copyright Paul Irish 2015 // Date.now() is supported everywhere except IE8. For IE8 we use the Date.now polyfill // github.com/Financial-Times/polyfill-service/blob/master/polyfills/Date.now/polyfill.js // as Safari 6 doesn't have support for NavigationTiming, we use a Date.now() timestamp for relative values // if you want values similar to what you'd get with real perf.now, place this towards the head of the page // but in reality, you're just getting the delta between now() calls, so it's not terribly important where it's placed var performance = 'performance' in window ? window.performance : {}; var dateNow = Date.now || function () { // thanks IE8 return new Date().getTime(); }; var now = undefined; if ('now' in performance == false) { var nowOffset = dateNow(); if (performance.timing && performance.timing.navigationStart) { nowOffset = performance.timing.navigationStart; } now = function now() { return dateNow() - nowOffset; }; } else { now = performance.now.bind(performance); } exports.default = now; /***/ }, /* 2 */ /***/ function(module, exports) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); // http://paulirish.com/2011/requestanimationframe-for-smart-animating/ // http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating // requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel // MIT license var requestAnimationFrame = exports.requestAnimationFrame = window.requestAnimationFrame; var cancelAnimationFrame = exports.cancelAnimationFrame = window.cancelAnimationFrame; var lastTime = 0; var vendors = ['ms', 'moz', 'webkit', 'o']; for (var x = 0; x < vendors.length && !requestAnimationFrame; ++x) { exports.requestAnimationFrame = requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame']; exports.cancelAnimationFrame = cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame']; } if (!requestAnimationFrame) { exports.requestAnimationFrame = requestAnimationFrame = function requestAnimationFrame(callback) { var currTime = new Date().getTime(); var timeToCall = Math.max(0, 16 - (currTime - lastTime)); var id = window.setTimeout(function () { callback(currTime + timeToCall); }, timeToCall); lastTime = currTime + timeToCall; return id; }; } if (!cancelAnimationFrame) { exports.cancelAnimationFrame = cancelAnimationFrame = function cancelAnimationFrame(id) { clearTimeout(id); }; } /***/ } /******/ ]) }); ;