-
Notifications
You must be signed in to change notification settings - Fork 11
/
Blur.js
143 lines (130 loc) · 4.64 KB
/
Blur.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
import React, {
Component,
} from 'react'
import PropTypes from 'prop-types'
import {
Animated,
View,
} from 'react-native'
import {
DEFAULT_SHADOW_COLOR,
DEFAULT_SHADOW_OPACITY,
DEFAULT_SHADOW_ANIMATED,
DEFAULT_SHADOW_DURATION,
} from './constants'
export default class Blur extends Component {
static defaultProps = {
shadowColor: DEFAULT_SHADOW_COLOR,
shadowOpacity: DEFAULT_SHADOW_OPACITY,
animated: DEFAULT_SHADOW_ANIMATED,
duration: DEFAULT_SHADOW_DURATION,
}
static propTypes = {
show: PropTypes.bool.isRequired, // eslint-disable-line
// textDimension: PropTypes.shape({
// width: PropTypes.number.isRequired,
// height: PropTypes.number.isRequired,
// }),
containerDimension: PropTypes.object.isRequired, // eslint-disable-line
shadowOpacity: PropTypes.number,
shadowColor: PropTypes.string,
animated: PropTypes.bool,
duration: PropTypes.number, // eslint-disable-line
}
// 构造
constructor(props) {
super(props)
// 初始状态
this.state = {
opacity: props.animated ? new Animated.Value(0) : 0,
}
}
componentWillReceiveProps(nextProps) {
if (!this._boxDimension && nextProps.containerDimension) {
this._boxDimension = this._getBoxDimension(nextProps.containerDimension)
this._boxOffset = this._getBoxOffset(this._boxDimension)
this._shadowOffset = this._getShadowOffset(nextProps.containerDimension, this._boxDimension)
this._shadowRadius = this._getShadowRadius(this._boxDimension)
}
this._toogleBlur(nextProps)
}
render() {
let renderView = null
if (this._boxDimension) {
if (this.props.animated) {
renderView = (
<Animated.View
style={{
opacity: this.state.opacity,
position: 'absolute',
width: this._boxDimension.width,
height: this._boxDimension.height,
left: this._boxOffset.x,
top: this._boxOffset.y,
borderRadius: this._shadowRadius,
shadowColor: this.props.shadowColor,
shadowOpacity: this.props.shadowOpacity,
shadowOffset: this._shadowOffset,
shadowRadius: this._shadowRadius,
}}/>
)
} else {
console.log(`this.props.animated = `, this.props.animated)
renderView = (
<View
style={{
opacity: this.state.opacity,
position: 'absolute',
width: this._boxDimension.width,
height: this._boxDimension.height,
left: this._boxOffset.x,
top: this._boxOffset.y,
borderRadius: this._shadowRadius,
shadowColor: this.props.shadowColor,
shadowOpacity: this.props.shadowOpacity,
shadowOffset: this._shadowOffset,
shadowRadius: this._shadowRadius,
}}/>
)
}
}
return renderView
}
_getBoxDimension(containerDimension) {
return {
// width: textDimension.width,
width: containerDimension.height,
height: containerDimension.height,
}
}
_getBoxOffset(boxDimension) {
return {
x: -boxDimension.width,
y: -boxDimension.height,
}
}
_getShadowOffset(containerDimension, boxDimension) {
return {
width: containerDimension.width / 2 + boxDimension.width / 2,
height: containerDimension.height / 2 + boxDimension.height / 2,
}
}
_getShadowRadius(boxDimension) {
return (Math.min(boxDimension.width, boxDimension.height) / 2) * 0.5
}
_toogleBlur = (nextProps) => {
if (this.props.animated) {
Animated.timing(
this.state.opacity,
{
toValue: nextProps.show ? 1 : 0,
duration: nextProps.duration,
}
).start()
} else {
this.setState({
opacity: nextProps.show ? 1 : 0,
})
}
}
}