motion
Version:
The Motion library for the web
32 lines (29 loc) • 979 B
JavaScript
import { getEasingFunction } from '../targets/js/easing/get-function.es.js';
function stagger(duration = 0.1, { start = 0, from = 0, easing } = {}) {
return (i, total) => {
const fromIndex = typeof from === "number" ? from : getFromIndex(from, total);
const distance = Math.abs(fromIndex - i);
let delay = duration * distance;
if (easing) {
const maxDelay = total * i;
const easingFunction = getEasingFunction(easing);
delay = easingFunction(delay / maxDelay) * maxDelay;
}
return start + delay;
};
}
function getFromIndex(from, total) {
if (from === "first") {
return 0;
}
else {
const lastIndex = total - 1;
return from === "last" ? lastIndex : lastIndex / 2;
}
}
function resolveOption(option, i, total) {
return typeof option === "function"
? option(i, total)
: option;
}
export { getFromIndex, resolveOption, stagger };