@@ -13,23 +13,30 @@ export default function optimizePaths(slices, settings) {
1313 }
1414
1515 const parts = [ ] ;
16+ const boundingBoxes = new WeakMap ( ) ;
17+ for ( let i = 0 ; i < slice . parts . length ; i ++ ) {
18+ const part = slice . parts [ i ] ;
19+
20+ const shape = part . shape . closed ? part . outerLine : part . shape ;
21+ const bounds = shape . shapeBounds ( ) ;
22+
23+ boundingBoxes . set ( part , bounds ) ;
24+ }
1625
1726 while ( slice . parts . length > 0 ) {
1827 let closestDistance = Infinity ;
1928 let closestPart ;
2029
2130 for ( let i = 0 ; i < slice . parts . length ; i ++ ) {
2231 const part = slice . parts [ i ] ;
32+ const bounds = boundingBoxes . get ( part ) ;
2333
24- const shape = part . shape . closed ? part . outerLine : part . shape ;
25- const bounds = shape . shapeBounds ( ) ;
26-
27- const top = bounds . top - start . y ;
28- const bottom = start . y - bounds . bottom ;
29- const left = bounds . left - start . x ;
30- const right = start . x - bounds . right ;
34+ const topDistance = bounds . top - start . y ;
35+ const bottomDistance = start . y - bounds . bottom ;
36+ const leftDistance = bounds . left - start . x ;
37+ const rightDistance = start . x - bounds . right ;
3138
32- const distance = Math . max ( top , bottom , left , right ) ;
39+ const distance = Math . max ( topDistance , bottomDistance , leftDistance , rightDistance ) ;
3340
3441 if ( distance < closestDistance ) {
3542 closestDistance = distance ;
0 commit comments