-
Notifications
You must be signed in to change notification settings - Fork 2
/
WaveView.js
123 lines (123 loc) · 3.2 KB
/
WaveView.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
import React from 'react';
import { View, Animated } from 'react-native';
import PropTypes from 'prop-types';
export default class WaveView extends React.Component {
waveFactor =
this.props.wavePosition === 'top' // top wave
? 3
: this.props.wavePosition === 'bottom' // bottom wave
? -2
: 1; // both wave
speed =
typeof waveSpeed === 'number'
? this.props.waveSpeed
: this.props.waveSpeed === 'fast'
? 500
: 1000;
_renderWaves(_val, index) {
const { height, width, waveColor, waveAmplitude, noOfWaves } = this.props;
const sineIndex = Math.sin(index);
const transDistance = new Animated.Value(sineIndex * waveAmplitude);
const anim = () =>
Animated.sequence([
Animated.timing(transDistance, {
toValue: waveAmplitude,
duration: this.speed,
useNativeDriver: true
}),
Animated.timing(transDistance, {
toValue: -waveAmplitude,
duration: this.speed,
useNativeDriver: true
})
]).start(() => anim());
Animated.timing(transDistance, {
toValue: -waveAmplitude,
duration: ((sineIndex * waveAmplitude + waveAmplitude) * this.speed) / 10,
useNativeDriver: true
}).start(anim);
return (
<Animated.View
key={index}
style={{
height: height,
width: width / noOfWaves,
top: this.waveFactor * waveAmplitude,
borderRadius: 50,
backgroundColor: waveColor,
transform: [{ translateY: transDistance }]
}}
/>
);
}
_renderWaves = this._renderWaves.bind(this);
render() {
const {
height,
width,
waveColor,
waveAmplitude,
noOfWaves,
wavePosition,
style
} = this.props;
const wave = new Array(noOfWaves).fill(0);
return (
<View
style={{
height: height + waveAmplitude * 2,
overflow: 'hidden',
flexDirection: 'row'
}}
>
{wave.map(this._renderWaves)}
<View
style={{
backgroundColor: waveColor,
...style,
height:
height -
waveAmplitude *
(wavePosition === 'top'
? 2
: wavePosition === 'bottom'
? 3
: 2),
width,
...(wavePosition === 'top'
? { bottom: 0 }
: wavePosition === 'bottom'
? { top: 0 }
: { top: waveAmplitude * 2 }),
position: 'absolute'
}}
>
{this.props.children}
</View>
</View>
);
}
}
WaveView.propTypes = {
height: PropTypes.number,
width: PropTypes.number,
waveColor: PropTypes.string,
waveSpeed: PropTypes.oneOfType([
PropTypes.oneOf(['fast', 'slow']),
PropTypes.number
]),
waveAmplitude: PropTypes.number,
noOfWaves: PropTypes.number,
wavePosition: PropTypes.oneOf(['top', 'bottom', 'both']),
style: PropTypes.any
};
WaveView.defaultProps = {
height: 200,
width: 376,
waveColor: 'blue',
waveSpeed: 'slow',
waveAmplitude: 10,
noOfWaves: 100,
wavePosition: 'both',
style: {}
};