-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathanimations.js
180 lines (167 loc) · 5.62 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
import { getInputRangeFromIndexes } from 'react-native-snap-carousel';
// Photo album effect
function scrollInterpolator1 (index, carouselProps) {
const range = [3, 2, 1, 0, -1];
const inputRange = getInputRangeFromIndexes(range, index, carouselProps);
const outputRange = range;
return { inputRange, outputRange };
}
function animatedStyles1 (index, animatedValue, carouselProps) {
const sizeRef = carouselProps.vertical ? carouselProps.itemHeight : carouselProps.itemWidth;
const translateProp = carouselProps.vertical ? 'translateY' : 'translateX';
return {
zIndex: carouselProps.data.length - index,
opacity: animatedValue.interpolate({
inputRange: [2, 3],
outputRange: [1, 0],
extrapolate: 'clamp'
}),
transform: [{
rotate: animatedValue.interpolate({
inputRange: [-1, 0, 1, 2, 3],
outputRange: ['-25deg', '0deg', '-3deg', '1.8deg', '0deg'],
extrapolate: 'clamp'
})
}, {
[translateProp]: animatedValue.interpolate({
inputRange: [-1, 0, 1, 2, 3],
outputRange: [
-sizeRef * 0.5,
0,
-sizeRef, // centered
-sizeRef * 2, // centered
-sizeRef * 3 // centered
],
extrapolate: 'clamp'
})
}]
};
}
// Perspective effect
function scrollInterpolator2 (index, carouselProps) {
const range = [2, 1, 0, -1];
const inputRange = getInputRangeFromIndexes(range, index, carouselProps);
const outputRange = range;
return { inputRange, outputRange };
}
function animatedStyles2 (index, animatedValue, carouselProps) {
const sizeRef = carouselProps.vertical ? carouselProps.itemHeight : carouselProps.itemWidth;
const translateProp = carouselProps.vertical ? 'translateY' : 'translateX';
return {
zIndex: carouselProps.data.length - index,
opacity: animatedValue.interpolate({
inputRange: [-1, 0, 1, 2],
outputRange: [0.75, 1, 0.6, 0.4]
}),
transform: [{
rotate: animatedValue.interpolate({
inputRange: [-1, 0, 1, 2],
outputRange: ['0deg', '0deg', '5deg', '8deg'],
extrapolate: 'clamp'
})
}, {
scale: animatedValue.interpolate({
inputRange: [-1, 0, 1, 2],
outputRange: [0.96, 1, 0.85, 0.7]
})
}, {
[translateProp]: animatedValue.interpolate({
inputRange: [-1, 0, 1, 2],
outputRange: [
0,
0,
-sizeRef + 30,
-sizeRef * 2 + 45
],
extrapolate: 'clamp'
})
}]
};
}
// Left/right translate effect
function scrollInterpolator3 (index, carouselProps) {
const range = [2, 1, 0, -1];
const inputRange = getInputRangeFromIndexes(range, index, carouselProps);
const outputRange = range;
return { inputRange, outputRange };
}
function animatedStyles3 (index, animatedValue, carouselProps) {
const sizeRef = carouselProps.vertical ? carouselProps.itemHeight : carouselProps.itemWidth;
const translateProp = carouselProps.vertical ? 'translateY' : 'translateX';
return {
zIndex: carouselProps.data.length - index,
opacity: animatedValue.interpolate({
inputRange: [-1, 0, 1, 2],
outputRange: [1, 1, 0.75, 0.5],
extrapolate: 'clamp'
}),
transform: [{
[translateProp]: animatedValue.interpolate({
inputRange: [-1, 0, 1, 2],
outputRange: [
0,
0,
-sizeRef * 2,
-sizeRef
],
extrapolate: 'clamp'
})
}]
};
}
// From https://codeburst.io/horizontal-scroll-animations-in-react-native-18dac6e9c720
function scrollInterpolator4 (index, carouselProps) {
const range = [1, 0, -1];
const inputRange = getInputRangeFromIndexes(range, index, carouselProps);
const outputRange = range;
return { inputRange, outputRange };
}
function animatedStyles4 (index, animatedValue, carouselProps) {
return {
zIndex: carouselProps.data.length - index,
opacity: animatedValue.interpolate({
inputRange: [-1, 0, 1],
outputRange: [0.75, 1, 0.75],
extrapolate: 'clamp'
}),
transform: [
{
perspective: 1000
},
{
scale: animatedValue.interpolate({
inputRange: [-1, 0, 1],
outputRange: [0.65, 1, 0.65],
extrapolate: 'clamp'
})
},
{
rotateX: animatedValue.interpolate({
inputRange: [-1, 0, 1],
outputRange: ['30deg', '0deg', '30deg'],
extrapolate: 'clamp'
})
},
{
rotateY: animatedValue.interpolate({
inputRange: [-1, 0, 1],
outputRange: ['-30deg', '0deg', '30deg'],
extrapolate: 'clamp'
})
}
]
};
}
// Exports
export const scrollInterpolators = {
scrollInterpolator1,
scrollInterpolator2,
scrollInterpolator3,
scrollInterpolator4
};
export const animatedStyles = {
animatedStyles1,
animatedStyles2,
animatedStyles3,
animatedStyles4
};