-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ComplexAnimationBuilder.ts
276 lines (245 loc) · 9.24 KB
/
ComplexAnimationBuilder.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
'use strict';
import { withTiming, withSpring } from '../../animation';
import type {
AnimationFunction,
BaseBuilderAnimationConfig,
LayoutAnimationAndConfig,
} from './commonTypes';
import type { EasingFunction } from '../../Easing';
import { BaseAnimationBuilder } from './BaseAnimationBuilder';
import type { StyleProps } from '../../commonTypes';
export class ComplexAnimationBuilder extends BaseAnimationBuilder {
easingV?: EasingFunction;
rotateV?: string;
type?: AnimationFunction;
dampingV?: number;
dampingRatioV?: number;
massV?: number;
stiffnessV?: number;
overshootClampingV?: number;
restDisplacementThresholdV?: number;
restSpeedThresholdV?: number;
initialValues?: StyleProps;
static createInstance: <T extends typeof BaseAnimationBuilder>(
this: T
) => InstanceType<T>;
/**
* Lets you change the easing curve of the animation. Can be chained alongside other [layout animation modifiers](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#layout-animation-modifier).
*
* @param easingFunction - An easing function which defines the animation curve.
*/
static easing<T extends typeof ComplexAnimationBuilder>(
this: T,
easingFunction: EasingFunction
) {
const instance = this.createInstance();
return instance.easing(easingFunction);
}
easing(easingFunction: EasingFunction): this {
this.easingV = easingFunction;
return this;
}
/**
* Lets you rotate the element. Can be chained alongside other [layout animation modifiers](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#layout-animation-modifier).
*
* @param degree - The rotation degree.
*/
static rotate<T extends typeof ComplexAnimationBuilder>(
this: T,
degree: string
) {
const instance = this.createInstance();
return instance.rotate(degree);
}
rotate(degree: string): this {
this.rotateV = degree;
return this;
}
/**
* Enables the spring-based animation configuration. Can be chained alongside other [layout animation modifiers](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#layout-animation-modifier).
*
* @param duration - An optional duration of the spring animation (in milliseconds).
*/
static springify<T extends typeof ComplexAnimationBuilder>(
this: T,
duration?: number
): ComplexAnimationBuilder {
const instance = this.createInstance();
return instance.springify(duration);
}
springify(duration?: number): this {
this.durationV = duration;
this.type = withSpring as AnimationFunction;
return this;
}
/**
* Lets you adjust the spring animation damping ratio. Can be chained alongside other [layout animation modifiers](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#layout-animation-modifier).
*
* @param dampingRatio - How damped the spring is.
*/
static dampingRatio<T extends typeof ComplexAnimationBuilder>(
this: T,
dampingRatio: number
) {
const instance = this.createInstance();
return instance.dampingRatio(dampingRatio);
}
dampingRatio(value: number): this {
this.dampingRatioV = value;
return this;
}
/**
* Lets you adjust the spring animation damping. Can be chained alongside other [layout animation modifiers](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#layout-animation-modifier).
*
* @param value - Decides how quickly a spring stops moving. Higher damping means the spring will come to rest faster.
*/
static damping<T extends typeof ComplexAnimationBuilder>(
this: T,
damping: number
) {
const instance = this.createInstance();
return instance.damping(damping);
}
damping(damping: number): this {
this.dampingV = damping;
return this;
}
/**
* Lets you adjust the spring animation mass. Can be chained alongside other [layout animation modifiers](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#layout-animation-modifier).
*
* @param mass - The weight of the spring. Reducing this value makes the animation faster.
*/
static mass<T extends typeof ComplexAnimationBuilder>(this: T, mass: number) {
const instance = this.createInstance();
return instance.mass(mass);
}
mass(mass: number): this {
this.massV = mass;
return this;
}
/**
* Lets you adjust the stiffness of the spring animation. Can be chained alongside other [layout animation modifiers](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#layout-animation-modifier).
*
* @param stiffness - How bouncy the spring is.
*/
static stiffness<T extends typeof ComplexAnimationBuilder>(
this: T,
stiffness: number
) {
const instance = this.createInstance();
return instance.stiffness(stiffness);
}
stiffness(stiffness: number): this {
this.stiffnessV = stiffness;
return this;
}
/**
* Lets you adjust overshoot clamping of the spring. Can be chained alongside other [layout animation modifiers](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#layout-animation-modifier).
*
* @param overshootClamping - Whether a spring can bounce over the final position.
*/
static overshootClamping<T extends typeof ComplexAnimationBuilder>(
this: T,
overshootClamping: number
) {
const instance = this.createInstance();
return instance.overshootClamping(overshootClamping);
}
overshootClamping(overshootClamping: number): this {
this.overshootClampingV = overshootClamping;
return this;
}
/**
* Lets you adjust the rest displacement threshold of the spring animation. Can be chained alongside other [layout animation modifiers](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#layout-animation-modifier).
*
* @param restDisplacementThreshold - The displacement below which the spring will snap to the designated position without further oscillations.
*/
static restDisplacementThreshold<T extends typeof ComplexAnimationBuilder>(
this: T,
restDisplacementThreshold: number
) {
const instance = this.createInstance();
return instance.restDisplacementThreshold(restDisplacementThreshold);
}
restDisplacementThreshold(restDisplacementThreshold: number) {
this.restDisplacementThresholdV = restDisplacementThreshold;
return this;
}
/**
* Lets you adjust the rest speed threshold of the spring animation. Can be chained alongside other [layout animation modifiers](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#layout-animation-modifier).
*
* @param restSpeedThreshold - The speed in pixels per second from which the spring will snap to the designated position without further oscillations.
*/
static restSpeedThreshold<T extends typeof ComplexAnimationBuilder>(
this: T,
restSpeedThreshold: number
) {
const instance = this.createInstance();
return instance.restSpeedThreshold(restSpeedThreshold);
}
restSpeedThreshold(restSpeedThreshold: number): this {
this.restSpeedThresholdV = restSpeedThreshold;
return this;
}
/**
* Lets you override the initial config of the animation
*
* @param values - An object containing the styles to override.
*/
static withInitialValues<T extends typeof ComplexAnimationBuilder>(
this: T,
values: StyleProps
) {
const instance = this.createInstance();
return instance.withInitialValues(values);
}
withInitialValues(values: StyleProps): this {
this.initialValues = values;
return this;
}
getAnimationAndConfig(): LayoutAnimationAndConfig {
const duration = this.durationV;
const easing = this.easingV;
const rotate = this.rotateV;
const type = this.type ? this.type : (withTiming as AnimationFunction);
const damping = this.dampingV;
const dampingRatio = this.dampingRatioV;
const mass = this.massV;
const stiffness = this.stiffnessV;
const overshootClamping = this.overshootClampingV;
const restDisplacementThreshold = this.restDisplacementThresholdV;
const restSpeedThreshold = this.restSpeedThresholdV;
const animation = type;
const config: BaseBuilderAnimationConfig = {};
function maybeSetConfigValue<Key extends keyof BaseBuilderAnimationConfig>(
value: BaseBuilderAnimationConfig[Key],
variableName: Key
) {
if (value) {
config[variableName] = value;
}
}
if (type === withTiming) {
maybeSetConfigValue(easing, 'easing');
}
(
[
{ variableName: 'damping', value: damping },
{ variableName: 'dampingRatio', value: dampingRatio },
{ variableName: 'mass', value: mass },
{ variableName: 'stiffness', value: stiffness },
{ variableName: 'overshootClamping', value: overshootClamping },
{
variableName: 'restDisplacementThreshold',
value: restDisplacementThreshold,
},
{ variableName: 'restSpeedThreshold', value: restSpeedThreshold },
{ variableName: 'duration', value: duration },
{ variableName: 'rotate', value: rotate },
] as const
).forEach(({ value, variableName }) =>
maybeSetConfigValue(value, variableName)
);
return [animation, config];
}
}