Skip to content

Commit

Permalink
typed anim_ease
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur committed Sep 2, 2022
1 parent 7c99ce5 commit cd54755
Showing 1 changed file with 48 additions and 43 deletions.
91 changes: 48 additions & 43 deletions src/util/anim_ease.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
//@ts-nocheck
import { twoMathPi, halfPI } from "../constants";

type TEasingFunction = (
currentTime: number,
startValue: number,
byValue: number,
duration: number) => number;

/**
* Easing functions
* See <a href="http://gizma.com/easing/">Easing Equations by Robert Penner</a>
* @namespace fabric.util.ease
*/

const normalize = (a, c, p, s) => {
const normalize = (a: number, c: number, p: number, s: number) => {
if (a < Math.abs(c)) {
a = c;
s = p / 4;
Expand All @@ -21,24 +26,24 @@ const normalize = (a, c, p, s) => {
s = p / twoMathPi * Math.asin(c / a);
}
}
return { a: a, c: c, p: p, s: s };
return { a, c, p, s };
}

const elastic = (opts, t, d) => opts.a *
const elastic = (a: number, s: number, p: number, t: number, d: number): number => a *
Math.pow(2, 10 * (t -= 1)) *
Math.sin((t * d - opts.s) * twoMathPi / opts.p);
Math.sin((t * d - s) * twoMathPi / p);

/**
* Cubic easing out
* @memberOf fabric.util.ease
*/
export const easeOutCubic = (t, b, c, d) => c * ((t /= d - 1) * t ** 2 + 1) + b;
export const easeOutCubic:TEasingFunction = (t, b, c, d) => c * ((t /= d - 1) * t ** 2 + 1) + b;

/**
* Cubic easing in and out
* @memberOf fabric.util.ease
*/
export const easeInOutCubic = (t, b, c, d) => {
export const easeInOutCubic:TEasingFunction = (t, b, c, d) => {
t /= d / 2;
if (t < 1) {
return c / 2 * t ** 3 + b;
Expand All @@ -50,19 +55,19 @@ export const easeInOutCubic = (t, b, c, d) => {
* Quartic easing in
* @memberOf fabric.util.ease
*/
export const easeInQuart = (t, b, c, d) => c * (t /= d) * t ** 3 + b;
export const easeInQuart:TEasingFunction = (t, b, c, d) => c * (t /= d) * t ** 3 + b;

/**
* Quartic easing out
* @memberOf fabric.util.ease
*/
export const easeOutQuart = (t, b, c, d) => -c * ((t = t / d - 1) * t ** 3 - 1) + b;
export const easeOutQuart:TEasingFunction = (t, b, c, d) => -c * ((t = t / d - 1) * t ** 3 - 1) + b;

/**
* Quartic easing in and out
* @memberOf fabric.util.ease
*/
export const easeInOutQuart = (t, b, c, d) => {
export const easeInOutQuart:TEasingFunction = (t, b, c, d) => {
t /= d / 2;
if (t < 1) {
return c / 2 * t ** 4 + b;
Expand All @@ -74,19 +79,19 @@ export const easeInOutQuart = (t, b, c, d) => {
* Quintic easing in
* @memberOf fabric.util.ease
*/
export const easeInQuint = (t, b, c, d) => c * (t /= d) * t ** 4 + b;
export const easeInQuint:TEasingFunction = (t, b, c, d) => c * (t /= d) * t ** 4 + b;

/**
* Quintic easing out
* @memberOf fabric.util.ease
*/
export const easeOutQuint = (t, b, c, d) => c * ((t /= d - 1) * t ** 4 + 1) + b;
export const easeOutQuint:TEasingFunction = (t, b, c, d) => c * ((t /= d - 1) * t ** 4 + 1) + b;

/**
* Quintic easing in and out
* @memberOf fabric.util.ease
*/
export const easeInOutQuint = (t, b, c, d) => {
export const easeInOutQuint:TEasingFunction = (t, b, c, d) => {
t /= d / 2;
if (t < 1) {
return c / 2 * t ** 5 + b;
Expand All @@ -98,37 +103,37 @@ export const easeInOutQuint = (t, b, c, d) => {
* Sinusoidal easing in
* @memberOf fabric.util.ease
*/
export const easeInSine = (t, b, c, d) => -c * Math.cos(t / d * halfPI) + c + b;
export const easeInSine:TEasingFunction = (t, b, c, d) => -c * Math.cos(t / d * halfPI) + c + b;

/**
* Sinusoidal easing out
* @memberOf fabric.util.ease
*/
export const easeOutSine = (t, b, c, d) => c * Math.sin(t / d * halfPI) + b;
export const easeOutSine:TEasingFunction = (t, b, c, d) => c * Math.sin(t / d * halfPI) + b;

/**
* Sinusoidal easing in and out
* @memberOf fabric.util.ease
*/
export const easeInOutSine = (t, b, c, d) => -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
export const easeInOutSine:TEasingFunction = (t, b, c, d) => -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;

/**
* Exponential easing in
* @memberOf fabric.util.ease
*/
export const easeInExpo = (t, b, c, d) => (t === 0) ? b : c * 2 ** (10 * (t / d - 1)) + b;
export const easeInExpo:TEasingFunction = (t, b, c, d) => (t === 0) ? b : c * 2 ** (10 * (t / d - 1)) + b;

/**
* Exponential easing out
* @memberOf fabric.util.ease
*/
export const easeOutExpo = (t, b, c, d) => (t === d) ? b + c : c * -(2 ** (-10 * t / d) + 1) + b;
export const easeOutExpo:TEasingFunction = (t, b, c, d) => (t === d) ? b + c : c * -(2 ** (-10 * t / d) + 1) + b;

/**
* Exponential easing in and out
* @memberOf fabric.util.ease
*/
export const easeInOutExpo = (t, b, c, d) => {
export const easeInOutExpo:TEasingFunction = (t, b, c, d) => {
if (t === 0) {
return b;
}
Expand All @@ -146,19 +151,19 @@ export const easeInOutExpo = (t, b, c, d) => {
* Circular easing in
* @memberOf fabric.util.ease
*/
export const easeInCirc = (t, b, c, d) => -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
export const easeInCirc:TEasingFunction = (t, b, c, d) => -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;

/**
* Circular easing out
* @memberOf fabric.util.ease
*/
export const easeOutCirc = (t, b, c, d) => c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
export const easeOutCirc:TEasingFunction = (t, b, c, d) => c * Math.sqrt(1 - (t = t / d - 1) * t) + b;

/**
* Circular easing in and out
* @memberOf fabric.util.ease
*/
export const easeInOutCirc = (t, b, c, d) => {
export const easeInOutCirc:TEasingFunction = (t, b, c, d) => {
t /= d / 2;
if (t < 1) {
return -c / 2 * (Math.sqrt(1 - t ** 2) - 1) + b;
Expand All @@ -170,7 +175,7 @@ export const easeInOutCirc = (t, b, c, d) => {
* Elastic easing in
* @memberOf fabric.util.ease
*/
export const easeInElastic = (t, b, c, d) => {
export const easeInElastic:TEasingFunction = (t, b, c, d) => {
const s = 1.70158, a = c;
let p = 0;
if (t === 0) {
Expand All @@ -183,15 +188,15 @@ export const easeInElastic = (t, b, c, d) => {
if (!p) {
p = d * 0.3;
}
const opts = normalize(a, c, p, s);
return -elastic(opts, t, d) + b;
const { a: normA, s: normS, p: normP } = normalize(a, c, p, s);
return -elastic(normA, normS, normP, t, d) + b;
}

/**
* Elastic easing out
* @memberOf fabric.util.ease
*/
export const easeOutElastic = (t, b, c, d) => {
export const easeOutElastic:TEasingFunction = (t, b, c, d) => {
const s = 1.70158, a = c;
let p = 0;
if (t === 0) {
Expand All @@ -204,15 +209,15 @@ export const easeOutElastic = (t, b, c, d) => {
if (!p) {
p = d * 0.3;
}
const opts = normalize(a, c, p, s);
return opts.a * 2 ** (-10 * t) * Math.sin((t * d - opts.s) * (twoMathPi) / opts.p ) + opts.c + b;
const { a: normA, s: normS, p: normP, c: normC } = normalize(a, c, p, s);
return normA * 2 ** (-10 * t) * Math.sin((t * d - normS) * (twoMathPi) / normP ) + normC + b;
}

/**
* Elastic easing in and out
* @memberOf fabric.util.ease
*/
export const easeInOutElastic = (t, b, c, d) => {
export const easeInOutElastic:TEasingFunction = (t, b, c, d) => {
const s = 1.70158, a = c;
let p = 0;
if (t === 0) {
Expand All @@ -225,31 +230,31 @@ export const easeInOutElastic = (t, b, c, d) => {
if (!p) {
p = d * (0.3 * 1.5);
}
const opts = normalize(a, c, p, s);
const { a: normA, s: normS, p: normP, c: normC } = normalize(a, c, p, s);
if (t < 1) {
return -0.5 * elastic(opts, t, d) + b;
return -0.5 * elastic(normA, normS, normP, t, d) + b;
}
return opts.a * Math.pow(2, -10 * (t -= 1)) *
Math.sin((t * d - opts.s) * (twoMathPi) / opts.p ) * 0.5 + opts.c + b;
return normA * Math.pow(2, -10 * (t -= 1)) *
Math.sin((t * d - normS) * (twoMathPi) / normP ) * 0.5 + normC + b;
}

/**
* Backwards easing in
* @memberOf fabric.util.ease
*/
export const easeInBack = (t, b, c, d, s = 1.70158) => c * (t /= d) * t * ((s + 1) * t - s) + b;
export const easeInBack:TEasingFunction = (t, b, c, d, s = 1.70158) => c * (t /= d) * t * ((s + 1) * t - s) + b;

/**
* Backwards easing out
* @memberOf fabric.util.ease
*/
export const easeOutBack = (t, b, c, d, s = 1.70158) => c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
export const easeOutBack:TEasingFunction = (t, b, c, d, s = 1.70158) => c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;

/**
* Backwards easing in and out
* @memberOf fabric.util.ease
*/
export const easeInOutBack = (t, b, c, d, s = 1.70158) => {
export const easeInOutBack:TEasingFunction = (t, b, c, d, s = 1.70158) => {
t /= d / 2;
if (t < 1) {
return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
Expand All @@ -261,7 +266,7 @@ export const easeInOutBack = (t, b, c, d, s = 1.70158) => {
* Bouncing easing out
* @memberOf fabric.util.ease
*/
export const easeOutBounce = (t, b, c, d) => {
export const easeOutBounce:TEasingFunction = (t, b, c, d) => {
if ((t /= d) < (1 / 2.75)) {
return c * (7.5625 * t * t) + b;
}
Expand All @@ -280,13 +285,13 @@ export const easeOutBounce = (t, b, c, d) => {
* Bouncing easing in
* @memberOf fabric.util.ease
*/
export const easeInBounce = (t, b, c, d) => c - easeOutBounce(d - t, 0, c, d) + b;
export const easeInBounce:TEasingFunction = (t, b, c, d) => c - easeOutBounce(d - t, 0, c, d) + b;

/**
* Bouncing easing in and out
* @memberOf fabric.util.ease
*/
export const easeInOutBounce = (t, b, c, d) =>
export const easeInOutBounce:TEasingFunction = (t, b, c, d) =>
t < d / 2 ?
easeInBounce(t * 2, 0, c, d) * 0.5 + b :
easeOutBounce(t * 2 - d, 0, c, d) * 0.5 + c * 0.5 + b;
Expand All @@ -296,19 +301,19 @@ export const easeInOutBounce = (t, b, c, d) =>
* Quadratic easing in
* @memberOf fabric.util.ease
*/
export const easeInQuad = (t, b, c, d) => c * (t /= d) * t + b;
export const easeInQuad:TEasingFunction = (t, b, c, d) => c * (t /= d) * t + b;

/**
* Quadratic easing out
* @memberOf fabric.util.ease
*/
export const easeOutQuad = (t, b, c, d) => -c * (t /= d) * (t - 2) + b;
export const easeOutQuad:TEasingFunction = (t, b, c, d) => -c * (t /= d) * (t - 2) + b;

/**
* Quadratic easing in and out
* @memberOf fabric.util.ease
*/
export const easeInOutQuad = (t, b, c, d) => {
export const easeInOutQuad:TEasingFunction = (t, b, c, d) => {
t /= (d / 2);
if (t < 1) {
return c / 2 * t ** 2 + b;
Expand All @@ -320,4 +325,4 @@ export const easeInOutQuad = (t, b, c, d) => {
* Cubic easing in
* @memberOf fabric.util.ease
*/
export const easeInCubic = (t, b, c, d) => c * (t /= d) * t * t + b;
export const easeInCubic:TEasingFunction = (t, b, c, d) => c * (t /= d) * t * t + b;

0 comments on commit cd54755

Please sign in to comment.