@@ -4,51 +4,39 @@ class DrawBox extends React.Component {
4
4
state = {
5
5
isDrawing : false ,
6
6
isCapturing : false ,
7
- isOver : false ,
8
- x : 0 ,
9
- y : 0 ,
10
7
} ;
11
8
12
9
el = React . createRef ( ) ;
13
10
14
11
onDown = event => {
15
- this . setState ( {
16
- isDrawing : true ,
17
- ...this . extractRelativeCoordinates ( event ) ,
18
- } ) ;
12
+ this . setState ( { isDrawing : true } ) ;
19
13
20
14
this . el . current . setPointerCapture ( event . pointerId ) ;
15
+
16
+ const { x, y} = this . extractRelativeCoordinates ( event ) ;
17
+
18
+ const ctx = this . el . current . getContext ( '2d' ) ;
19
+ ctx . beginPath ( ) ;
20
+ ctx . moveTo ( x , y ) ;
21
21
} ;
22
22
23
23
onMove = event => {
24
24
if ( ! this . state . isDrawing ) {
25
25
return ;
26
26
}
27
27
28
- const nextState = this . extractRelativeCoordinates ( event ) ;
28
+ const { x , y } = this . extractRelativeCoordinates ( event ) ;
29
29
30
30
const ctx = this . el . current . getContext ( '2d' ) ;
31
- ctx . beginPath ( ) ;
32
- ctx . moveTo ( this . state . x , this . state . y ) ;
33
- ctx . lineTo ( nextState . x , nextState . y ) ;
31
+ ctx . lineTo ( x , y ) ;
34
32
ctx . stroke ( ) ;
35
- ctx . closePath ( ) ;
36
-
37
- this . setState ( nextState ) ;
38
33
} ;
39
34
40
35
onUp = event => {
41
- this . setState ( {
42
- isDrawing : false ,
43
- } ) ;
44
- } ;
45
-
46
- onOver = event => {
47
- this . setState ( { isOver : true } ) ;
48
- } ;
36
+ this . setState ( { isDrawing : false } ) ;
49
37
50
- onOut = event => {
51
- this . setState ( { isOver : false } ) ;
38
+ const ctx = this . el . current . getContext ( '2d' ) ;
39
+ ctx . closePath ( ) ;
52
40
} ;
53
41
54
42
onGotCapture = event => {
@@ -69,10 +57,10 @@ class DrawBox extends React.Component {
69
57
} ;
70
58
71
59
render ( ) {
72
- const { isOver , isCapturing} = this . state ;
60
+ const { isCapturing} = this . state ;
73
61
74
62
const boxStyle = {
75
- border : `1px solid ${ isCapturing ? 'blue' : isOver ? 'red' : '#d9d9d9' } ` ,
63
+ border : `1px solid ${ isCapturing ? 'blue' : '#d9d9d9' } ` ,
76
64
margin : '10px 0 20px' ,
77
65
touchAction : 'none' ,
78
66
} ;
@@ -87,8 +75,6 @@ class DrawBox extends React.Component {
87
75
onPointerMove = { this . onMove }
88
76
onPointerUp = { this . onUp }
89
77
onPointerCancel = { this . onUp }
90
- onPointerOver = { this . onOver }
91
- onPointerOut = { this . onOut }
92
78
onGotPointerCapture = { this . onGotCapture }
93
79
onLostPointerCapture = { this . onLostCapture }
94
80
/>
0 commit comments