Repository navigation
Expand file tree
/
Copy pathSurfaceView.js
More file actions
executable file
·189 lines (170 loc) · 4.54 KB
/
Copy pathSurfaceView.js
File metadata and controls
executable file
·189 lines (170 loc) · 4.54 KB
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
/*
Copyright © SuperMap. All rights reserved.
Author: Yang Shanglong
E-mail: yangshanglong@supermap.com
*/
import React, { Component } from 'react'
import { View, ART, PanResponder, Dimensions } from 'react-native'
import { color } from '../../styles'
import styles from './styles'
const ShapeType = {
RECTANGLE: 'RECTANGLE',
CIRCLE: 'CIRCLE',
}
export { ShapeType }
export default class SurfaceView extends Component {
props: {
style: Object,
type: string,
}
static defaultProps = {
style: styles.container,
type: ShapeType.RECTANGLE,
}
constructor(props) {
super(props)
this.state = {
isShow: false,
startPoint: {
x: 0,
y: 0,
},
endPoint: {
x: 0,
y: 0,
},
}
this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder,
onMoveShouldSetPanResponder: this._handleMoveShouldSetPanResponder,
onPanResponderGrant: this._onPanResponderGrant,
onPanResponderMove: this._handlePanResponderMove,
onPanResponderRelease: this._handlePanResponderEnd,
onPanResponderTerminate: this._handlePanResponderEnd,
})
this.path = ART.Path()
}
shouldComponentUpdate(nextProps, nextState) {
return (
JSON.stringify(nextState) !== JSON.stringify(this.state) ||
JSON.stringify(nextProps) !== JSON.stringify(this.props)
)
}
_handleStartShouldSetPanResponder = () => {
return true
}
_handleMoveShouldSetPanResponder = () => {
return true
}
_onPanResponderGrant = (evt, gestureState) => {
this.setState({
startPoint: {
x: gestureState.x0,
y: gestureState.y0,
},
endPoint: {
x: gestureState.x0,
y: gestureState.y0,
},
})
}
_handlePanResponderMove = (evt, gestureState) => {
this.setState({
endPoint: {
x: gestureState.moveX,
y: gestureState.moveY,
},
})
}
_handlePanResponderEnd = () => {}
show = isShow => {
let newState = {}
if (
(isShow === undefined && this.state.isShow) ||
(isShow !== undefined && !isShow)
) {
newState = {
isShow: false,
startPoint: {
x: 0,
y: 0,
},
endPoint: {
x: 0,
y: 0,
},
}
} else {
newState = {
isShow: true,
}
}
this.setState(newState)
}
getResult = () => {
switch (this.props.type) {
case ShapeType.CIRCLE:
return [this.state.startPoint, this.state.endPoint]
case ShapeType.RECTANGLE:
default:
return [
this.state.startPoint,
{ x: this.state.startPoint.x, y: this.state.endPoint.y },
this.state.endPoint,
{ x: this.state.endPoint.x, y: this.state.startPoint.y },
]
}
}
draw = () => {
switch (this.props.type) {
case ShapeType.CIRCLE:
this._drawCircle()
break
case ShapeType.RECTANGLE:
default:
this._drawRectangle()
}
}
/** 画矩形 **/
_drawRectangle = () => {
this.path = ART.Path()
this.path
.moveTo(this.state.startPoint.x, this.state.startPoint.y)
.lineTo(this.state.endPoint.x, this.state.startPoint.y)
.lineTo(this.state.endPoint.x, this.state.endPoint.y)
.lineTo(this.state.startPoint.x, this.state.endPoint.y)
.close()
}
/** 画圆 **/
_drawCircle = () => {
this.path = ART.Path()
const dx = Math.abs(this.state.endPoint.x - this.state.startPoint.x)
const dy = Math.abs(this.state.endPoint.y - this.state.startPoint.y)
let circleRadius = Math.max(dx, dy) / 2
this.path
.moveTo(
// Math.min(this.state.startPoint.x, this.state.endPoint.x) + circleRadius,
this.state.startPoint.x > this.state.endPoint.x
? this.state.startPoint.x - circleRadius
: this.state.startPoint.x + circleRadius,
this.state.startPoint.y,
// Math.min(this.state.startPoint.y, this.state.endPoint.y),
)
.arc(0, Math.max(dx, dy), circleRadius)
.arc(0, -Math.max(dx, dy), circleRadius)
.close()
}
render() {
if (!this.state.isShow) return null
this.draw()
const dWidth = Dimensions.get('window').width
const dHeight = Dimensions.get('window').height
return (
<View {...this._panResponder.panHandlers} style={this.props.style}>
<ART.Surface width={dWidth} height={dHeight}>
<ART.Shape d={this.path} stroke={color.switch} strokeWidth={5} />
</ART.Surface>
</View>
)
}
}