weex-nuke
Version:
基于 Rax 、Weex 的高性能组件体系 ~~
95 lines (70 loc) • 2.08 kB
JavaScript
let counter = 1;
const running = {};
export const Animate = {
requestAnimationFrame: (() => {
// isomorphic support
if (typeof window === 'undefined') {
return () => { };
}
const requestFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame;
return (callback) => {
requestFrame(callback);
};
})(),
start(stepFunc, callback, duration, easeFunc) {
const { max, min } = Math;
const animateId = counter++;
let startTime = null, lastTime = null;
running[animateId] = true;
const step = (timeStamp) => {
if (running[animateId] !== true) {
return;
}
if (startTime === null) {
startTime = timeStamp;
lastTime = timeStamp;
}
let percent = 0,
timeDiff = timeStamp - startTime,
timeFrame = timeStamp - lastTime;
lastTime = timeStamp;
if (duration) {
percent = min(timeDiff / duration, 1);
}
if (percent >= 1 || running[animateId] !== true) {
stepFunc(percent, timeDiff, timeFrame);
callback();
running[animateId] = false;
delete running[animateId];
return;
}
if (easeFunc) {
percent = easeFunc(timeDiff, 0, 1, duration);
}
stepFunc(percent, timeDiff, timeFrame);
Animate.requestAnimationFrame(step);
};
Animate.requestAnimationFrame(step);
return animateId;
},
stop(animateId) {
running[animateId] = false;
delete running[animateId];
}
}
export function easeOutBounce(t, b, c, d) {
if ((t /= d) < (1 / 2.75)) {
return c * (7.5625 * t * t) + b;
} else if (t < (2 / 2.75)) {
return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
} else if (t < (2.5 / 2.75)) {
return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
} else {
return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
}
}
export function easeInOutQuad(t, b, c, d) {
if ((t /= d / 2) < 1) return c / 2 * t * t + b;
return - c / 2 * ((--t) * (t - 2) - 1) + b;
}
;