@@ -4,51 +4,39 @@ class DrawBox extends React.Component {
44 state = {
55 isDrawing : false ,
66 isCapturing : false ,
7- isOver : false ,
8- x : 0 ,
9- y : 0 ,
107 } ;
118
129 el = React . createRef ( ) ;
1310
1411 onDown = event => {
15- this . setState ( {
16- isDrawing : true ,
17- ...this . extractRelativeCoordinates ( event ) ,
18- } ) ;
12+ this . setState ( { isDrawing : true } ) ;
1913
2014 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 ) ;
2121 } ;
2222
2323 onMove = event => {
2424 if ( ! this . state . isDrawing ) {
2525 return ;
2626 }
2727
28- const nextState = this . extractRelativeCoordinates ( event ) ;
28+ const { x , y } = this . extractRelativeCoordinates ( event ) ;
2929
3030 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 ) ;
3432 ctx . stroke ( ) ;
35- ctx . closePath ( ) ;
36-
37- this . setState ( nextState ) ;
3833 } ;
3934
4035 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 } ) ;
4937
50- onOut = event => {
51- this . setState ( { isOver : false } ) ;
38+ const ctx = this . el . current . getContext ( '2d' ) ;
39+ ctx . closePath ( ) ;
5240 } ;
5341
5442 onGotCapture = event => {
@@ -69,10 +57,10 @@ class DrawBox extends React.Component {
6957 } ;
7058
7159 render ( ) {
72- const { isOver , isCapturing} = this . state ;
60+ const { isCapturing} = this . state ;
7361
7462 const boxStyle = {
75- border : `1px solid ${ isCapturing ? 'blue' : isOver ? 'red' : '#d9d9d9' } ` ,
63+ border : `1px solid ${ isCapturing ? 'blue' : '#d9d9d9' } ` ,
7664 margin : '10px 0 20px' ,
7765 touchAction : 'none' ,
7866 } ;
@@ -87,8 +75,6 @@ class DrawBox extends React.Component {
8775 onPointerMove = { this . onMove }
8876 onPointerUp = { this . onUp }
8977 onPointerCancel = { this . onUp }
90- onPointerOver = { this . onOver }
91- onPointerOut = { this . onOut }
9278 onGotPointerCapture = { this . onGotCapture }
9379 onLostPointerCapture = { this . onLostCapture }
9480 />
0 commit comments