utils.js (2116B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 // The maximum number of times we can loop before we find the optimal time interval in the 8 // timeline graph. 9 const OPTIMAL_TIME_INTERVAL_MAX_ITERS = 100; 10 // Time graduations should be multiple of one of these number. 11 const OPTIMAL_TIME_INTERVAL_MULTIPLES = [1, 2.5, 5]; 12 13 /** 14 * Find the optimal interval between time graduations in the animation timeline 15 * graph based on a minimum time interval. 16 * 17 * @param {number} minTimeInterval 18 * Minimum time in ms in one interval 19 * @return {number} The optimal interval time in ms 20 */ 21 function findOptimalTimeInterval(minTimeInterval) { 22 if (!minTimeInterval) { 23 return 0; 24 } 25 26 let numIters = 0; 27 let multiplier = 1; 28 let interval; 29 30 while (true) { 31 for (let i = 0; i < OPTIMAL_TIME_INTERVAL_MULTIPLES.length; i++) { 32 interval = OPTIMAL_TIME_INTERVAL_MULTIPLES[i] * multiplier; 33 34 if (minTimeInterval <= interval) { 35 return interval; 36 } 37 } 38 39 if (++numIters > OPTIMAL_TIME_INTERVAL_MAX_ITERS) { 40 return interval; 41 } 42 43 multiplier *= 10; 44 } 45 } 46 47 /** 48 * Check whether or not the given list of animations has an iteration count of infinite. 49 * 50 * @param {Array} animations. 51 * @return {boolean} true if there is an animation in the list of animations 52 * whose animation iteration count is infinite. 53 */ 54 function hasAnimationIterationCountInfinite(animations) { 55 return animations.some(({ state }) => !state.iterationCount); 56 } 57 58 /** 59 * Check wether the animations are running at least one. 60 * 61 * @param {Array} animations. 62 * @return {boolean} true: running 63 */ 64 function hasRunningAnimation(animations) { 65 return animations.some(({ state }) => state.playState === "running"); 66 } 67 68 exports.findOptimalTimeInterval = findOptimalTimeInterval; 69 exports.hasAnimationIterationCountInfinite = hasAnimationIterationCountInfinite; 70 exports.hasRunningAnimation = hasRunningAnimation;