-
Notifications
You must be signed in to change notification settings - Fork 13
/
parabola.js
161 lines (131 loc) · 3.99 KB
/
parabola.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* A smart parabola for react-native apps
* https://github.com/react-native-component/react-native-smart-parabola/
* Released under the MIT license
* Copyright (c) 2016 react-native-component <moonsunfall@aliyun.com>
*/
import React, {
PropTypes,
Component,
} from 'react'
import {
StyleSheet,
View,
Text,
Dimensions,
} from 'react-native'
export default class Parabola extends Component {
static defaultProps = {
rate: 1,
duration: 500,
top: 0,
}
static propTypes = {
isTrigger: PropTypes.bool.isRequired, //true/false
rate: PropTypes.number,
start: PropTypes.shape({
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired,
}).isRequired,
end: PropTypes.shape({
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired,
}).isRequired,
duration: PropTypes.number,
top: PropTypes.number,
renderParabola: PropTypes.func.isRequired,
}
// 构造
constructor (props) {
super(props)
// 初始状态
this.state = {
parabolas: [],
}
this._isAnimating = false
}
componentWillReceiveProps (nextProps) {
let {start, end, isTrigger,} = nextProps
if (isTrigger) {
let parabola = {
start,
end,
translateX: 0,
translateY: 0,
startTime: Date.now(),
animationEnd: false,
}
this._addBall(parabola)
}
}
render () {
let parabolas = this.state.parabolas.map((parabola, index) => {
let {translateX, translateY,} = parabola
return this.props.renderParabola({
index,
translateX,
translateY,
})
})
return (
<View style={{position: 'absolute', left: 0, top: 0}}>
{parabolas}
</View>
)
}
_addBall (parabola) {
this.state.parabolas.push(parabola)
if(!this._isAnimating) {
this._updateBalls()
}
}
_updateBalls () {
this._isAnimating = true
if (this.state.parabolas.length == 0) {
this._isAnimating = false
return
}
let {duration, rate, top: rry1,} = this.props
//let r_animationEnd //test code
let parabolas = this.state.parabolas.map((parabola) => {
let {start, end, startTime, animationEnd} = parabola
let interval = Date.now() - startTime
if (interval > duration) {
if(animationEnd) {
return null
}
else {
interval = duration
parabola.animationEnd = true
//r_animationEnd = true //test code
}
}
let percent = interval / duration
let {x: rx1, y: ry1} = start
let {x: rx2, y: ry2} = end
let direction = rx2 > rx1 ? 1 : -1
let lmy1 = ry2 - rry1
let my1 = ry2 - ry1
let mx2 = direction * (rx2 - rx1)
let lmh = mx2 / 2
//let mh = (1 - my1 / lmy1) * lmh
let mh = (1 - my1 / lmy1) * lmh * ( rate ) * (1 - my1 / lmy1) + lmh * ( 1 - rate) * (1 + my1 / lmy1)
let a = my1 / mx2 / (2 * mh - mx2)
let b = -2 * a * mh
let c = my1
let mx = percent * mx2
let my = a * Math.pow(mx, 2) + b * mx + c
parabola.translateX = rx1 + direction * mx
parabola.translateY = ry2 - my
return parabola
})
parabolas = parabolas.filter((parabola) => {
return parabola != null
})
this.setState({
parabolas,
})
//if(!r_animationEnd) //test code
requestAnimationFrame(this._updateBalls.bind(this))
}
}