加权缓动
这是一个有用的函数,它允许你将一个介于 -1 和 1 之间的比例值传递给任何标准(不可配置的)缓动函数,使它们在一个方向或另一个方向上“加权”,就像将缓动曲线拉向起点或终点一样。比例值为 "0" 表示不偏向任何一方,-1 表示偏向该缓动函数的“入”部分,1 表示偏向“出”部分。
function addWeightedEases() {
let eases = gsap.parseEase(),
createConfig = (ease) => (ratio) => {
let y = 0.5 + ratio / 2;
return (p) => ease(2 * (1 - p) * p * y + p * p);
};
for (let p in eases) {
if (!eases[p].config) {
eases[p].config = createConfig(eases[p]);
}
}
}
//example usage:
ease: "power2.inOut(0.5)"; // weighted halfway to the "out" portion
ease: "power2.inOut(-0.2)"; // weighted slightly to the "in" portion
ease: "power2.inOut(-1)"; // weighted ALL THE WAY to the "in" portion
ease: "power2.inOut(1)"; // weighted ALL THE WAY to the "out" portion
一旦运行了这个函数一次之后,你可以像这样通过添加括号并提供一个数字来配置几乎任何标准缓动函数power2.inOut
或者power1.in
(或其它名称)的方式,指定你希望在某一方向如何对该缓动函数进行加权。再次强调一下,它适用于所有不带已有配置选项的标准缓动函数(例如 "steps()", "slow()" 等)。