@@ -39,27 +39,21 @@ var handleOutline = require('./handle_outline');
39
39
var clearOutlineControllers = handleOutline . clearOutlineControllers ;
40
40
var clearSelect = handleOutline . clearSelect ;
41
41
42
- function recordPositions ( polygonsOut , polygonsIn ) { // copy & clean (i.e. skip duplicates)
42
+ function recordPositions ( polygonsOut , polygonsIn ) {
43
43
for ( var i = 0 ; i < polygonsIn . length ; i ++ ) {
44
+ var cell = polygonsIn [ i ] ;
44
45
polygonsOut [ i ] = [ ] ;
45
- var len = polygonsIn [ i ] . length ;
46
- for ( var newJ = 0 , j = 0 ; j < len ; j ++ ) {
47
- // skip close points
48
- if ( j > 0 && dist ( polygonsIn [ i ] [ j ] , polygonsIn [ i ] [ ( j + 1 ) % len ] ) < 1 ) continue ;
49
-
50
- polygonsOut [ i ] [ newJ ] = [ ] ;
51
- for ( var k = 0 ; k < polygonsIn [ i ] [ newJ ] . length ; k ++ ) {
52
- polygonsOut [ i ] [ newJ ] [ k ] = polygonsIn [ i ] [ j ] [ k ] ;
46
+ for ( var j = 0 ; j < cell . length ; j ++ ) {
47
+ polygonsOut [ i ] [ j ] = [ ] ;
48
+ for ( var k = 0 ; k < cell [ j ] . length ; k ++ ) {
49
+ polygonsOut [ i ] [ j ] [ k ] = cell [ j ] [ k ] ;
53
50
}
54
- newJ ++ ;
55
51
}
56
52
}
57
53
return polygonsOut ;
58
54
}
59
55
60
- function displayOutlines ( polygonsIn , outlines , dragOptions , nCalls ) {
61
- var polygons = recordPositions ( [ ] , polygonsIn ) ;
62
-
56
+ function displayOutlines ( polygons , outlines , dragOptions , nCalls ) {
63
57
if ( ! nCalls ) nCalls = 0 ;
64
58
65
59
var gd = dragOptions . gd ;
@@ -87,12 +81,11 @@ function displayOutlines(polygonsIn, outlines, dragOptions, nCalls) {
87
81
88
82
var dragmode = dragOptions . dragmode ;
89
83
var isDrawMode = drawMode ( dragmode ) ;
90
- var isOpenMode = openMode ( dragmode ) ;
91
84
92
85
if ( isDrawMode ) gd . _fullLayout . _drawing = true ;
93
86
94
87
// make outline
95
- outlines . attr ( 'd' , writePaths ( polygons , isOpenMode ) ) ;
88
+ outlines . attr ( 'd' , writePaths ( polygons ) ) ;
96
89
97
90
// add controllers
98
91
var rVertexController = MINSELECT * 1.5 ; // bigger vertex buttons
@@ -337,24 +330,29 @@ function displayOutlines(polygonsIn, outlines, dragOptions, nCalls) {
337
330
}
338
331
}
339
332
340
- function writePaths ( polygons , isOpenMode ) {
333
+ function writePaths ( polygons ) {
341
334
var nI = polygons . length ;
342
335
if ( ! nI ) return 'M0,0Z' ;
343
336
344
337
var str = '' ;
345
338
for ( var i = 0 ; i < nI ; i ++ ) {
346
339
var nJ = polygons [ i ] . length ;
347
340
for ( var j = 0 ; j < nJ ; j ++ ) {
348
- var nK = polygons [ i ] [ j ] . length ;
349
- for ( var k = 0 ; k < nK ; k ++ ) {
350
- str += polygons [ i ] [ j ] [ k ] ;
351
- if ( k > 0 && k < nK - 1 ) {
352
- str += ',' ;
341
+ if ( polygons [ i ] [ j ] [ 0 ] === 'Z' ) {
342
+ str += 'Z' ;
343
+ break ;
344
+ } else {
345
+ var nK = polygons [ i ] [ j ] . length ;
346
+ for ( var k = 0 ; k < nK ; k ++ ) {
347
+ str += polygons [ i ] [ j ] [ k ] ;
348
+ if ( k > 0 && k < nK - 1 ) {
349
+ str += ',' ;
350
+ }
353
351
}
354
352
}
355
353
}
356
- if ( ! isOpenMode ) str += 'Z' ;
357
354
}
355
+
358
356
return str ;
359
357
}
360
358
@@ -434,41 +432,44 @@ function readPaths(str, plotinfo, size, isActiveShape) {
434
432
}
435
433
436
434
break ;
437
- }
438
435
439
- if ( c === 'Z' ) {
440
- x = initX ;
441
- y = initY ;
442
- } else {
443
- for ( var j = 0 ; j < newPos . length ; j ++ ) {
444
- x = newPos [ j ] [ 0 ] ;
445
- y = newPos [ j ] [ 1 ] ;
446
-
447
- if ( ! plotinfo || ! ( plotinfo . xaxis && plotinfo . yaxis ) ) {
448
- polys [ n ] . push ( [
449
- w ,
450
- x ,
451
- y
452
- ] ) ;
453
- } else if ( plotinfo . domain ) {
454
- polys [ n ] . push ( [
455
- w ,
456
- plotinfo . domain . x [ 0 ] + x / size . w ,
457
- plotinfo . domain . y [ 1 ] - y / size . h
458
- ] ) ;
459
- } else if ( isActiveShape === false ) {
460
- polys [ n ] . push ( [
461
- w ,
462
- p2r ( plotinfo . xaxis , x - plotinfo . xaxis . _offset ) ,
463
- p2r ( plotinfo . yaxis , y - plotinfo . yaxis . _offset )
464
- ] ) ;
465
- } else {
466
- polys [ n ] . push ( [
467
- w ,
468
- p2r ( plotinfo . xaxis , x ) ,
469
- p2r ( plotinfo . yaxis , y )
470
- ] ) ;
436
+ case 'Z' :
437
+ if ( x !== initX || y !== initY ) {
438
+ x = initX ;
439
+ y = initY ;
440
+ newPos . push ( [ x , y ] ) ;
471
441
}
442
+ break ;
443
+ }
444
+
445
+ for ( var j = 0 ; j < newPos . length ; j ++ ) {
446
+ x = newPos [ j ] [ 0 ] ;
447
+ y = newPos [ j ] [ 1 ] ;
448
+
449
+ if ( ! plotinfo || ! ( plotinfo . xaxis && plotinfo . yaxis ) ) {
450
+ polys [ n ] . push ( [
451
+ w ,
452
+ x ,
453
+ y
454
+ ] ) ;
455
+ } else if ( plotinfo . domain ) {
456
+ polys [ n ] . push ( [
457
+ w ,
458
+ plotinfo . domain . x [ 0 ] + x / size . w ,
459
+ plotinfo . domain . y [ 1 ] - y / size . h
460
+ ] ) ;
461
+ } else if ( isActiveShape === false ) {
462
+ polys [ n ] . push ( [
463
+ w ,
464
+ p2r ( plotinfo . xaxis , x - plotinfo . xaxis . _offset ) ,
465
+ p2r ( plotinfo . yaxis , y - plotinfo . yaxis . _offset )
466
+ ] ) ;
467
+ } else {
468
+ polys [ n ] . push ( [
469
+ w ,
470
+ p2r ( plotinfo . xaxis , x ) ,
471
+ p2r ( plotinfo . yaxis , y )
472
+ ] ) ;
472
473
}
473
474
}
474
475
}
@@ -518,9 +519,10 @@ function calcMax(cell, dim) {
518
519
return v ;
519
520
}
520
521
521
- function pointsShapeRectangle ( cell , len ) {
522
- if ( ! len ) len = cell . length ;
523
- if ( len !== 4 ) return false ;
522
+ function pointsShapeRectangle ( cell ) {
523
+ var len = cell . length ;
524
+ if ( len !== 5 ) return false ;
525
+
524
526
for ( var j = 1 ; j < 3 ; j ++ ) {
525
527
var e01 = cell [ 0 ] [ j ] - cell [ 1 ] [ j ] ;
526
528
var e32 = cell [ 3 ] [ j ] - cell [ 2 ] [ j ] ;
@@ -545,10 +547,12 @@ function pointsShapeRectangle(cell, len) {
545
547
) ;
546
548
}
547
549
548
- function pointsShapeEllipse ( cell , len ) {
549
- if ( ! len ) len = cell . length ;
550
- if ( len !== CIRCLE_SIDES ) return false ;
550
+ function pointsShapeEllipse ( cell ) {
551
+ var len = cell . length ;
552
+ if ( len !== CIRCLE_SIDES + 1 ) return false ;
553
+
551
554
// opposite diagonals should be the same
555
+ len = CIRCLE_SIDES ;
552
556
for ( var i = 0 ; i < len ; i ++ ) {
553
557
var k = ( len * 2 - i ) % len ;
554
558
@@ -660,30 +664,15 @@ function addNewShapes(outlines, dragOptions) {
660
664
}
661
665
}
662
666
}
667
+
663
668
var isOpenMode = openMode ( dragmode ) ;
664
669
665
670
var polygons = readPaths ( d , plotinfo , gd . _fullLayout . _size , isActiveShape ) ;
666
- if ( isOpenMode ) {
667
- var last = polygons [ 0 ] . length - 1 ;
668
- if ( // ensure first and last positions are not the same on an open path
669
- polygons [ 0 ] [ 0 ] [ 1 ] === polygons [ 0 ] [ last ] [ 1 ] &&
670
- polygons [ 0 ] [ 0 ] [ 2 ] === polygons [ 0 ] [ last ] [ 2 ]
671
- ) {
672
- polygons [ 0 ] . pop ( ) ;
673
- }
674
- }
675
671
676
672
var newShapes = [ ] ;
677
673
for ( var i = 0 ; i < polygons . length ; i ++ ) {
678
674
var cell = polygons [ i ] ;
679
- var len = cell . length ;
680
- if (
681
- cell [ 0 ] [ 1 ] === cell [ len - 1 ] [ 1 ] &&
682
- cell [ 0 ] [ 2 ] === cell [ len - 1 ] [ 2 ]
683
- ) {
684
- len -= 1 ;
685
- }
686
- if ( len < 2 ) continue ;
675
+ if ( cell . length < 2 ) continue ;
687
676
688
677
var shape = {
689
678
editable : true ,
@@ -707,7 +696,7 @@ function addNewShapes(outlines, dragOptions) {
707
696
708
697
if (
709
698
dragmode === 'rectdraw' &&
710
- pointsShapeRectangle ( cell , len ) // should pass len here which is equal to cell.length - 1 i.e. because of the closing point
699
+ pointsShapeRectangle ( cell )
711
700
) {
712
701
shape . type = 'rect' ;
713
702
shape . x0 = cell [ 0 ] [ 1 ] ;
@@ -724,7 +713,7 @@ function addNewShapes(outlines, dragOptions) {
724
713
shape . y1 = cell [ 1 ] [ 2 ] ;
725
714
} else if (
726
715
dragmode === 'ellipsedraw' &&
727
- ( isActiveShape === false || pointsShapeEllipse ( cell , len ) ) // should pass len here which is equal to cell.length - 1 i.e. because of the closing point
716
+ ( isActiveShape === false || pointsShapeEllipse ( cell ) )
728
717
) {
729
718
shape . type = 'circle' ; // an ellipse!
730
719
var pos = { } ;
@@ -758,7 +747,7 @@ function addNewShapes(outlines, dragOptions) {
758
747
fixDatesOnPaths ( cell , xaxis , yaxis ) ;
759
748
}
760
749
761
- shape . path = writePaths ( [ cell ] , isOpenMode ) ;
750
+ shape . path = writePaths ( [ cell ] ) ;
762
751
}
763
752
764
753
newShapes . push ( shape ) ;
0 commit comments