-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathDial.js
200 lines (182 loc) · 5.44 KB
/
Dial.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import React, { Component } from 'react'
import {
Dimensions,
PanResponder,
StyleSheet,
View,
} from 'react-native'
import { throttle } from 'lodash'
const GREY_LIGHT = '#eeeeee'
export class Dial extends Component {
static defaultProps = {
initialRadius: 1,
initialAngle: 0,
precision: 0,
}
constructor(props) {
super(props)
this.state = {
startingAngle: this.props.initialAngle,
startingRadius: this.props.initialRadius,
releaseAngle: this.props.initialAngle,
releaseRadius: this.props.initialRadius,
angle: this.props.initialAngle,
radius: this.props.initialRadius,
}
this.offset = { x: 0, y: 0 }
this.updateState = throttle(this.updateState.bind(this), 16)
}
componentWillMount () {
this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: (e, gestureState) => true,
onStartShouldSetPanResponderCapture: (e, gestureState) => {
this.measureOffset() // measure again
const { deg, radius } = this.calcAngle(e.nativeEvent)
this.setState({ startingAngle: deg, startingRadius: radius })
return true
},
onMoveShouldSetPanResponder: (e, g) => true,
onMoveShouldSetPanResponderCapture: (e, gestureState) => true,
onPanResponderGrant: (e, gestureState) => true,
onPanResponderMove: (e, gestureState) => {
this.updateAngle(gestureState)
},
onPanResponderRelease: (e, gestureState) => {
const {
angle,
radius,
releaseAngle,
releaseRadius,
} = this.state
if (angle !== releaseAngle || radius !== releaseRadius) {
this.setState({
releaseAngle: angle,
releaseRadius: radius,
})
}
},
})
}
onLayout (nativeEvent) {
/*
* Multiple measures to avoid the gap between animated
* and not animated views
*/
this.measureOffset()
setTimeout(() => this.measureOffset(), 200)
}
measureOffset () {
/*
* const {x, y, width, height} = nativeEvent.layout
* onlayout values are different than measureInWindow
* x and y are the distances to its previous element
* but in measureInWindow they are relative to the window
*/
const { width: screenWidth } = Dimensions.get('window')
this.self.measureInWindow((x, y, width, height) => {
this.offset = {
x: x % screenWidth + width / 2,
y: y + height / 2,
}
this.radius = width / 2
})
}
updateAngle (gestureState) {
let { deg, radius } = this.calcAngle(gestureState)
if (deg < 0) deg += 360
if (Math.abs(this.state.angle - deg) > this.props.precision) {
this.updateState({ deg, radius })
}
}
calcAngle (gestureState) {
const { pageX, pageY, moveX, moveY } = gestureState
const [x, y] = [pageX || moveX, pageY || moveY]
const [dx, dy] = [x - this.offset.x, y - this.offset.y]
return {
deg: Math.atan2(dy, dx) * 180 / Math.PI + 120,
radius: Math.sqrt(dy * dy + dx * dx) / this.radius, // pitagoras r^2 = x^2 + y^2 normalizado
}
}
updateState ({ deg, radius = this.state.radius }) {
radius = this.state.releaseRadius + radius - this.state.startingRadius
if (radius < this.props.radiusMin) radius = this.props.radiusMin
else if (radius > this.props.radiusMax) radius = this.props.radiusMax
const angle = deg + this.state.releaseAngle - this.state.startingAngle
if (deg < 0) deg += 360
if (angle !== this.state.angle || radius !== this.state.radius) {
this.setState({ angle, radius })
if (this.props.onValueChange) this.props.onValueChange(angle, radius)
}
}
forceUpdate = (deg: number, radius: number) => {
this.setState({
angle: deg === undefined ? this.state.angle : deg,
radius: radius === undefined ? this.state.radius : radius,
})
}
render () {
const rotate = this.props.fixed ? '0deg' : `${this.state.angle}deg`
const scale = this.props.elastic ? this.state.radius : 1
return (
<View
onLayout={(nativeEvent) => this.onLayout(nativeEvent)}
ref={(node) => { this.self = node }}
style={[styles.coverResponder, this.props.responderStyle]}
{...this._panResponder.panHandlers}
>
{this.props.children
? <View style={[this.props.wrapperStyle, { transform: [{ rotate }, { scale }] }]}>
{this.props.children}
</View>
: <DefaultDial style={this.props.style} rotate={rotate} scale={scale} />
}
</View>
)
}
}
export const DefaultDial = ({ style = {}, rotate = '0rad', scale = 1 }) => (
<View
style={[styles.dial, style, {
transform: [
{ rotate }, { scale },
]
}]} >
<View style={styles.innerDialDecorator}>
<View style={styles.pointer} />
</View>
</View>
)
const styles = StyleSheet.create({
coverResponder: {
padding: 20, // needs a minimum
},
dial: {
width: 120,
height: 120,
backgroundColor: 'white',
borderRadius: 60,
elevation: 5,
shadowColor: GREY_LIGHT,
shadowOffset: { width: 1, height: 2 },
shadowOpacity: 0.8,
shadowRadius: 1,
},
innerDialDecorator: {
top: 10,
left: 10,
width: 100,
height: 100,
borderRadius: 50,
backgroundColor: 'white',
elevation: 3,
},
pointer: {
top: 20,
left: 20,
position: 'absolute',
width: 10,
height: 10,
backgroundColor: 'rgb(221,223,226)',
borderRadius: 5,
},
})