easing.js
4.01 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { warn } from './util';
const ACCURACY = 1e-4;
const cubicBezierFactor = (c1, c2) => [0, 3 * c1, 3 * c2 - 6 * c1, 3 * c1 - 3 * c2 + 1];
const multyTime = (params, t) => params.map((param, i) => param * t ** i).reduce((pre, curr) => pre + curr);
const cubicBezier = (c1, c2) => t => {
const params = cubicBezierFactor(c1, c2);
return multyTime(params, t);
};
const derivativeCubicBezier = (c1, c2) => t => {
const params = cubicBezierFactor(c1, c2);
const newParams = [...params.map((param, i) => param * i).slice(1), 0];
return multyTime(newParams, t);
};
// calculate cubic-bezier using Newton's method
export const configBezier = (...args) => {
let [x1, y1, x2, y2] = args;
if (args.length === 1) {
switch (args[0]) {
case 'linear':
[x1, y1, x2, y2] = [0.0, 0.0, 1.0, 1.0];
break;
case 'ease':
[x1, y1, x2, y2] = [0.25, 0.1, 0.25, 1.0];
break;
case 'ease-in':
[x1, y1, x2, y2] = [0.42, 0.0, 1.0, 1.0];
break;
case 'ease-out':
[x1, y1, x2, y2] = [0.42, 0.0, 0.58, 1.0];
break;
case 'ease-in-out':
[x1, y1, x2, y2] = [0.0, 0.0, 0.58, 1.0];
break;
default: {
const easing = args[0].split('(');
if (easing[0] === 'cubic-bezier' && easing[1].split(')')[0].split(',').length === 4) {
[x1, y1, x2, y2] = easing[1]
.split(')')[0]
.split(',')
.map(x => parseFloat(x));
} else {
warn(
false,
'[configBezier]: arguments should be one of ' +
"oneOf 'linear', 'ease', 'ease-in', 'ease-out', " +
"'ease-in-out','cubic-bezier(x1,y1,x2,y2)', instead received %s",
args,
);
}
}
}
}
warn(
[x1, x2, y1, y2].every(num => typeof num === 'number' && num >= 0 && num <= 1),
'[configBezier]: arguments should be x1, y1, x2, y2 of [0, 1] instead received %s',
args,
);
const curveX = cubicBezier(x1, x2);
const curveY = cubicBezier(y1, y2);
const derCurveX = derivativeCubicBezier(x1, x2);
const rangeValue = value => {
if (value > 1) {
return 1;
}
if (value < 0) {
return 0;
}
return value;
};
const bezier = _t => {
const t = _t > 1 ? 1 : _t;
let x = t;
for (let i = 0; i < 8; ++i) {
const evalT = curveX(x) - t;
const derVal = derCurveX(x);
if (Math.abs(evalT - t) < ACCURACY || derVal < ACCURACY) {
return curveY(x);
}
x = rangeValue(x - evalT / derVal);
}
return curveY(x);
};
bezier.isStepper = false;
return bezier;
};
export const configSpring = (config = {}) => {
const { stiff = 100, damping = 8, dt = 17 } = config;
const stepper = (currX, destX, currV) => {
const FSpring = -(currX - destX) * stiff;
const FDamping = currV * damping;
const newV = currV + ((FSpring - FDamping) * dt) / 1000;
const newX = (currV * dt) / 1000 + currX;
if (Math.abs(newX - destX) < ACCURACY && Math.abs(newV) < ACCURACY) {
return [destX, 0];
}
return [newX, newV];
};
stepper.isStepper = true;
stepper.dt = dt;
return stepper;
};
export const configEasing = (...args) => {
const [easing] = args;
if (typeof easing === 'string') {
switch (easing) {
case 'ease':
case 'ease-in-out':
case 'ease-out':
case 'ease-in':
case 'linear':
return configBezier(easing);
case 'spring':
return configSpring();
default:
if (easing.split('(')[0] === 'cubic-bezier') {
return configBezier(easing);
}
warn(
false,
"[configEasing]: first argument should be one of 'ease', 'ease-in', " +
"'ease-out', 'ease-in-out','cubic-bezier(x1,y1,x2,y2)', 'linear' and 'spring', instead received %s",
args,
);
}
}
if (typeof easing === 'function') {
return easing;
}
warn(false, '[configEasing]: first argument type should be function or string, instead received %s', args);
return null;
};