forked from microsoft/microcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curves.ts
80 lines (78 loc) · 2.51 KB
/
curves.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
namespace microcode.curves.easing {
// Curve formulas from https://easings.net/
// Only "ease in"-style curves should appear here. "ease out" and "ease in-out" are derived programmatically from these.
export function sine(t: number): number {
return 1 - Math.cos((t * Math.PI) / 2)
}
export function sq2(t: number): number {
return t * t
}
export function sq3(t: number): number {
return t * t * t
}
export function sq4(t: number): number {
return t * t * t * t
}
export function sq5(t: number): number {
return t * t * t * t * t
}
export function expo(t: number): number {
return t === 0 ? 0 : Math.pow(2, 10 * t - 10)
}
export function circ(t: number): number {
return 1.0 - Math.sqrt(1.0 - Math.pow(t, 2))
}
export function back(t: number): number {
const c1 = 1.70158
const c3 = c1 + 1
return c3 * t * t * t - c1 * t * t
}
export function elastic(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)
}
}
namespace microcode.curves.util {
export function lerp(a: number, b: number, t: number): number {
return a + (b - a) * t
}
export function flip(t: number): number {
return 1 - t
}
}
// Predefined curves for use with animation EaseFrame.
// Choose an ease type and curve pair, like: `easing.easeIn(easing.curves.sine)`
namespace microcode.curves {
export function linear(): (a: number, b: number, t: number) => number {
return util.lerp
}
export function snap(
pct: number
): (a: number, b: number, t: number) => number {
return (a, b, t) => (t < pct ? a : b)
}
export function easeIn(
easeInFn: (t: number) => number
): (a: number, b: number, t: number) => number {
return (a, b, t) => util.lerp(a, b, easeInFn(t))
}
export function easeOut(
easeInFn: (t: number) => number
): (a: number, b: number, t: number) => number {
return (a, b, t) => util.lerp(a, b, util.flip(easeInFn(util.flip(t))))
}
export function easeInOut(
easeInFn: (t: number) => number
): (a: number, b: number, t: number) => number {
return (a, b, t) =>
util.lerp(
a,
b,
util.lerp(easeInFn(t), util.flip(easeInFn(util.flip(t))), t)
)
}
}