@@ -34,6 +34,10 @@ export class DrawHandlerImpl implements DrawHandler {
34
34
private onDrawDone : ( data : object , continueDraw ?: boolean ) => void ;
35
35
private canvas : SVG . Container ;
36
36
private text : SVG . Container ;
37
+ private cursorPosition : {
38
+ x : number ;
39
+ y : number ;
40
+ } ;
37
41
private crosshair : {
38
42
x : SVG . Line ;
39
43
y : SVG . Line ;
@@ -96,12 +100,13 @@ export class DrawHandlerImpl implements DrawHandler {
96
100
}
97
101
98
102
private addCrosshair ( ) : void {
103
+ const { x, y } = this . cursorPosition ;
99
104
this . crosshair = {
100
- x : this . canvas . line ( 0 , 0 , this . canvas . node . clientWidth , 0 ) . attr ( {
105
+ x : this . canvas . line ( 0 , y , this . canvas . node . clientWidth , y ) . attr ( {
101
106
'stroke-width' : consts . BASE_STROKE_WIDTH / ( 2 * this . geometry . scale ) ,
102
107
zOrder : Number . MAX_SAFE_INTEGER ,
103
108
} ) . addClass ( 'cvat_canvas_crosshair' ) ,
104
- y : this . canvas . line ( 0 , 0 , 0 , this . canvas . node . clientHeight ) . attr ( {
109
+ y : this . canvas . line ( x , 0 , x , this . canvas . node . clientHeight ) . attr ( {
105
110
'stroke-width' : consts . BASE_STROKE_WIDTH / ( 2 * this . geometry . scale ) ,
106
111
zOrder : Number . MAX_SAFE_INTEGER ,
107
112
} ) . addClass ( 'cvat_canvas_crosshair' ) ,
@@ -181,7 +186,6 @@ export class DrawHandlerImpl implements DrawHandler {
181
186
this . shapeSizeElement . update ( this . drawInstance ) ;
182
187
} ) . addClass ( 'cvat_canvas_shape_drawing' ) . attr ( {
183
188
'stroke-width' : consts . BASE_STROKE_WIDTH / this . geometry . scale ,
184
- z_order : Number . MAX_SAFE_INTEGER ,
185
189
} ) ;
186
190
}
187
191
@@ -222,10 +226,6 @@ export class DrawHandlerImpl implements DrawHandler {
222
226
}
223
227
224
228
private drawPolyshape ( ) : void {
225
- this . drawInstance . attr ( {
226
- z_order : Number . MAX_SAFE_INTEGER ,
227
- } ) ;
228
-
229
229
let size = this . drawData . numberOfPoints ;
230
230
const sizeDecrement = function sizeDecrement ( ) : void {
231
231
if ( ! -- size ) {
@@ -371,18 +371,17 @@ export class DrawHandlerImpl implements DrawHandler {
371
371
372
372
// Common settings for rectangle and polyshapes
373
373
private pasteShape ( ) : void {
374
- this . drawInstance . attr ( {
375
- z_order : Number . MAX_SAFE_INTEGER ,
376
- } ) ;
374
+ function moveShape ( shape : SVG . Shape , x : number , y : number ) : void {
375
+ const bbox = shape . bbox ( ) ;
376
+ shape . move ( x - bbox . width / 2 , y - bbox . height / 2 ) ;
377
+ }
377
378
378
- this . canvas . on ( 'mousemove.draw' , ( e : MouseEvent ) : void => {
379
- const [ x , y ] = translateToSVG (
380
- this . canvas . node as any as SVGSVGElement ,
381
- [ e . clientX , e . clientY ] ,
382
- ) ;
379
+ const { x : initialX , y : initialY } = this . cursorPosition ;
380
+ moveShape ( this . drawInstance , initialX , initialY ) ;
383
381
384
- const bbox = this . drawInstance . bbox ( ) ;
385
- this . drawInstance . move ( x - bbox . width / 2 , y - bbox . height / 2 ) ;
382
+ this . canvas . on ( 'mousemove.draw' , ( ) : void => {
383
+ const { x, y } = this . cursorPosition ; // was computer in another callback
384
+ moveShape ( this . drawInstance , x , y ) ;
386
385
} ) ;
387
386
}
388
387
@@ -429,45 +428,53 @@ export class DrawHandlerImpl implements DrawHandler {
429
428
this . pastePolyshape ( ) ;
430
429
}
431
430
432
- private pastePoints ( points : string ) : void {
433
- this . drawInstance = ( this . canvas as any ) . polyline ( points )
431
+ private pastePoints ( initialPoints : string ) : void {
432
+ function moveShape (
433
+ shape : SVG . PolyLine ,
434
+ group : SVG . G ,
435
+ x : number ,
436
+ y : number ,
437
+ scale : number ,
438
+ ) : void {
439
+ const bbox = shape . bbox ( ) ;
440
+ shape . move ( x - bbox . width / 2 , y - bbox . height / 2 ) ;
441
+
442
+ const points = shape . attr ( 'points' ) . split ( ' ' ) ;
443
+ const radius = consts . BASE_POINT_SIZE / scale ;
444
+
445
+ group . children ( ) . forEach ( ( child : SVG . Element , idx : number ) : void => {
446
+ const [ px , py ] = points [ idx ] . split ( ',' ) ;
447
+ child . move ( px - radius / 2 , py - radius / 2 ) ;
448
+ } ) ;
449
+ }
450
+
451
+ const { x : initialX , y : initialY } = this . cursorPosition ;
452
+ this . pointsGroup = this . canvas . group ( ) ;
453
+ this . drawInstance = ( this . canvas as any ) . polyline ( initialPoints )
434
454
. addClass ( 'cvat_canvas_shape_drawing' ) . style ( {
435
455
'stroke-width' : 0 ,
436
456
} ) ;
437
457
438
- this . pointsGroup = this . canvas . group ( ) ;
439
- for ( const point of points . split ( ' ' ) ) {
458
+ let numOfPoints = initialPoints . split ( ' ' ) . length ;
459
+ while ( numOfPoints ) {
460
+ numOfPoints -- ;
440
461
const radius = consts . BASE_POINT_SIZE / this . geometry . scale ;
441
462
const stroke = consts . POINTS_STROKE_WIDTH / this . geometry . scale ;
442
- const [ x , y ] = point . split ( ',' ) . map ( ( coord : string ) : number => + coord ) ;
443
- this . pointsGroup . circle ( ) . move ( x - radius / 2 , y - radius / 2 )
444
- . fill ( 'white' ) . stroke ( 'black' ) . attr ( {
445
- r : radius ,
446
- 'stroke-width' : stroke ,
447
- } ) ;
463
+ this . pointsGroup . circle ( ) . fill ( 'white' ) . stroke ( 'black' ) . attr ( {
464
+ r : radius ,
465
+ 'stroke-width' : stroke ,
466
+ } ) ;
448
467
}
449
468
450
- this . pointsGroup . attr ( {
451
- z_order : Number . MAX_SAFE_INTEGER ,
452
- } ) ;
469
+ moveShape (
470
+ this . drawInstance , this . pointsGroup , initialX , initialY , this . geometry . scale ,
471
+ ) ;
453
472
454
- this . canvas . on ( 'mousemove.draw' , ( e : MouseEvent ) : void => {
455
- const [ x , y ] = translateToSVG (
456
- this . canvas . node as any as SVGSVGElement ,
457
- [ e . clientX , e . clientY ] ,
473
+ this . canvas . on ( 'mousemove.draw' , ( ) : void => {
474
+ const { x, y } = this . cursorPosition ; // was computer in another callback
475
+ moveShape (
476
+ this . drawInstance , this . pointsGroup , x , y , this . geometry . scale ,
458
477
) ;
459
-
460
- const bbox = this . drawInstance . bbox ( ) ;
461
- this . drawInstance . move ( x - bbox . width / 2 , y - bbox . height / 2 ) ;
462
- const radius = consts . BASE_POINT_SIZE / this . geometry . scale ;
463
- const newPoints = this . drawInstance . attr ( 'points' ) . split ( ' ' ) ;
464
- if ( this . pointsGroup ) {
465
- this . pointsGroup . children ( )
466
- . forEach ( ( child : SVG . Element , idx : number ) : void => {
467
- const [ px , py ] = newPoints [ idx ] . split ( ',' ) ;
468
- child . move ( px - radius / 2 , py - radius / 2 ) ;
469
- } ) ;
470
- }
471
478
} ) ;
472
479
473
480
this . pastePolyshape ( ) ;
@@ -593,23 +600,20 @@ export class DrawHandlerImpl implements DrawHandler {
593
600
this . crosshair = null ;
594
601
this . drawInstance = null ;
595
602
this . pointsGroup = null ;
603
+ this . cursorPosition = {
604
+ x : 0 ,
605
+ y : 0 ,
606
+ } ;
596
607
597
608
this . canvas . on ( 'mousemove.crosshair' , ( e : MouseEvent ) : void => {
609
+ const [ x , y ] = translateToSVG (
610
+ this . canvas . node as any as SVGSVGElement ,
611
+ [ e . clientX , e . clientY ] ,
612
+ ) ;
613
+ this . cursorPosition = { x, y } ;
598
614
if ( this . crosshair ) {
599
- const [ x , y ] = translateToSVG (
600
- this . canvas . node as any as SVGSVGElement ,
601
- [ e . clientX , e . clientY ] ,
602
- ) ;
603
-
604
- this . crosshair . x . attr ( {
605
- y1 : y ,
606
- y2 : y ,
607
- } ) ;
608
-
609
- this . crosshair . y . attr ( {
610
- x1 : x ,
611
- x2 : x ,
612
- } ) ;
615
+ this . crosshair . x . attr ( { y1 : y , y2 : y } ) ;
616
+ this . crosshair . y . attr ( { x1 : x , x2 : x } ) ;
613
617
}
614
618
} ) ;
615
619
}
0 commit comments