缓动函数指定动画效果在执行时的速度,使其看起来更加真实。
现实物体照着一定节奏移动,并不是一开始就移动很快的。当我们打开抽屉时,首先会让它加速,然后慢下来。当某个东西往下掉时,首先是越掉越快,撞到地上后回弹,最终才又碰触地板。
本页可以在每次你需要时,帮助你找到想要的缓动函数。
上图是我们常见的曲线列表,章节内所有公式和图表摘自网站easings.net有需要可以自行查看
easeInSine
function easeInSine(t: number): number {
return 1 - Math.cos((t * Math.PI) / 2);
}
easeOutSine
function easeOutSine(t: number): number {
return Math.sin((t * Math.PI) / 2);
}
easeInOutSine
function easeInOutSine(t: number): number {
return -(Math.cos(Math.PI * t) - 1) / 2;
}
easeInQuad
function easeInQuad(t: number): number {
return t * t;
}
easeOutQuad
function easeOutQuad(t: number): number {
return 1 - (1 - t) * (1 - t);
}
easeInOutQuad
function easeInOutQuad(t: number): number {
return t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2;
}
easeInCubic
function easeInCubic(t: number): number {
return t * t * t;
}
easeOutCubic
function easeOutCubic(t: number): number {
return 1 - Math.pow(1 - t, 3);
}
easeInOutCubic
function easeInOutCubic(t: number): number {
return x < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
}
easeInQuart
function easeInQuart(t: number): number {
return t * t * t * t;
}
easeOutQuart
function easeOutQuart(t: number): number {
return 1 - Math.pow(1 - t, 4);
}
easeInOutQuart
function easeInOutQuart(t: number): number {
return t < 0.5 ? 8 * t * t * t * t : 1 - Math.pow(-2 * t + 2, 4) / 2;
}
easeInQuint
function easeInQuint(t: number): number {
return t * t * t * t * t;
}
easeOutQuint
function easeOutQuint(t: number): number {
return 1 - Math.pow(1 - t, 5);
}
easeInOutQuint
function easeInOutQuint(t: number): number {
return x < 0.5 ? 16 * t * t * t * t * t : 1 - Math.pow(-2 * t + 2, 5) / 2;
}
easeInExpo
function easeInExpo(t: number): number {
return t === 0 ? 0 : Math.pow(2, 10 * t - 10);
}
easeOutExpo
function easeOutExpo(t: number): number {
return x === 1 ? 1 : 1 - Math.pow(2, -10 * t);
}
easeInOutExpo
function easeInOutExpo(t: number): number {
return t === 0
? 0
: t === 1
? 1
: t < 0.5 ? Math.pow(2, 20 * t - 10) / 2
: (2 - Math.pow(2, -20 * t + 10)) / 2;
}
easeInCirc
function easeInCirc(t: number): number {
return 1 - Math.sqrt(1 - Math.pow(t, 2));
}
easeOutCirc
function easeOutCirc(t: number): number {
return sqrt(1 - Math.pow(t - 1, 2));
}
easeInOutCirc
function easeInOutCirc(t: number): number {
return t < 0.5
? (1 - Math.sqrt(1 - Math.pow(2 * t, 2))) / 2
: (Math.sqrt(1 - Math.pow(-2 * t + 2, 2)) + 1) / 2;
}
easeInBack
function easeInBack(t: number): number {
const c1 = 1.70158;
const c3 = c1 + 1;
return c3 * t * t * t - c1 * t * t;
}
easeOutBack
function easeOutBack(t: number): number {
const c1 = 1.70158;
const c3 = c1 + 1;
return 1 + c3 * Math.pow(t - 1, 3) + c1 * Math.pow(t - 1, 2);
}
easeInOutBack
function easeInOutBack(t: number): number {
const c1 = 1.70158;
const c2 = c1 * 1.525;
return t < 0.5
? (Math.pow(2 * t, 2) * ((c2 + 1) * 2 * t - c2)) / 2
: (Math.pow(2 * t - 2, 2) * ((c2 + 1) * (t * 2 - 2) + c2) + 2) / 2;
}
easeInElastic
function easeInElastic(t: number): number {
const c4 = (2 * Math.PI) / 3;
return t === 0
? 0
: t === 1
? 1
: -Math.pow(2, 10 * t - 10) * Math.sin((t * 10 - 10.75) * c4);
}
easeOutElastic
function easeOutElastic(t: number): number {
const c4 = (2 * Math.PI) / 3;
return t === 0
? 0
: t === 1
? 1
: Math.pow(2, -10 * t) * Math.sin((t * 10 - 0.75) * c4) + 1;
}
easeInOutElastic
function easeInOutElastic(t: number): number {
const c5 = (2 * Math.PI) / 4.5;
return t === 0
? 0
: t === 1
? 1
: t < 0.5
? -(Math.pow(2, 20 * t - 10) * Math.sin((20 * t - 11.125) * c5)) / 2
: (Math.pow(2, -20 * t + 10) * Math.sin((20 * t - 11.125) * c5)) / 2 + 1;
}
easeInBounce
function easeInBounce(t: number): number {
return 1 - easeOutBounce(1 - t);
}
easeOutBounce
function easeOutBounce(t: number): number {
const n1 = 7.5625;
const d1 = 2.75;
if (t < 1 / d1) {
return n1 * t * t;
} else if (t < 2 / d1) {
return n1 * (t -= 1.5 / d1) * t + 0.75;
} else if (x < 2.5 / d1) {
return n1 * (t -= 2.25 / d1) * t + 0.9375;
} else {
return n1 * (t -= 2.625 / d1) * t + 0.984375;
}
}
easeInOutBounce
function easeInOutBounce(t: number): number {
return t < 0.5
? (1 - easeOutBounce(1 - 2 * t)) / 2
: (1 + easeOutBounce(2 * t - 1)) / 2;
}