UNPKG

@visactor/vrender-core

Version:
134 lines (129 loc) 5.26 kB
import { pi2 } from "@visactor/vutils"; export class Easing { constructor() {} static linear(t) { return t; } static none() { return this.linear; } static get(amount) { return amount < -1 ? amount = -1 : amount > 1 && (amount = 1), function(t) { return 0 === amount ? t : amount < 0 ? t * (t * -amount + 1 + amount) : t * ((2 - t) * amount + (1 - amount)); }; } static getPowIn(pow) { return function(t) { return Math.pow(t, pow); }; } static getPowOut(pow) { return function(t) { return 1 - Math.pow(1 - t, pow); }; } static getPowInOut(pow) { return function(t) { return (t *= 2) < 1 ? .5 * Math.pow(t, pow) : 1 - .5 * Math.abs(Math.pow(2 - t, pow)); }; } static getBackIn(amount) { return function(t) { return t * t * ((amount + 1) * t - amount); }; } static getBackOut(amount) { return function(t) { return --t * t * ((amount + 1) * t + amount) + 1; }; } static getBackInOut(amount) { return amount *= 1.525, function(t) { return (t *= 2) < 1 ? t * t * ((amount + 1) * t - amount) * .5 : .5 * ((t -= 2) * t * ((amount + 1) * t + amount) + 2); }; } static sineIn(t) { return 1 - Math.cos(t * Math.PI / 2); } static sineOut(t) { return Math.sin(t * Math.PI / 2); } static sineInOut(t) { return -(Math.cos(Math.PI * t) - 1) / 2; } static expoIn(t) { return 0 === t ? 0 : Math.pow(2, 10 * t - 10); } static expoOut(t) { return 1 === t ? 1 : 1 - Math.pow(2, -10 * t); } static expoInOut(t) { return 0 === t ? 0 : 1 === t ? 1 : t < .5 ? Math.pow(2, 20 * t - 10) / 2 : (2 - Math.pow(2, -20 * t + 10)) / 2; } static circIn(t) { return -(Math.sqrt(1 - t * t) - 1); } static circOut(t) { return Math.sqrt(1 - --t * t); } static circInOut(t) { return (t *= 2) < 1 ? -.5 * (Math.sqrt(1 - t * t) - 1) : .5 * (Math.sqrt(1 - (t -= 2) * t) + 1); } static bounceOut(t) { return t < 1 / 2.75 ? 7.5625 * t * t : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75 : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375 : 7.5625 * (t -= 2.625 / 2.75) * t + .984375; } static bounceIn(t) { return 1 - Easing.bounceOut(1 - t); } static bounceInOut(t) { return t < .5 ? .5 * Easing.bounceIn(2 * t) : .5 * Easing.bounceOut(2 * t - 1) + .5; } static getElasticIn(amplitude, period) { return function(t) { if (0 === t || 1 === t) return t; const s = period / pi2 * Math.asin(1 / amplitude); return -amplitude * Math.pow(2, 10 * (t -= 1)) * Math.sin((t - s) * pi2 / period); }; } static getElasticOut(amplitude, period) { return function(t) { if (0 === t || 1 === t) return t; const s = period / pi2 * Math.asin(1 / amplitude); return amplitude * Math.pow(2, -10 * t) * Math.sin((t - s) * pi2 / period) + 1; }; } static getElasticInOut(amplitude, period) { return function(t) { const s = period / pi2 * Math.asin(1 / amplitude); return (t *= 2) < 1 ? amplitude * Math.pow(2, 10 * (t -= 1)) * Math.sin((t - s) * pi2 / period) * -.5 : amplitude * Math.pow(2, -10 * (t -= 1)) * Math.sin((t - s) * pi2 / period) * .5 + 1; }; } static registerFunc(name, func) { Easing[name] = func; } } function flicker(t, n) { const step = 1 / n; let flag = 1; for (;t > step; ) t -= step, flag *= -1; const v = flag * t / step; return v > 0 ? v : 1 + v; } Easing.quadIn = Easing.getPowIn(2), Easing.quadOut = Easing.getPowOut(2), Easing.quadInOut = Easing.getPowInOut(2), Easing.cubicIn = Easing.getPowIn(3), Easing.cubicOut = Easing.getPowOut(3), Easing.cubicInOut = Easing.getPowInOut(3), Easing.quartIn = Easing.getPowIn(4), Easing.quartOut = Easing.getPowOut(4), Easing.quartInOut = Easing.getPowInOut(4), Easing.quintIn = Easing.getPowIn(5), Easing.quintOut = Easing.getPowOut(5), Easing.quintInOut = Easing.getPowInOut(5), Easing.backIn = Easing.getBackIn(1.7), Easing.backOut = Easing.getBackOut(1.7), Easing.backInOut = Easing.getBackInOut(1.7), Easing.elasticIn = Easing.getElasticIn(1, .3), Easing.elasticOut = Easing.getElasticOut(1, .3), Easing.elasticInOut = Easing.getElasticInOut(1, .3 * 1.5), Easing.easeInOutQuad = t => (t /= .5) < 1 ? .5 * Math.pow(t, 2) : -.5 * ((t -= 2) * t - 2), Easing.easeOutElastic = x => { const c4 = 2 * Math.PI / 3; return 0 === x ? 0 : 1 === x ? 1 : Math.pow(2, -10 * x) * Math.sin((10 * x - .75) * c4) + 1; }, Easing.easeInOutElastic = x => { const c5 = 2 * Math.PI / 4.5; return 0 === x ? 0 : 1 === x ? 1 : x < .5 ? -Math.pow(2, 20 * x - 10) * Math.sin((20 * x - 11.125) * c5) / 2 : Math.pow(2, -20 * x + 10) * Math.sin((20 * x - 11.125) * c5) / 2 + 1; }; for (let i = 0; i < 10; i++) Easing[`flicker${i}`] = t => flicker(t, i); for (let i = 2; i < 10; i++) Easing[`aIn${i}`] = t => i * t * t + (1 - i) * t; //# sourceMappingURL=easing.js.map