-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathCirclesLoader.js
More file actions
85 lines (78 loc) · 2.16 KB
/
CirclesLoader.js
File metadata and controls
85 lines (78 loc) · 2.16 KB
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
import React from 'react';
import PropTypes from 'prop-types';
import { Animated } from 'react-native';
import { Surface } from '@react-native-community/art';
import AnimatedCircle from '../animated/AnimatedCircle';
import { color } from '../const';
export default class CirclesLoader extends React.PureComponent {
static propTypes = {
color: PropTypes.string,
dotRadius: PropTypes.number,
size: PropTypes.number,
numberDots: PropTypes.number,
};
static defaultProps = {
color,
dotRadius: 8,
size: 40,
numberDots: 8,
};
state = {
opacities: Array.apply(null, Array(this.props.numberDots)).map(function(){return new Animated.Value(1)}),
};
eachDegree = 360 / this.state.opacities.length;
timers = [];
componentDidMount() {
this.state.opacities.forEach((item, i) => {
const id = setTimeout(() => {
this._animation(i);
}, i * 150);
this.timers.push(id);
});
}
componentWillUnmount() {
this.unmounted = true;
this.timers.forEach((id) => {
clearTimeout(id);
});
}
_animation = (i) => {
Animated.sequence([
Animated.timing(this.state.opacities[i], {
toValue: 0.1,
duration: 600,
useNativeDriver: false,
}),
Animated.timing(this.state.opacities[i], {
toValue: 1,
duration: 600,
useNativeDriver: false,
}),
]).start(() => {
!this.unmounted && this._animation(i);
});
};
render() {
const { size, dotRadius, color } = this.props;
const { opacities } = this.state;
return (
<Surface width={size + dotRadius} height={size + dotRadius}>
{opacities.map((item, i) => {
let radian = (i * this.eachDegree * Math.PI) / 180;
let x = Math.round((size / 2) * Math.cos(radian)) + size / 2 + dotRadius / 2;
let y = Math.round((size / 2) * Math.sin(radian)) + size / 2 + dotRadius / 2;
return (
<AnimatedCircle
key={i}
radius={dotRadius}
fill={color}
x={x}
y={y}
opacity={opacities[i]}
/>
);
})}
</Surface>
);
}
}