forked from oblador/react-native-progress
-
Notifications
You must be signed in to change notification settings - Fork 5
/
CircleSnail.js
186 lines (169 loc) · 4.56 KB
/
CircleSnail.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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Animated, Easing } from 'react-native';
import { Svg } from 'react-native-svg';
import Arc from './Shapes/Arc';
const AnimatedArc = Animated.createAnimatedComponent(Arc);
const MIN_ARC_ANGLE = 0.1;
const MAX_ARC_ANGLE = 1.5 * Math.PI;
export default class CircleSnail extends Component {
static propTypes = {
animating: PropTypes.bool,
color: PropTypes.oneOfType([
PropTypes.string,
PropTypes.arrayOf(PropTypes.string),
]),
children: PropTypes.node,
direction: PropTypes.oneOf(['clockwise', 'counter-clockwise']),
duration: PropTypes.number,
hidesWhenStopped: PropTypes.bool,
size: PropTypes.number,
spinDuration: PropTypes.number,
style: PropTypes.any,
thickness: PropTypes.number,
strokeCap: PropTypes.string,
useNativeDriver: PropTypes.bool,
};
static defaultProps = {
animating: true,
color: 'rgba(0, 122, 255, 1)',
direction: 'counter-clockwise',
hidesWhenStopped: false,
size: 40,
thickness: 3,
strokeCap: 'round',
useNativeDriver: false,
};
constructor(props) {
super(props);
this.state = {
startAngle: new Animated.Value(-MIN_ARC_ANGLE),
endAngle: new Animated.Value(0),
rotation: new Animated.Value(0),
colorIndex: 0,
};
}
componentDidMount() {
if (this.props.animating) {
this.animate();
this.spin();
}
}
componentDidUpdate(prevProps) {
if (prevProps.animating !== this.props.animating) {
if (this.props.animating) {
this.animate();
this.spin();
} else {
this.stopAnimations();
}
}
}
animate(iteration = 1) {
Animated.sequence([
Animated.timing(this.state.startAngle, {
toValue: -MAX_ARC_ANGLE * iteration - MIN_ARC_ANGLE,
duration: this.props.duration || 1000,
isInteraction: false,
easing: Easing.inOut(Easing.quad),
useNativeDriver: this.props.useNativeDriver,
}),
Animated.timing(this.state.endAngle, {
toValue: -MAX_ARC_ANGLE * iteration,
duration: this.props.duration || 1000,
isInteraction: false,
easing: Easing.inOut(Easing.quad),
useNativeDriver: this.props.useNativeDriver,
}),
]).start(endState => {
if (endState.finished) {
if (Array.isArray(this.props.color)) {
this.setState({
colorIndex: iteration % this.props.color.length,
});
}
this.animate(iteration + 1);
}
});
}
spin() {
Animated.timing(this.state.rotation, {
toValue: 1,
duration: this.props.spinDuration || 5000,
easing: Easing.linear,
isInteraction: false,
useNativeDriver: this.props.useNativeDriver,
}).start(endState => {
if (endState.finished) {
this.state.rotation.setValue(0);
this.spin();
}
});
}
stopAnimations() {
this.state.startAngle.stopAnimation();
this.state.endAngle.stopAnimation();
this.state.rotation.stopAnimation();
}
render() {
const {
animating,
children,
color,
direction,
hidesWhenStopped,
size,
style,
thickness,
strokeCap,
...restProps
} = this.props;
if (!animating && hidesWhenStopped) {
return null;
}
const radius = size / 2 - thickness;
const offset = {
top: thickness,
left: thickness,
};
const directionFactor = direction === 'counter-clockwise' ? -1 : 1;
return (
<Animated.View
{...restProps}
style={[
style,
{
backgroundColor: 'transparent',
overflow: 'hidden',
transform: [
{
rotate: this.state.rotation.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', `${directionFactor * 360}deg`],
}),
},
],
},
]}
>
<Svg width={size} height={size} fill="none">
<AnimatedArc
direction={
direction === 'counter-clockwise'
? 'clockwise'
: 'counter-clockwise'
}
radius={radius}
stroke={Array.isArray(color) ? color[this.state.colorIndex] : color}
offset={offset}
startAngle={this.state.startAngle}
endAngle={this.state.endAngle}
strokeCap={strokeCap}
strokeWidth={thickness}
/>
</Svg>
{children}
</Animated.View>
);
}
}