-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
animations.js
325 lines (295 loc) · 11.6 KB
/
animations.js
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import { Platform } from 'react-native';
const IS_ANDROID = Platform.OS === 'android';
// Get scroll interpolator's input range from an array of slide indexes
// Indexes are relative to the current active slide (index 0)
// For example, using [3, 2, 1, 0, -1] will return:
// [
// (index - 3) * sizeRef, // active + 3
// (index - 2) * sizeRef, // active + 2
// (index - 1) * sizeRef, // active + 1
// index * sizeRef, // active
// (index + 1) * sizeRef // active - 1
// ]
export function getInputRangeFromIndexes (range, index, carouselProps) {
const sizeRef = carouselProps.vertical ? carouselProps.itemHeight : carouselProps.itemWidth;
let inputRange = [];
for (let i = 0; i < range.length; i++) {
inputRange.push((index - range[i]) * sizeRef);
}
return inputRange;
}
// Default behavior
// Scale and/or opacity effect
// Based on props 'inactiveSlideOpacity' and 'inactiveSlideScale'
export function defaultScrollInterpolator (index, carouselProps) {
const range = [1, 0, -1];
const inputRange = getInputRangeFromIndexes(range, index, carouselProps);
const outputRange = [0, 1, 0];
return { inputRange, outputRange };
}
export function defaultAnimatedStyles (index, animatedValue, carouselProps) {
let animatedOpacity = {};
let animatedScale = {};
if (carouselProps.inactiveSlideOpacity < 1) {
animatedOpacity = {
opacity: animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [carouselProps.inactiveSlideOpacity, 1]
})
};
}
if (carouselProps.inactiveSlideScale < 1) {
animatedScale = {
transform: [{
scale: animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [carouselProps.inactiveSlideScale, 1]
})
}]
};
}
return {
...animatedOpacity,
...animatedScale
};
}
// Shift animation
// Same as the default one, but the active slide is also shifted up or down
// Based on prop 'inactiveSlideShift'
export function shiftAnimatedStyles (index, animatedValue, carouselProps) {
let animatedOpacity = {};
let animatedScale = {};
let animatedTranslate = {};
if (carouselProps.inactiveSlideOpacity < 1) {
animatedOpacity = {
opacity: animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [carouselProps.inactiveSlideOpacity, 1]
})
};
}
if (carouselProps.inactiveSlideScale < 1) {
animatedScale = {
scale: animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [carouselProps.inactiveSlideScale, 1]
})
};
}
if (carouselProps.inactiveSlideShift !== 0) {
const translateProp = carouselProps.vertical ? 'translateX' : 'translateY';
animatedTranslate = {
[translateProp]: animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [carouselProps.inactiveSlideShift, 0]
})
};
}
return {
...animatedOpacity,
transform: [
{ ...animatedScale },
{ ...animatedTranslate }
]
};
}
// Stack animation
// Imitate a deck/stack of cards (see #195)
// WARNING: The effect had to be visually inverted on Android because this OS doesn't honor the `zIndex`property
// This means that the item with the higher zIndex (and therefore the tap receiver) remains the one AFTER the currently active item
// The `elevation` property compensates for that only visually, which is not good enough
export function stackScrollInterpolator (index, carouselProps) {
const range = IS_ANDROID ?
[1, 0, -1, -2, -3] :
[3, 2, 1, 0, -1];
const inputRange = getInputRangeFromIndexes(range, index, carouselProps);
const outputRange = range;
return { inputRange, outputRange };
}
export function stackAnimatedStyles (index, animatedValue, carouselProps, cardOffset) {
const sizeRef = carouselProps.vertical ? carouselProps.itemHeight : carouselProps.itemWidth;
const translateProp = carouselProps.vertical ? 'translateY' : 'translateX';
const card1Scale = 0.9;
const card2Scale = 0.8;
cardOffset = !cardOffset && cardOffset !== 0 ? 18 : cardOffset;
const getTranslateFromScale = (cardIndex, scale) => {
const centerFactor = 1 / scale * cardIndex;
const centeredPosition = -Math.round(sizeRef * centerFactor);
const edgeAlignment = Math.round((sizeRef - (sizeRef * scale)) / 2);
const offset = Math.round(cardOffset * Math.abs(cardIndex) / scale);
return IS_ANDROID ?
centeredPosition - edgeAlignment - offset :
centeredPosition + edgeAlignment + offset;
};
const opacityOutputRange = carouselProps.inactiveSlideOpacity === 1 ? [1, 1, 1, 0] : [1, 0.75, 0.5, 0];
return IS_ANDROID ? {
// elevation: carouselProps.data.length - index, // fix zIndex bug visually, but not from a logic point of view
opacity: animatedValue.interpolate({
inputRange: [-3, -2, -1, 0],
outputRange: opacityOutputRange.reverse(),
extrapolate: 'clamp'
}),
transform: [{
scale: animatedValue.interpolate({
inputRange: [-2, -1, 0, 1],
outputRange: [card2Scale, card1Scale, 1, card1Scale],
extrapolate: 'clamp'
})
}, {
[translateProp]: animatedValue.interpolate({
inputRange: [-3, -2, -1, 0, 1],
outputRange: [
getTranslateFromScale(-3, card2Scale),
getTranslateFromScale(-2, card2Scale),
getTranslateFromScale(-1, card1Scale),
0,
sizeRef * 0.5
],
extrapolate: 'clamp'
})
}]
} : {
zIndex: carouselProps.data.length - index,
opacity: animatedValue.interpolate({
inputRange: [0, 1, 2, 3],
outputRange: opacityOutputRange,
extrapolate: 'clamp'
}),
transform: [{
scale: animatedValue.interpolate({
inputRange: [-1, 0, 1, 2],
outputRange: [card1Scale, 1, card1Scale, card2Scale],
extrapolate: 'clamp'
})
}, {
[translateProp]: animatedValue.interpolate({
inputRange: [-1, 0, 1, 2, 3],
outputRange: [
-sizeRef * 0.5,
0,
getTranslateFromScale(1, card1Scale),
getTranslateFromScale(2, card2Scale),
getTranslateFromScale(3, card2Scale)
],
extrapolate: 'clamp'
})
}]
};
}
// Tinder animation
// Imitate the popular Tinder layout
// WARNING: The effect had to be visually inverted on Android because this OS doesn't honor the `zIndex`property
// This means that the item with the higher zIndex (and therefore the tap receiver) remains the one AFTER the currently active item
// The `elevation` property compensates for that only visually, which is not good enough
export function tinderScrollInterpolator (index, carouselProps) {
const range = IS_ANDROID ?
[1, 0, -1, -2, -3] :
[3, 2, 1, 0, -1];
const inputRange = getInputRangeFromIndexes(range, index, carouselProps);
const outputRange = range;
return { inputRange, outputRange };
}
export function tinderAnimatedStyles (index, animatedValue, carouselProps, cardOffset) {
const sizeRef = carouselProps.vertical ? carouselProps.itemHeight : carouselProps.itemWidth;
const mainTranslateProp = carouselProps.vertical ? 'translateY' : 'translateX';
const secondaryTranslateProp = carouselProps.vertical ? 'translateX' : 'translateY';
const card1Scale = 0.96;
const card2Scale = 0.92;
const card3Scale = 0.88;
const peekingCardsOpacity = IS_ANDROID ? 0.92 : 1;
cardOffset = !cardOffset && cardOffset !== 0 ? 9 : cardOffset;
const getMainTranslateFromScale = (cardIndex, scale) => {
const centerFactor = 1 / scale * cardIndex;
return -Math.round(sizeRef * centerFactor);
};
const getSecondaryTranslateFromScale = (cardIndex, scale) => {
return Math.round(cardOffset * Math.abs(cardIndex) / scale);
};
return IS_ANDROID ? {
// elevation: carouselProps.data.length - index, // fix zIndex bug visually, but not from a logic point of view
opacity: animatedValue.interpolate({
inputRange: [-3, -2, -1, 0, 1],
outputRange: [0, peekingCardsOpacity, peekingCardsOpacity, 1, 0],
extrapolate: 'clamp'
}),
transform: [{
scale: animatedValue.interpolate({
inputRange: [-3, -2, -1, 0],
outputRange: [card3Scale, card2Scale, card1Scale, 1],
extrapolate: 'clamp'
})
}, {
rotate: animatedValue.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '22deg'],
extrapolate: 'clamp'
})
}, {
[mainTranslateProp]: animatedValue.interpolate({
inputRange: [-3, -2, -1, 0, 1],
outputRange: [
getMainTranslateFromScale(-3, card3Scale),
getMainTranslateFromScale(-2, card2Scale),
getMainTranslateFromScale(-1, card1Scale),
0,
sizeRef * 1.1
],
extrapolate: 'clamp'
})
}, {
[secondaryTranslateProp]: animatedValue.interpolate({
inputRange: [-3, -2, -1, 0],
outputRange: [
getSecondaryTranslateFromScale(-3, card3Scale),
getSecondaryTranslateFromScale(-2, card2Scale),
getSecondaryTranslateFromScale(-1, card1Scale),
0
],
extrapolate: 'clamp'
})
}]
} : {
zIndex: carouselProps.data.length - index,
opacity: animatedValue.interpolate({
inputRange: [-1, 0, 1, 2, 3],
outputRange: [0, 1, peekingCardsOpacity, peekingCardsOpacity, 0],
extrapolate: 'clamp'
}),
transform: [{
scale: animatedValue.interpolate({
inputRange: [0, 1, 2, 3],
outputRange: [1, card1Scale, card2Scale, card3Scale],
extrapolate: 'clamp'
})
}, {
rotate: animatedValue.interpolate({
inputRange: [-1, 0],
outputRange: ['-22deg', '0deg'],
extrapolate: 'clamp'
})
}, {
[mainTranslateProp]: animatedValue.interpolate({
inputRange: [-1, 0, 1, 2, 3],
outputRange: [
-sizeRef * 1.1,
0,
getMainTranslateFromScale(1, card1Scale),
getMainTranslateFromScale(2, card2Scale),
getMainTranslateFromScale(3, card3Scale)
],
extrapolate: 'clamp'
})
}, {
[secondaryTranslateProp]: animatedValue.interpolate({
inputRange: [0, 1, 2, 3],
outputRange: [
0,
getSecondaryTranslateFromScale(1, card1Scale),
getSecondaryTranslateFromScale(2, card2Scale),
getSecondaryTranslateFromScale(3, card3Scale)
],
extrapolate: 'clamp'
})
}]
};
}