@antv/util 3.2.2 → 3.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/dist/util.min.js +1 -1
  2. package/esm/path/convert/path-2-absolute.js +4 -3
  3. package/esm/path/convert/path-2-absolute.js.map +1 -1
  4. package/esm/path/parser/finalize-segment.js +2 -3
  5. package/esm/path/parser/finalize-segment.js.map +1 -1
  6. package/esm/path/process/arc-2-cubic.js +4 -3
  7. package/esm/path/process/arc-2-cubic.js.map +1 -1
  8. package/esm/path/process/clone-path.js +1 -2
  9. package/esm/path/process/clone-path.js.map +1 -1
  10. package/esm/path/process/line-2-cubic.js +4 -5
  11. package/esm/path/process/line-2-cubic.js.map +1 -1
  12. package/esm/path/process/normalize-path.js +1 -6
  13. package/esm/path/process/normalize-path.js.map +1 -1
  14. package/esm/path/process/normalize-segment.js +2 -3
  15. package/esm/path/process/normalize-segment.js.map +1 -1
  16. package/esm/path/process/round-path.js +2 -2
  17. package/esm/path/process/round-path.js.map +1 -1
  18. package/esm/path/process/segment-2-cubic.js +8 -7
  19. package/esm/path/process/segment-2-cubic.js.map +1 -1
  20. package/esm/path/types.d.ts +5 -0
  21. package/esm/path/util/equalize-segments.js +3 -1
  22. package/esm/path/util/equalize-segments.js.map +1 -1
  23. package/esm/path/util/get-path-area.js +2 -2
  24. package/esm/path/util/get-path-area.js.map +1 -1
  25. package/esm/path/util/get-path-bbox-total-length.d.ts +2 -2
  26. package/esm/path/util/get-path-bbox-total-length.js +3 -2
  27. package/esm/path/util/get-path-bbox-total-length.js.map +1 -1
  28. package/esm/path/util/get-path-bbox.d.ts +2 -2
  29. package/esm/path/util/get-path-bbox.js +3 -2
  30. package/esm/path/util/get-path-bbox.js.map +1 -1
  31. package/esm/path/util/get-point-at-length.d.ts +2 -2
  32. package/esm/path/util/get-point-at-length.js +3 -2
  33. package/esm/path/util/get-point-at-length.js.map +1 -1
  34. package/esm/path/util/get-properties-at-length.js +1 -2
  35. package/esm/path/util/get-properties-at-length.js.map +1 -1
  36. package/esm/path/util/get-total-length.d.ts +2 -2
  37. package/esm/path/util/get-total-length.js +3 -2
  38. package/esm/path/util/get-total-length.js.map +1 -1
  39. package/esm/path/util/path-length-factory.d.ts +2 -2
  40. package/esm/path/util/path-length-factory.js +13 -19
  41. package/esm/path/util/path-length-factory.js.map +1 -1
  42. package/esm/path/util/segment-arc-factory.d.ts +4 -2
  43. package/esm/path/util/segment-arc-factory.js +15 -9
  44. package/esm/path/util/segment-arc-factory.js.map +1 -1
  45. package/esm/path/util/segment-cubic-factory.d.ts +2 -2
  46. package/esm/path/util/segment-cubic-factory.js +12 -8
  47. package/esm/path/util/segment-cubic-factory.js.map +1 -1
  48. package/esm/path/util/segment-quad-factory.d.ts +2 -2
  49. package/esm/path/util/segment-quad-factory.js +12 -8
  50. package/esm/path/util/segment-quad-factory.js.map +1 -1
  51. package/lib/path/convert/path-2-absolute.js +4 -3
  52. package/lib/path/convert/path-2-absolute.js.map +1 -1
  53. package/lib/path/parser/finalize-segment.js +2 -3
  54. package/lib/path/parser/finalize-segment.js.map +1 -1
  55. package/lib/path/process/arc-2-cubic.js +4 -3
  56. package/lib/path/process/arc-2-cubic.js.map +1 -1
  57. package/lib/path/process/clone-path.js +1 -2
  58. package/lib/path/process/clone-path.js.map +1 -1
  59. package/lib/path/process/line-2-cubic.js +4 -5
  60. package/lib/path/process/line-2-cubic.js.map +1 -1
  61. package/lib/path/process/normalize-path.js +1 -6
  62. package/lib/path/process/normalize-path.js.map +1 -1
  63. package/lib/path/process/normalize-segment.js +2 -3
  64. package/lib/path/process/normalize-segment.js.map +1 -1
  65. package/lib/path/process/round-path.js +2 -2
  66. package/lib/path/process/round-path.js.map +1 -1
  67. package/lib/path/process/segment-2-cubic.js +8 -7
  68. package/lib/path/process/segment-2-cubic.js.map +1 -1
  69. package/lib/path/types.d.ts +5 -0
  70. package/lib/path/util/equalize-segments.js +3 -1
  71. package/lib/path/util/equalize-segments.js.map +1 -1
  72. package/lib/path/util/get-path-area.js +2 -2
  73. package/lib/path/util/get-path-area.js.map +1 -1
  74. package/lib/path/util/get-path-bbox-total-length.d.ts +2 -2
  75. package/lib/path/util/get-path-bbox-total-length.js +3 -2
  76. package/lib/path/util/get-path-bbox-total-length.js.map +1 -1
  77. package/lib/path/util/get-path-bbox.d.ts +2 -2
  78. package/lib/path/util/get-path-bbox.js +3 -2
  79. package/lib/path/util/get-path-bbox.js.map +1 -1
  80. package/lib/path/util/get-point-at-length.d.ts +2 -2
  81. package/lib/path/util/get-point-at-length.js +3 -2
  82. package/lib/path/util/get-point-at-length.js.map +1 -1
  83. package/lib/path/util/get-properties-at-length.js +1 -2
  84. package/lib/path/util/get-properties-at-length.js.map +1 -1
  85. package/lib/path/util/get-total-length.d.ts +2 -2
  86. package/lib/path/util/get-total-length.js +3 -2
  87. package/lib/path/util/get-total-length.js.map +1 -1
  88. package/lib/path/util/path-length-factory.d.ts +2 -2
  89. package/lib/path/util/path-length-factory.js +13 -19
  90. package/lib/path/util/path-length-factory.js.map +1 -1
  91. package/lib/path/util/segment-arc-factory.d.ts +4 -2
  92. package/lib/path/util/segment-arc-factory.js +15 -9
  93. package/lib/path/util/segment-arc-factory.js.map +1 -1
  94. package/lib/path/util/segment-cubic-factory.d.ts +2 -2
  95. package/lib/path/util/segment-cubic-factory.js +12 -8
  96. package/lib/path/util/segment-cubic-factory.js.map +1 -1
  97. package/lib/path/util/segment-quad-factory.d.ts +2 -2
  98. package/lib/path/util/segment-quad-factory.js +12 -8
  99. package/lib/path/util/segment-quad-factory.js.map +1 -1
  100. package/package.json +2 -1
  101. package/src/path/convert/path-2-absolute.ts +4 -2
  102. package/src/path/parser/finalize-segment.ts +2 -2
  103. package/src/path/process/arc-2-cubic.ts +5 -2
  104. package/src/path/process/clone-path.ts +1 -1
  105. package/src/path/process/line-2-cubic.ts +5 -4
  106. package/src/path/process/normalize-path.ts +1 -7
  107. package/src/path/process/normalize-segment.ts +2 -2
  108. package/src/path/process/round-path.ts +2 -1
  109. package/src/path/process/segment-2-cubic.ts +11 -7
  110. package/src/path/types.ts +5 -0
  111. package/src/path/util/equalize-segments.ts +14 -1
  112. package/src/path/util/get-path-area.ts +3 -1
  113. package/src/path/util/get-path-bbox-total-length.ts +6 -3
  114. package/src/path/util/get-path-bbox.ts +3 -3
  115. package/src/path/util/get-point-at-length.ts +7 -3
  116. package/src/path/util/get-properties-at-length.ts +1 -1
  117. package/src/path/util/get-total-length.ts +3 -3
  118. package/src/path/util/path-length-factory.ts +62 -19
  119. package/src/path/util/segment-arc-factory.ts +30 -9
  120. package/src/path/util/segment-cubic-factory.ts +27 -8
  121. package/src/path/util/segment-quad-factory.ts +27 -8
@@ -1,5 +1,5 @@
1
1
  import { normalizePath } from '../process/normalize-path';
2
- import type { PathCommand, PathArray, LengthFactory } from '../types';
2
+ import type { PathCommand, PathArray, LengthFactory, PathLengthFactoryOptions } from '../types';
3
3
  import { segmentLineFactory } from './segment-line-factory';
4
4
  import { segmentArcFactory } from './segment-arc-factory';
5
5
  import { segmentCubicFactory } from './segment-cubic-factory';
@@ -10,7 +10,11 @@ import { segmentQuadFactory } from './segment-quad-factory';
10
10
  * of a shape, the shape total length and
11
11
  * the shape minimum and maximum {x,y} coordinates.
12
12
  */
13
- export function pathLengthFactory(pathInput: string | PathArray, distance?: number): LengthFactory {
13
+ export function pathLengthFactory(
14
+ pathInput: string | PathArray,
15
+ distance?: number,
16
+ options?: Partial<PathLengthFactoryOptions>,
17
+ ): LengthFactory {
14
18
  const path = normalizePath(pathInput);
15
19
  const distanceIsNumber = typeof distance === 'number';
16
20
  let isM: boolean;
@@ -34,7 +38,7 @@ export function pathLengthFactory(pathInput: string | PathArray, distance?: numb
34
38
  seg = path[i];
35
39
  [pathCommand] = seg;
36
40
  isM = pathCommand === 'M';
37
- data = !isM ? [x, y, ...seg.slice(1)] : data;
41
+ data = !isM ? [x, y].concat(seg.slice(1)) : data;
38
42
 
39
43
  // this segment is always ZERO
40
44
  /* istanbul ignore else */
@@ -49,29 +53,56 @@ export function pathLengthFactory(pathInput: string | PathArray, distance?: numb
49
53
  POINT = min;
50
54
  }
51
55
  } else if (pathCommand === 'L') {
52
- // @ts-ignore
53
- ({ length, min, max, point } = segmentLineFactory(...data, (distance || 0) - LENGTH));
56
+ ({ length, min, max, point } = segmentLineFactory(data[0], data[1], data[2], data[3], (distance || 0) - LENGTH));
54
57
  } else if (pathCommand === 'A') {
55
- // @ts-ignore
56
- ({ length, min, max, point } = segmentArcFactory(...data, (distance || 0) - LENGTH));
58
+ ({ length, min, max, point } = segmentArcFactory(
59
+ data[0],
60
+ data[1],
61
+ data[2],
62
+ data[3],
63
+ data[4],
64
+ data[5],
65
+ data[6],
66
+ data[7],
67
+ data[8],
68
+ (distance || 0) - LENGTH,
69
+ options || {},
70
+ ));
57
71
  } else if (pathCommand === 'C') {
58
- // @ts-ignore
59
- ({ length, min, max, point } = segmentCubicFactory(...data, (distance || 0) - LENGTH));
72
+ ({ length, min, max, point } = segmentCubicFactory(
73
+ data[0],
74
+ data[1],
75
+ data[2],
76
+ data[3],
77
+ data[4],
78
+ data[5],
79
+ data[6],
80
+ data[7],
81
+ (distance || 0) - LENGTH,
82
+ options || {},
83
+ ));
60
84
  } else if (pathCommand === 'Q') {
61
- // @ts-ignore
62
- ({ length, min, max, point } = segmentQuadFactory(...data, (distance || 0) - LENGTH));
85
+ ({ length, min, max, point } = segmentQuadFactory(
86
+ data[0],
87
+ data[1],
88
+ data[2],
89
+ data[3],
90
+ data[4],
91
+ data[5],
92
+ (distance || 0) - LENGTH,
93
+ options || {},
94
+ ));
63
95
  } else if (pathCommand === 'Z') {
64
96
  data = [x, y, mx, my];
65
- // @ts-ignore
66
- ({ length, min, max, point } = segmentLineFactory(...data, (distance || 0) - LENGTH));
97
+ ({ length, min, max, point } = segmentLineFactory(data[0], data[1], data[2], data[3], (distance || 0) - LENGTH));
67
98
  }
68
99
 
69
100
  if (distanceIsNumber && LENGTH < distance && LENGTH + length >= distance) {
70
101
  POINT = point;
71
102
  }
72
103
 
73
- MAX = [...MAX, max];
74
- MIN = [...MIN, min];
104
+ MAX.push(max);
105
+ MIN.push(min);
75
106
  LENGTH += length;
76
107
 
77
108
  [x, y] = pathCommand !== 'Z' ? seg.slice(-2) : [mx, my];
@@ -87,12 +118,24 @@ export function pathLengthFactory(pathInput: string | PathArray, distance?: numb
87
118
  length: LENGTH,
88
119
  point: POINT,
89
120
  min: {
90
- x: Math.min(...MIN.map((n) => n.x)),
91
- y: Math.min(...MIN.map((n) => n.y)),
121
+ x: Math.min.apply(
122
+ null,
123
+ MIN.map((n) => n.x),
124
+ ),
125
+ y: Math.min.apply(
126
+ null,
127
+ MIN.map((n) => n.y),
128
+ ),
92
129
  },
93
130
  max: {
94
- x: Math.max(...MAX.map((n) => n.x)),
95
- y: Math.max(...MAX.map((n) => n.y)),
131
+ x: Math.max.apply(
132
+ null,
133
+ MAX.map((n) => n.x),
134
+ ),
135
+ y: Math.max.apply(
136
+ null,
137
+ MAX.map((n) => n.y),
138
+ ),
96
139
  },
97
140
  };
98
141
  }
@@ -1,4 +1,4 @@
1
- import type { Point, LengthFactory } from '../types';
1
+ import type { Point, LengthFactory, PathLengthFactoryOptions } from '../types';
2
2
  import { segmentLineFactory } from './segment-line-factory';
3
3
  import { distanceSquareRoot } from './distance-square-root';
4
4
 
@@ -120,6 +120,8 @@ function getPointAtArcSegmentLength(
120
120
  /**
121
121
  * Returns a {x,y} point at a given length, the total length and
122
122
  * the shape minimum and maximum {x,y} coordinates of an A (arc-to) segment.
123
+ *
124
+ * For better performance, it can skip calculate bbox or length in some scenario.
123
125
  */
124
126
  export function segmentArcFactory(
125
127
  X1: number,
@@ -132,7 +134,9 @@ export function segmentArcFactory(
132
134
  X2: number,
133
135
  Y2: number,
134
136
  distance: number,
137
+ options: Partial<PathLengthFactoryOptions>,
135
138
  ): LengthFactory {
139
+ const { bbox = true, length = true, sampleSize = 30 } = options;
136
140
  const distanceIsNumber = typeof distance === 'number';
137
141
  let x = X1;
138
142
  let y = Y1;
@@ -147,14 +151,19 @@ export function segmentArcFactory(
147
151
  POINT = { x, y };
148
152
  }
149
153
 
150
- // bad perf when size = 300
151
- const sampleSize = 100;
154
+ // bad perf when size > 100
152
155
  for (let j = 0; j <= sampleSize; j += 1) {
153
156
  t = j / sampleSize;
154
157
 
155
158
  ({ x, y } = getPointAtArcSegmentLength(X1, Y1, RX, RY, angle, LAF, SF, X2, Y2, t));
156
- POINTS = POINTS.concat({ x, y });
157
- LENGTH += distanceSquareRoot(cur, [x, y]);
159
+
160
+ if (bbox) {
161
+ POINTS.push({ x, y });
162
+ }
163
+
164
+ if (length) {
165
+ LENGTH += distanceSquareRoot(cur, [x, y]);
166
+ }
158
167
  cur = [x, y];
159
168
 
160
169
  if (distanceIsNumber && LENGTH >= distance && distance > prev[2]) {
@@ -176,12 +185,24 @@ export function segmentArcFactory(
176
185
  length: LENGTH,
177
186
  point: POINT,
178
187
  min: {
179
- x: Math.min(...POINTS.map((n) => n.x)),
180
- y: Math.min(...POINTS.map((n) => n.y)),
188
+ x: Math.min.apply(
189
+ null,
190
+ POINTS.map((n) => n.x),
191
+ ),
192
+ y: Math.min.apply(
193
+ null,
194
+ POINTS.map((n) => n.y),
195
+ ),
181
196
  },
182
197
  max: {
183
- x: Math.max(...POINTS.map((n) => n.x)),
184
- y: Math.max(...POINTS.map((n) => n.y)),
198
+ x: Math.max.apply(
199
+ null,
200
+ POINTS.map((n) => n.x),
201
+ ),
202
+ y: Math.max.apply(
203
+ null,
204
+ POINTS.map((n) => n.y),
205
+ ),
185
206
  },
186
207
  };
187
208
  }
@@ -1,4 +1,4 @@
1
- import type { LengthFactory } from '../types';
1
+ import type { LengthFactory, PathLengthFactoryOptions } from '../types';
2
2
  import { distanceSquareRoot } from './distance-square-root';
3
3
 
4
4
  /**
@@ -37,7 +37,9 @@ export function segmentCubicFactory(
37
37
  x2: number,
38
38
  y2: number,
39
39
  distance: number,
40
+ options: Partial<PathLengthFactoryOptions>,
40
41
  ): LengthFactory {
42
+ const { bbox = true, length = true, sampleSize = 10 } = options;
41
43
  const distanceIsNumber = typeof distance === 'number';
42
44
  let x = x1;
43
45
  let y = y1;
@@ -53,13 +55,18 @@ export function segmentCubicFactory(
53
55
  }
54
56
 
55
57
  // bad perf when size = 300
56
- const sampleSize = 30;
57
58
  for (let j = 0; j <= sampleSize; j += 1) {
58
59
  t = j / sampleSize;
59
60
 
60
61
  ({ x, y } = getPointAtCubicSegmentLength(x1, y1, c1x, c1y, c2x, c2y, x2, y2, t));
61
- POINTS = POINTS.concat({ x, y });
62
- LENGTH += distanceSquareRoot(cur, [x, y]);
62
+
63
+ if (bbox) {
64
+ POINTS.push({ x, y });
65
+ }
66
+
67
+ if (length) {
68
+ LENGTH += distanceSquareRoot(cur, [x, y]);
69
+ }
63
70
  cur = [x, y];
64
71
 
65
72
  if (distanceIsNumber && LENGTH >= distance && distance > prev[2]) {
@@ -81,12 +88,24 @@ export function segmentCubicFactory(
81
88
  length: LENGTH,
82
89
  point: POINT,
83
90
  min: {
84
- x: Math.min(...POINTS.map((n) => n.x)),
85
- y: Math.min(...POINTS.map((n) => n.y)),
91
+ x: Math.min.apply(
92
+ null,
93
+ POINTS.map((n) => n.x),
94
+ ),
95
+ y: Math.min.apply(
96
+ null,
97
+ POINTS.map((n) => n.y),
98
+ ),
86
99
  },
87
100
  max: {
88
- x: Math.max(...POINTS.map((n) => n.x)),
89
- y: Math.max(...POINTS.map((n) => n.y)),
101
+ x: Math.max.apply(
102
+ null,
103
+ POINTS.map((n) => n.x),
104
+ ),
105
+ y: Math.max.apply(
106
+ null,
107
+ POINTS.map((n) => n.y),
108
+ ),
90
109
  },
91
110
  };
92
111
  }
@@ -1,4 +1,4 @@
1
- import type { LengthFactory } from '../types';
1
+ import type { LengthFactory, PathLengthFactoryOptions } from '../types';
2
2
  import { distanceSquareRoot } from './distance-square-root';
3
3
 
4
4
  /**
@@ -35,7 +35,9 @@ export function segmentQuadFactory(
35
35
  x2: number,
36
36
  y2: number,
37
37
  distance: number,
38
+ options: Partial<PathLengthFactoryOptions>,
38
39
  ): LengthFactory {
40
+ const { bbox = true, length = true, sampleSize = 10 } = options;
39
41
  const distanceIsNumber = typeof distance === 'number';
40
42
  let x = x1;
41
43
  let y = y1;
@@ -50,13 +52,18 @@ export function segmentQuadFactory(
50
52
  POINT = { x, y };
51
53
  }
52
54
 
53
- const sampleSize = 30;
54
55
  for (let j = 0; j <= sampleSize; j += 1) {
55
56
  t = j / sampleSize;
56
57
 
57
58
  ({ x, y } = getPointAtQuadSegmentLength(x1, y1, qx, qy, x2, y2, t));
58
- POINTS = POINTS.concat({ x, y });
59
- LENGTH += distanceSquareRoot(cur, [x, y]);
59
+
60
+ if (bbox) {
61
+ POINTS.push({ x, y });
62
+ }
63
+
64
+ if (length) {
65
+ LENGTH += distanceSquareRoot(cur, [x, y]);
66
+ }
60
67
  cur = [x, y];
61
68
 
62
69
  if (distanceIsNumber && LENGTH >= distance && distance > prev[2]) {
@@ -79,12 +86,24 @@ export function segmentQuadFactory(
79
86
  length: LENGTH,
80
87
  point: POINT,
81
88
  min: {
82
- x: Math.min(...POINTS.map((n) => n.x)),
83
- y: Math.min(...POINTS.map((n) => n.y)),
89
+ x: Math.min.apply(
90
+ null,
91
+ POINTS.map((n) => n.x),
92
+ ),
93
+ y: Math.min.apply(
94
+ null,
95
+ POINTS.map((n) => n.y),
96
+ ),
84
97
  },
85
98
  max: {
86
- x: Math.max(...POINTS.map((n) => n.x)),
87
- y: Math.max(...POINTS.map((n) => n.y)),
99
+ x: Math.max.apply(
100
+ null,
101
+ POINTS.map((n) => n.x),
102
+ ),
103
+ y: Math.max.apply(
104
+ null,
105
+ POINTS.map((n) => n.y),
106
+ ),
88
107
  },
89
108
  };
90
109
  }