@antv/util 3.2.3 → 3.2.4

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 (71) hide show
  1. package/dist/util.min.js +1 -1
  2. package/esm/path/process/normalize-path.js +1 -6
  3. package/esm/path/process/normalize-path.js.map +1 -1
  4. package/esm/path/types.d.ts +5 -0
  5. package/esm/path/util/equalize-segments.js +1 -1
  6. package/esm/path/util/equalize-segments.js.map +1 -1
  7. package/esm/path/util/get-path-bbox-total-length.d.ts +2 -2
  8. package/esm/path/util/get-path-bbox-total-length.js +3 -2
  9. package/esm/path/util/get-path-bbox-total-length.js.map +1 -1
  10. package/esm/path/util/get-path-bbox.d.ts +2 -2
  11. package/esm/path/util/get-path-bbox.js +3 -2
  12. package/esm/path/util/get-path-bbox.js.map +1 -1
  13. package/esm/path/util/get-point-at-length.d.ts +2 -2
  14. package/esm/path/util/get-point-at-length.js +3 -2
  15. package/esm/path/util/get-point-at-length.js.map +1 -1
  16. package/esm/path/util/get-total-length.d.ts +2 -2
  17. package/esm/path/util/get-total-length.js +3 -2
  18. package/esm/path/util/get-total-length.js.map +1 -1
  19. package/esm/path/util/path-length-factory.d.ts +2 -2
  20. package/esm/path/util/path-length-factory.js +4 -4
  21. package/esm/path/util/path-length-factory.js.map +1 -1
  22. package/esm/path/util/segment-arc-factory.d.ts +4 -2
  23. package/esm/path/util/segment-arc-factory.js +11 -5
  24. package/esm/path/util/segment-arc-factory.js.map +1 -1
  25. package/esm/path/util/segment-cubic-factory.d.ts +2 -2
  26. package/esm/path/util/segment-cubic-factory.js +8 -4
  27. package/esm/path/util/segment-cubic-factory.js.map +1 -1
  28. package/esm/path/util/segment-quad-factory.d.ts +2 -2
  29. package/esm/path/util/segment-quad-factory.js +8 -4
  30. package/esm/path/util/segment-quad-factory.js.map +1 -1
  31. package/lib/path/process/normalize-path.js +1 -6
  32. package/lib/path/process/normalize-path.js.map +1 -1
  33. package/lib/path/types.d.ts +5 -0
  34. package/lib/path/util/equalize-segments.js +1 -1
  35. package/lib/path/util/equalize-segments.js.map +1 -1
  36. package/lib/path/util/get-path-bbox-total-length.d.ts +2 -2
  37. package/lib/path/util/get-path-bbox-total-length.js +3 -2
  38. package/lib/path/util/get-path-bbox-total-length.js.map +1 -1
  39. package/lib/path/util/get-path-bbox.d.ts +2 -2
  40. package/lib/path/util/get-path-bbox.js +3 -2
  41. package/lib/path/util/get-path-bbox.js.map +1 -1
  42. package/lib/path/util/get-point-at-length.d.ts +2 -2
  43. package/lib/path/util/get-point-at-length.js +3 -2
  44. package/lib/path/util/get-point-at-length.js.map +1 -1
  45. package/lib/path/util/get-total-length.d.ts +2 -2
  46. package/lib/path/util/get-total-length.js +3 -2
  47. package/lib/path/util/get-total-length.js.map +1 -1
  48. package/lib/path/util/path-length-factory.d.ts +2 -2
  49. package/lib/path/util/path-length-factory.js +4 -4
  50. package/lib/path/util/path-length-factory.js.map +1 -1
  51. package/lib/path/util/segment-arc-factory.d.ts +4 -2
  52. package/lib/path/util/segment-arc-factory.js +11 -5
  53. package/lib/path/util/segment-arc-factory.js.map +1 -1
  54. package/lib/path/util/segment-cubic-factory.d.ts +2 -2
  55. package/lib/path/util/segment-cubic-factory.js +8 -4
  56. package/lib/path/util/segment-cubic-factory.js.map +1 -1
  57. package/lib/path/util/segment-quad-factory.d.ts +2 -2
  58. package/lib/path/util/segment-quad-factory.js +8 -4
  59. package/lib/path/util/segment-quad-factory.js.map +1 -1
  60. package/package.json +2 -1
  61. package/src/path/process/normalize-path.ts +1 -7
  62. package/src/path/types.ts +5 -0
  63. package/src/path/util/equalize-segments.ts +1 -0
  64. package/src/path/util/get-path-bbox-total-length.ts +6 -3
  65. package/src/path/util/get-path-bbox.ts +3 -3
  66. package/src/path/util/get-point-at-length.ts +7 -3
  67. package/src/path/util/get-total-length.ts +3 -3
  68. package/src/path/util/path-length-factory.ts +9 -2
  69. package/src/path/util/segment-arc-factory.ts +14 -5
  70. package/src/path/util/segment-cubic-factory.ts +11 -4
  71. package/src/path/util/segment-quad-factory.ts +11 -4
@@ -1,10 +1,13 @@
1
- import type { PathArray, PathBBoxTotalLength } from '../types';
1
+ import type { PathArray, PathBBoxTotalLength, PathLengthFactoryOptions } from '../types';
2
2
  import { pathLengthFactory } from './path-length-factory';
3
3
 
4
4
  /**
5
5
  * Returns the bounding box of a shape.
6
6
  */
7
- export function getPathBBoxTotalLength(path: PathArray): PathBBoxTotalLength {
7
+ export function getPathBBoxTotalLength(
8
+ path: PathArray,
9
+ options?: Partial<PathLengthFactoryOptions>,
10
+ ): PathBBoxTotalLength {
8
11
  if (!path) {
9
12
  return {
10
13
  length: 0,
@@ -24,7 +27,7 @@ export function getPathBBoxTotalLength(path: PathArray): PathBBoxTotalLength {
24
27
  length,
25
28
  min: { x: xMin, y: yMin },
26
29
  max: { x: xMax, y: yMax },
27
- } = pathLengthFactory(path);
30
+ } = pathLengthFactory(path, undefined, { ...options, bbox: true, length: true });
28
31
 
29
32
  const width = xMax - xMin;
30
33
  const height = yMax - yMin;
@@ -1,10 +1,10 @@
1
- import type { PathArray, PathBBox } from '../types';
1
+ import type { PathArray, PathBBox, PathLengthFactoryOptions } from '../types';
2
2
  import { pathLengthFactory } from './path-length-factory';
3
3
 
4
4
  /**
5
5
  * Returns the bounding box of a shape.
6
6
  */
7
- export function getPathBBox(path: string | PathArray): PathBBox {
7
+ export function getPathBBox(path: string | PathArray, options?: Partial<PathLengthFactoryOptions>): PathBBox {
8
8
  if (!path) {
9
9
  return {
10
10
  x: 0,
@@ -22,7 +22,7 @@ export function getPathBBox(path: string | PathArray): PathBBox {
22
22
  const {
23
23
  min: { x: xMin, y: yMin },
24
24
  max: { x: xMax, y: yMax },
25
- } = pathLengthFactory(path);
25
+ } = pathLengthFactory(path, undefined, { ...options, length: false });
26
26
 
27
27
  const width = xMax - xMin;
28
28
  const height = yMax - yMin;
@@ -1,9 +1,13 @@
1
- import type { PathArray } from '../types';
1
+ import type { PathArray, PathLengthFactoryOptions } from '../types';
2
2
  import { pathLengthFactory } from './path-length-factory';
3
3
 
4
4
  /**
5
5
  * Returns [x,y] coordinates of a point at a given length of a shape.
6
6
  */
7
- export function getPointAtLength(pathInput: string | PathArray, distance: number) {
8
- return pathLengthFactory(pathInput, distance).point;
7
+ export function getPointAtLength(
8
+ pathInput: string | PathArray,
9
+ distance: number,
10
+ options?: Partial<PathLengthFactoryOptions>,
11
+ ) {
12
+ return pathLengthFactory(pathInput, distance, { ...options, bbox: false, length: true }).point;
9
13
  }
@@ -1,4 +1,4 @@
1
- import type { PathArray } from '../types';
1
+ import type { PathArray, PathLengthFactoryOptions } from '../types';
2
2
  import { pathLengthFactory } from './path-length-factory';
3
3
 
4
4
  /**
@@ -7,6 +7,6 @@ import { pathLengthFactory } from './path-length-factory';
7
7
  * The `normalizePath` version is lighter, faster, more efficient and more accurate
8
8
  * with paths that are not `curveArray`.
9
9
  */
10
- export function getTotalLength(pathInput: string | PathArray) {
11
- return pathLengthFactory(pathInput).length;
10
+ export function getTotalLength(pathInput: string | PathArray, options?: Partial<PathLengthFactoryOptions>) {
11
+ return pathLengthFactory(pathInput, undefined, { ...options, bbox: false, length: true }).length;
12
12
  }
@@ -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;
@@ -62,6 +66,7 @@ export function pathLengthFactory(pathInput: string | PathArray, distance?: numb
62
66
  data[7],
63
67
  data[8],
64
68
  (distance || 0) - LENGTH,
69
+ options || {},
65
70
  ));
66
71
  } else if (pathCommand === 'C') {
67
72
  ({ length, min, max, point } = segmentCubicFactory(
@@ -74,6 +79,7 @@ export function pathLengthFactory(pathInput: string | PathArray, distance?: numb
74
79
  data[6],
75
80
  data[7],
76
81
  (distance || 0) - LENGTH,
82
+ options || {},
77
83
  ));
78
84
  } else if (pathCommand === 'Q') {
79
85
  ({ length, min, max, point } = segmentQuadFactory(
@@ -84,6 +90,7 @@ export function pathLengthFactory(pathInput: string | PathArray, distance?: numb
84
90
  data[4],
85
91
  data[5],
86
92
  (distance || 0) - LENGTH,
93
+ options || {},
87
94
  ));
88
95
  } else if (pathCommand === 'Z') {
89
96
  data = [x, y, mx, my];
@@ -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 = 10 } = 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]) {
@@ -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]) {
@@ -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]) {