/
BackgroundRippleAnimation.js
90 lines (77 loc) 路 2.19 KB
/
BackgroundRippleAnimation.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
import React from 'react'
import PropTypes from 'prop-types'
import { Animated, Dimensions, Platform } from 'react-native'
import * as easings from './utils/easing'
export default class BackgroundRippleAnimation extends React.PureComponent {
static propTypes = {
containerWidth: PropTypes.number.isRequired,
containerHeight: PropTypes.number.isRequired,
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired,
color: PropTypes.string.isRequired,
onAnimationEnd: PropTypes.func.isRequired
}
state = {
animation: new Animated.Value(0)
}
UNSAFE_componentWillMount() {
this.radius = this.calcRadius()
}
componentDidMount() {
this.startAnimation()
}
calcRadius = () => {
const { containerWidth, containerHeight, x, y } = this.props
const testVetices = [
{ x: 0, y: 0 }, // top left
{ x: containerWidth, y: 0 }, // top right
{ x: 0, y: containerHeight }, // bottom left
{ x: containerWidth, y: containerHeight } // bottom right
]
const possibleRadii = testVetices.map(vertex => {
const dX = vertex.x - x
const dY = vertex.y - y
const radiusSquared = Math.pow(dX, 2) + Math.pow(dY, 2)
return Math.sqrt(radiusSquared)
})
return Math.max(...possibleRadii)
}
startAnimation = () => {
Animated.timing(this.state.animation, {
toValue: 1,
duration: 400,
easing: easings.easeOut,
useNativeDriver: Platform.OS === 'android'
}).start(() => {
this.props.onAnimationEnd()
})
}
render() {
const { x, y, color } = this.props
const { radius } = this
const diameter = radius * 2
const scale = this.state.animation.interpolate({
inputRange: [0, 1],
outputRange: [0.01, 1]
})
const opacity = this.state.animation.interpolate({
inputRange: [0, 0.3, 1],
outputRange: [0, 1, 1]
})
return (
<Animated.View
style={{
position: 'absolute',
top: y - radius,
left: x - radius,
width: diameter,
height: diameter,
borderRadius: radius,
backgroundColor: color,
transform: [{ scale }],
opacity
}}
/>
)
}
}