forked from heineiuo/react-native-animated-linear-gradient
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
136 lines (118 loc) · 3.3 KB
/
index.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
import React, {Component, PropTypes} from 'react';
import {StyleSheet, StatusBar, Dimensions, View, Animated, Easing} from 'react-native';
import NativeLinearGradient from 'react-native-linear-gradient';
import rgb2hex from 'rgb2hex';
// const {height, width} = Dimensions.get('window');
class LinearGradient extends Component {
render () {
const {color0, color1, children} = this.props;
return (
<NativeLinearGradient
// colors={this.props.colors.map((c) => rgb2hex(c).hex)}
colors={[color0, color1].map((c) => rgb2hex(c).hex)}
start={{x: 0.0, y: 0.4}}
end={{x: 1.0, y: 0.6}}
style={[styles.linearGradient]}>
{children}
</NativeLinearGradient>
)
}
}
Animated.LinearGradient = Animated.createAnimatedComponent(LinearGradient)
// Animated.NativeLinearGradient = Animated.createAnimatedComponent(NativeLinearGradient)
export const presetColors = {
instagram: [
'rgb(106, 57, 171)',
'rgb(151, 52, 160)',
'rgb(197, 57, 92)',
'rgb(231, 166, 73)',
'rgb(181, 70, 92)'
],
firefox: [
'rgb(236, 190, 55)',
'rgb(215, 110, 51)',
'rgb(181, 63, 49)',
'rgb(192, 71, 45)',
],
sunrise: [
'rgb(92, 160, 186)',
'rgb(106, 166, 186)',
'rgb(142, 191, 186)',
'rgb(172, 211, 186)',
'rgb(239, 235, 186)',
'rgb(212, 222, 206)',
'rgb(187, 216, 200)',
'rgb(152, 197, 190)',
'rgb(100, 173, 186)',
]
};
class AnimatedGradient extends Component {
static defaultProps = {
customColors: presetColors.instagram,
speed: 4000
}
state = {
color0: new Animated.Value(0),
color1: new Animated.Value(0),
}
componentDidMount = () => {
this.startAnimation();
}
startAnimation = () => {
const {color0, color1} = this.state;
const {customColors, speed} = this.props;
[color0, color1].forEach(color => color.setValue(0));
Animated.parallel(
[color0, color1].map(animatedColor => {
return Animated.timing(animatedColor, {
toValue: customColors.length,
duration: customColors.length * speed,
easing: Easing.linear
})
})
)
.start(this.startAnimation);
};
render () {
const {color0, color1} = this.state;
const {customColors, children} = this.props;
const preferColors = [];
// while (preferColors.length < customColors.length) {
while (preferColors.length < 2) {
preferColors.push(
customColors
.slice(preferColors.length)
.concat(customColors.slice(0, preferColors.length+1))
)
}
const interpolatedColors = [color0, color1]
.map((animatedColor, index) => {
return animatedColor.interpolate({
inputRange: Array.from({length: customColors.length+1}, (v, k) => k),
outputRange: preferColors[index]
})
});
return (
<Animated.LinearGradient
style={[styles.linearGradient]}
// colors={interpolatedColors}
color0={interpolatedColors[0]}
color1={interpolatedColors[1]}>
{children}
</Animated.LinearGradient>
)
}
}
const styles = StyleSheet.create({
linearGradient: {
position: 'absolute',
flex: 1,
flexDirection: 'column',
alignItems: 'stretch',
left: 0,
right: 0,
top: 0,
bottom: 0
}
});
export default AnimatedGradient