Skip to content

Commit 4498648

Browse files
committed
add preset interpolators
1 parent 2105200 commit 4498648

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Layer, Vector } from 'expression-globals-typescript';
2+
import { Interpolator, interpolators } from './interpolators';
23

34
const thisLayer = new Layer();
45

@@ -22,11 +23,7 @@ interface EKey extends InputKey {
2223

2324
interface AnimateOptions {
2425
inputTime: number;
25-
interpolator?: (
26-
progress: number,
27-
easeOut?: number,
28-
easeIn?: number
29-
) => number;
26+
interpolator?: Interpolator;
3027
}
3128

3229
// The function that's called from After Effects
@@ -449,4 +446,4 @@ function animate(
449446

450447
const version: string = '_npmVersion';
451448

452-
export { animate, version };
449+
export { animate, version, interpolators };

src/interpolators.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export type Interpolator = (
2+
progress: number,
3+
easeOut?: number,
4+
easeIn?: number
5+
) => number;
6+
7+
export const interpolators: Record<string, Interpolator> = {
8+
// no easing, no acceleration
9+
linear: t => t,
10+
// accelerating from zero velocity
11+
easeInQuad: t => t * t,
12+
// decelerating to zero velocity
13+
easeOutQuad: t => t * (2 - t),
14+
// acceleration until halfway, then deceleration
15+
easeInOutQuad: t => (t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t),
16+
// accelerating from zero velocity
17+
easeInCubic: t => t * t * t,
18+
// decelerating to zero velocity
19+
easeOutCubic: t => --t * t * t + 1,
20+
// acceleration until halfway, then deceleration
21+
easeInOutCubic: t =>
22+
t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1,
23+
// accelerating from zero velocity
24+
easeInQuart: t => t * t * t * t,
25+
// decelerating to zero velocity
26+
easeOutQuart: t => 1 - --t * t * t * t,
27+
// acceleration until halfway, then deceleration
28+
easeInOutQuart: t => (t < 0.5 ? 8 * t * t * t * t : 1 - 8 * --t * t * t * t),
29+
// accelerating from zero velocity
30+
easeInQuint: t => t * t * t * t * t,
31+
// decelerating to zero velocity
32+
easeOutQuint: t => 1 + --t * t * t * t * t,
33+
// acceleration until halfway, then deceleration
34+
easeInOutQuint: t =>
35+
t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * --t * t * t * t * t,
36+
easeInElastic: t => (0.04 - 0.04 / t) * Math.sin(25 * t) + 1,
37+
easeOutElastic: t => ((0.04 * t) / --t) * Math.sin(25 * t),
38+
easeInOutElastic: t =>
39+
(t -= 0.5) < 0
40+
? (0.02 + 0.01 / t) * Math.sin(50 * t)
41+
: (0.02 - 0.01 / t) * Math.sin(50 * t) + 1,
42+
};

0 commit comments

Comments
 (0)