UNPKG

frame-duration

Version:

A lib for calculating the duration of frame dynamically, based on requestAnimationFrame, default is 1000 / 30. The actual duration will be calculated after 15 frames.

49 lines (42 loc) 1.2 kB
/** * Bundle of frame-duration * Generated: 2020-05-01 * Version: 1.1.0 * License: MIT * Author: 2631541504@qq.com */ var root = typeof window === 'undefined' ? global || {} : window; var FrameDuration = { times: 1, duration: 1000 / 30, update: function update(currDuration) { // frameDuration usually not less than 1000 / 30 // and not greater than 1000 / 120 var $currDuration = Math.max(1000 / 120, Math.min(1000 / 30, currDuration)); this.times += 1; this.duration = (this.duration + $currDuration) / 2; }, /** * @return { Promise<number> } actual duration * */ correct: function correct() { var i = 0; var last; return new Promise(function (res) { (function init() { if (typeof root.requestAnimationFrame === 'function') { root.requestAnimationFrame(function (now) { var $now = now || new Date().getTime(); if (last) { FrameDuration.update($now - last); } last = $now; if (++i < 15) init();else res(FrameDuration.duration); }); } })(); }); } }; FrameDuration.correct(); export default FrameDuration;