-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScalingSquaresSpinner.js
110 lines (103 loc) · 3.76 KB
/
ScalingSquaresSpinner.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
/** @flow **/
import type { Element } from 'react';
import React, { useEffect } from 'react';
import { Animated, Easing, StyleSheet, View } from 'react-native';
import type { EpicSpinnersProps } from '../core/Typings';
import { EpicSpinnersDefaultProps } from '../core/Typings';
import { useAnimated, useAnimatedViewsNameGenerator } from '../core/CustomHooks';
import { GenerateAnimatedViews } from '../core/GenerateAnimatedViews';
export function ScalingSquaresSpinner(props: EpicSpinnersProps): Element<any> {
const { color, animationDuration, size, style, ...restProps } = props;
const containerSize = size * 3;
const axisDirection = { center: 0, positive: containerSize * 0.15, negative: containerSize * -0.15 };
const [container, firstSquare, secondSquare, thirdSquare, forthSquare] = useAnimated(5);
const VIEWS = useAnimatedViewsNameGenerator('square', 4);
const spinnerStyle = StyleSheet.create({
container: {
height: containerSize,
width: containerSize,
position: 'relative',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center'
},
square: {
height: (containerSize * 0.25) / 1.3,
width: (containerSize * 0.25) / 1.3,
borderWidth: (containerSize * 0.04) / 1.3,
borderColor: color,
position: 'absolute',
marginRight: 'auto',
marginLeft: 'auto'
}
});
const getTransformRotation = (animated) => {
return {
transform: [
{
rotate: animated.interpolate({
inputRange: [0, 1, 1.2, 2.2, 2.4],
outputRange: ['0deg', '90deg', '90deg', '180deg', '180deg']
})
}
]
};
};
const getSquareTransformation = (animated, translateX, translateY) => {
return {
transform: [
{
scale: animated.interpolate({
inputRange: [0, 1, 1.2, 2.2, 2.4],
outputRange: [1, 2, 2, 1, 1]
})
},
{
translateX: animated.interpolate({
inputRange: [0, 1, 1.2, 2.2, 2.4],
outputRange: [axisDirection.center, translateX, translateX, axisDirection.center, axisDirection.center]
})
},
{
translateY: animated.interpolate({
inputRange: [0, 1, 1.2, 2.2, 2.4],
outputRange: [axisDirection.center, translateY, translateY, axisDirection.center, axisDirection.center]
})
}
]
};
};
const animatedStyle = {
container: getTransformRotation(container),
square1: getSquareTransformation(firstSquare, axisDirection.positive, axisDirection.positive),
square2: getSquareTransformation(secondSquare, axisDirection.positive, axisDirection.negative),
square3: getSquareTransformation(thirdSquare, axisDirection.negative, axisDirection.positive),
square4: getSquareTransformation(forthSquare, axisDirection.negative, axisDirection.negative)
};
useEffect(() => {
const parallelAnimation = (animated) => {
return Animated.timing(animated, {
toValue: 2.4,
duration: animationDuration,
easing: Easing.linear
});
};
Animated.loop(
Animated.parallel([
parallelAnimation(container),
parallelAnimation(firstSquare),
parallelAnimation(secondSquare),
parallelAnimation(thirdSquare),
parallelAnimation(forthSquare)
])
).start();
}, [animationDuration, container, firstSquare, forthSquare, secondSquare, thirdSquare]);
return (
<View style={style} {...restProps}>
<Animated.View style={[spinnerStyle.container, animatedStyle.container]}>
<GenerateAnimatedViews animatedViewsArray={VIEWS} animatedStyle={animatedStyle} style={spinnerStyle.square} />
</Animated.View>
</View>
);
}
ScalingSquaresSpinner.defaultProps = EpicSpinnersDefaultProps;