@antv/util 2.0.15 → 3.0.0-beta.1
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.
- package/LICENSE +21 -0
- package/README.md +15 -241
- package/dist/util.min.js +1 -0
- package/esm/color/arr2rgb.d.ts +12 -0
- package/esm/color/arr2rgb.js +18 -0
- package/esm/color/arr2rgb.js.map +1 -0
- package/esm/color/gradient.d.ts +6 -0
- package/esm/color/gradient.js +45 -0
- package/esm/color/gradient.js.map +1 -0
- package/esm/color/index.d.ts +4 -0
- package/esm/color/index.js +6 -0
- package/esm/color/index.js.map +1 -0
- package/esm/color/rgb2arr.d.ts +6 -0
- package/esm/color/rgb2arr.js +9 -0
- package/esm/color/rgb2arr.js.map +1 -0
- package/esm/color/tocssgradient.d.ts +1 -0
- package/esm/color/tocssgradient.js +37 -0
- package/esm/color/tocssgradient.js.map +1 -0
- package/esm/color/torgb.d.ts +6 -0
- package/esm/color/torgb.js +36 -0
- package/esm/color/torgb.js.map +1 -0
- package/esm/helper/index.d.ts +2 -0
- package/esm/helper/index.js +3 -0
- package/esm/helper/index.js.map +1 -0
- package/esm/helper/mod.d.ts +1 -0
- package/esm/helper/mod.js +4 -0
- package/esm/helper/mod.js.map +1 -0
- package/esm/helper/to-radian.d.ts +1 -0
- package/esm/helper/to-radian.js +5 -0
- package/esm/helper/to-radian.js.map +1 -0
- package/esm/index.d.ts +3 -0
- package/esm/index.js +4 -0
- package/esm/index.js.map +1 -0
- package/esm/matrix/index.d.ts +27 -0
- package/esm/matrix/index.js +97 -0
- package/esm/matrix/index.js.map +1 -0
- package/esm/path/catmull-rom-2-bezier.d.ts +10 -0
- package/esm/path/catmull-rom-2-bezier.js +115 -0
- package/esm/path/catmull-rom-2-bezier.js.map +1 -0
- package/esm/path/fill-path-by-diff.d.ts +5 -0
- package/esm/path/fill-path-by-diff.js +110 -0
- package/esm/path/fill-path-by-diff.js.map +1 -0
- package/esm/path/fill-path.d.ts +1 -0
- package/esm/path/fill-path.js +129 -0
- package/esm/path/fill-path.js.map +1 -0
- package/esm/path/format-path.d.ts +1 -0
- package/esm/path/format-path.js +139 -0
- package/esm/path/format-path.js.map +1 -0
- package/esm/path/get-arc-params.d.ts +17 -0
- package/esm/path/get-arc-params.js +90 -0
- package/esm/path/get-arc-params.js.map +1 -0
- package/esm/path/get-line-intersect.d.ts +14 -0
- package/esm/path/get-line-intersect.js +33 -0
- package/esm/path/get-line-intersect.js.map +1 -0
- package/esm/path/index.d.ts +16 -0
- package/esm/path/index.js +17 -0
- package/esm/path/index.js.map +1 -0
- package/esm/path/is-polygons-intersect.d.ts +1 -0
- package/esm/path/is-polygons-intersect.js +81 -0
- package/esm/path/is-polygons-intersect.js.map +1 -0
- package/esm/path/parse-path-array.d.ts +1 -0
- package/esm/path/parse-path-array.js +5 -0
- package/esm/path/parse-path-array.js.map +1 -0
- package/esm/path/parse-path-string.d.ts +1 -0
- package/esm/path/parse-path-string.js +57 -0
- package/esm/path/parse-path-string.js.map +1 -0
- package/esm/path/parse-path.d.ts +1 -0
- package/esm/path/parse-path.js +33 -0
- package/esm/path/parse-path.js.map +1 -0
- package/esm/path/path-2-absolute.d.ts +1 -0
- package/esm/path/path-2-absolute.js +124 -0
- package/esm/path/path-2-absolute.js.map +1 -0
- package/esm/path/path-2-curve.d.ts +2 -0
- package/esm/path/path-2-curve.js +63 -0
- package/esm/path/path-2-curve.js.map +1 -0
- package/esm/path/path-2-segments.d.ts +1 -0
- package/esm/path/path-2-segments.js +138 -0
- package/esm/path/path-2-segments.js.map +1 -0
- package/esm/path/path-intersection.d.ts +9 -0
- package/esm/path/path-intersection.js +366 -0
- package/esm/path/path-intersection.js.map +1 -0
- package/esm/path/point-in-polygon.d.ts +5 -0
- package/esm/path/point-in-polygon.js +49 -0
- package/esm/path/point-in-polygon.js.map +1 -0
- package/esm/path/process/arc-2-cubic.d.ts +1 -0
- package/esm/path/process/arc-2-cubic.js +140 -0
- package/esm/path/process/arc-2-cubic.js.map +1 -0
- package/esm/path/process/line-2-cubic.d.ts +1 -0
- package/esm/path/process/line-2-cubic.js +35 -0
- package/esm/path/process/line-2-cubic.js.map +1 -0
- package/esm/path/process/quad-2-cubic.d.ts +1 -0
- package/esm/path/process/quad-2-cubic.js +13 -0
- package/esm/path/process/quad-2-cubic.js.map +1 -0
- package/esm/path/process/segment-2-cubic.d.ts +2 -0
- package/esm/path/process/segment-2-cubic.js +37 -0
- package/esm/path/process/segment-2-cubic.js.map +1 -0
- package/esm/path/rect-path.d.ts +4 -0
- package/esm/path/rect-path.js +19 -0
- package/esm/path/rect-path.js.map +1 -0
- package/esm/path/types.d.ts +25 -0
- package/esm/path/types.js +2 -0
- package/esm/path/types.js.map +1 -0
- package/lib/color/arr2rgb.d.ts +12 -0
- package/lib/color/arr2rgb.js +23 -0
- package/lib/color/arr2rgb.js.map +1 -0
- package/lib/color/gradient.d.ts +6 -0
- package/lib/color/gradient.js +49 -0
- package/lib/color/gradient.js.map +1 -0
- package/lib/color/index.d.ts +4 -0
- package/lib/color/index.js +13 -0
- package/lib/color/index.js.map +1 -0
- package/lib/color/rgb2arr.d.ts +6 -0
- package/lib/color/rgb2arr.js +13 -0
- package/lib/color/rgb2arr.js.map +1 -0
- package/lib/color/tocssgradient.d.ts +1 -0
- package/lib/color/tocssgradient.js +41 -0
- package/lib/color/tocssgradient.js.map +1 -0
- package/lib/color/torgb.d.ts +6 -0
- package/lib/color/torgb.js +40 -0
- package/lib/color/torgb.js.map +1 -0
- package/lib/helper/index.d.ts +2 -0
- package/lib/helper/index.js +8 -0
- package/lib/helper/index.js.map +1 -0
- package/lib/helper/mod.d.ts +1 -0
- package/lib/helper/mod.js +8 -0
- package/lib/helper/mod.js.map +1 -0
- package/lib/helper/to-radian.d.ts +1 -0
- package/lib/helper/to-radian.js +9 -0
- package/lib/helper/to-radian.js.map +1 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +16 -0
- package/lib/index.js.map +1 -0
- package/lib/matrix/index.d.ts +27 -0
- package/lib/matrix/index.js +104 -0
- package/lib/matrix/index.js.map +1 -0
- package/lib/path/catmull-rom-2-bezier.d.ts +10 -0
- package/lib/path/catmull-rom-2-bezier.js +119 -0
- package/lib/path/catmull-rom-2-bezier.js.map +1 -0
- package/lib/path/fill-path-by-diff.d.ts +5 -0
- package/lib/path/fill-path-by-diff.js +117 -0
- package/lib/path/fill-path-by-diff.js.map +1 -0
- package/lib/path/fill-path.d.ts +1 -0
- package/lib/path/fill-path.js +133 -0
- package/lib/path/fill-path.js.map +1 -0
- package/lib/path/format-path.d.ts +1 -0
- package/lib/path/format-path.js +143 -0
- package/lib/path/format-path.js.map +1 -0
- package/lib/path/get-arc-params.d.ts +17 -0
- package/lib/path/get-arc-params.js +95 -0
- package/lib/path/get-arc-params.js.map +1 -0
- package/lib/path/get-line-intersect.d.ts +14 -0
- package/lib/path/get-line-intersect.js +37 -0
- package/lib/path/get-line-intersect.js.map +1 -0
- package/lib/path/index.d.ts +16 -0
- package/lib/path/index.js +36 -0
- package/lib/path/index.js.map +1 -0
- package/lib/path/is-polygons-intersect.d.ts +1 -0
- package/lib/path/is-polygons-intersect.js +85 -0
- package/lib/path/is-polygons-intersect.js.map +1 -0
- package/lib/path/parse-path-array.d.ts +1 -0
- package/lib/path/parse-path-array.js +9 -0
- package/lib/path/parse-path-array.js.map +1 -0
- package/lib/path/parse-path-string.d.ts +1 -0
- package/lib/path/parse-path-string.js +61 -0
- package/lib/path/parse-path-string.js.map +1 -0
- package/lib/path/parse-path.d.ts +1 -0
- package/lib/path/parse-path.js +37 -0
- package/lib/path/parse-path.js.map +1 -0
- package/lib/path/path-2-absolute.d.ts +1 -0
- package/lib/path/path-2-absolute.js +128 -0
- package/lib/path/path-2-absolute.js.map +1 -0
- package/lib/path/path-2-curve.d.ts +2 -0
- package/lib/path/path-2-curve.js +67 -0
- package/lib/path/path-2-curve.js.map +1 -0
- package/lib/path/path-2-segments.d.ts +1 -0
- package/lib/path/path-2-segments.js +142 -0
- package/lib/path/path-2-segments.js.map +1 -0
- package/lib/path/path-intersection.d.ts +9 -0
- package/lib/path/path-intersection.js +370 -0
- package/lib/path/path-intersection.js.map +1 -0
- package/lib/path/point-in-polygon.d.ts +5 -0
- package/lib/path/point-in-polygon.js +53 -0
- package/lib/path/point-in-polygon.js.map +1 -0
- package/lib/path/process/arc-2-cubic.d.ts +1 -0
- package/lib/path/process/arc-2-cubic.js +144 -0
- package/lib/path/process/arc-2-cubic.js.map +1 -0
- package/lib/path/process/line-2-cubic.d.ts +1 -0
- package/lib/path/process/line-2-cubic.js +39 -0
- package/lib/path/process/line-2-cubic.js.map +1 -0
- package/lib/path/process/quad-2-cubic.d.ts +1 -0
- package/lib/path/process/quad-2-cubic.js +17 -0
- package/lib/path/process/quad-2-cubic.js.map +1 -0
- package/lib/path/process/segment-2-cubic.d.ts +2 -0
- package/lib/path/process/segment-2-cubic.js +41 -0
- package/lib/path/process/segment-2-cubic.js.map +1 -0
- package/lib/path/rect-path.d.ts +4 -0
- package/lib/path/rect-path.js +23 -0
- package/lib/path/rect-path.js.map +1 -0
- package/lib/path/types.d.ts +25 -0
- package/lib/path/types.js +3 -0
- package/lib/path/types.js.map +1 -0
- package/package.json +77 -36
- package/src/color/arr2rgb.ts +19 -0
- package/src/color/gradient.ts +55 -0
- package/src/color/index.ts +5 -0
- package/src/color/rgb2arr.ts +8 -0
- package/src/color/tocssgradient.ts +43 -0
- package/src/color/torgb.ts +45 -0
- package/src/helper/index.ts +2 -0
- package/src/helper/mod.ts +3 -0
- package/src/helper/to-radian.ts +5 -0
- package/src/index.ts +3 -130
- package/src/matrix/index.ts +108 -0
- package/src/path/catmull-rom-2-bezier.ts +141 -0
- package/src/path/fill-path-by-diff.ts +116 -0
- package/src/path/fill-path.ts +129 -0
- package/src/path/format-path.ts +142 -0
- package/src/path/get-arc-params.ts +102 -0
- package/src/path/get-line-intersect.ts +46 -0
- package/src/path/index.ts +16 -0
- package/src/path/is-polygons-intersect.ts +95 -0
- package/src/path/parse-path-array.ts +5 -0
- package/src/path/parse-path-string.ts +63 -0
- package/src/path/parse-path.ts +34 -0
- package/src/path/path-2-absolute.ts +132 -0
- package/src/path/path-2-curve.ts +69 -0
- package/src/path/path-2-segments.ts +142 -0
- package/src/path/path-intersection.ts +419 -0
- package/src/path/point-in-polygon.ts +56 -0
- package/src/path/process/arc-2-cubic.ts +221 -0
- package/src/path/process/line-2-cubic.ts +37 -0
- package/src/path/process/quad-2-cubic.ts +12 -0
- package/src/path/process/segment-2-cubic.ts +44 -0
- package/src/path/rect-path.ts +22 -0
- package/src/path/types.ts +25 -0
- package/src/augment.ts +0 -16
- package/src/cache.ts +0 -31
- package/src/clamp.ts +0 -10
- package/src/clear-animation-frame.ts +0 -11
- package/src/clone.ts +0 -31
- package/src/contains.ts +0 -10
- package/src/debounce.ts +0 -22
- package/src/deep-mix.ts +0 -39
- package/src/difference.ts +0 -17
- package/src/each.ts +0 -28
- package/src/ends-with.ts +0 -10
- package/src/every.ts +0 -14
- package/src/extend.ts +0 -36
- package/src/filter.ts +0 -18
- package/src/find-index.ts +0 -12
- package/src/find.ts +0 -29
- package/src/first-value.ts +0 -21
- package/src/fixed-base.ts +0 -14
- package/src/flatten-deep.ts +0 -24
- package/src/flatten.ts +0 -23
- package/src/for-in.ts +0 -3
- package/src/get-ellipsis-text.ts +0 -75
- package/src/get-range.ts +0 -35
- package/src/get-type.ts +0 -7
- package/src/get-wrap-behavior.ts +0 -13
- package/src/get.ts +0 -18
- package/src/group-by.ts +0 -35
- package/src/group-to-map.ts +0 -30
- package/src/group.ts +0 -14
- package/src/has-key.ts +0 -2
- package/src/has-value.ts +0 -4
- package/src/has.ts +0 -1
- package/src/head.ts +0 -8
- package/src/identity.ts +0 -1
- package/src/index-of.ts +0 -22
- package/src/is-arguments.ts +0 -13
- package/src/is-array-like.ts +0 -11
- package/src/is-array.ts +0 -7
- package/src/is-boolean.ts +0 -13
- package/src/is-date.ts +0 -7
- package/src/is-decimal.ts +0 -7
- package/src/is-element.ts +0 -8
- package/src/is-empty.ts +0 -39
- package/src/is-equal-with.ts +0 -31
- package/src/is-equal.ts +0 -46
- package/src/is-error.ts +0 -13
- package/src/is-even.ts +0 -7
- package/src/is-finite.ts +0 -11
- package/src/is-function.ts +0 -10
- package/src/is-integer.ts +0 -7
- package/src/is-match.ts +0 -17
- package/src/is-negative.ts +0 -7
- package/src/is-nil.ts +0 -10
- package/src/is-null.ts +0 -5
- package/src/is-number-equal.ts +0 -5
- package/src/is-number.ts +0 -10
- package/src/is-object-like.ts +0 -11
- package/src/is-object.ts +0 -10
- package/src/is-odd.ts +0 -7
- package/src/is-plain-object.ts +0 -24
- package/src/is-positive.ts +0 -7
- package/src/is-prototype.ts +0 -9
- package/src/is-reg-exp.ts +0 -7
- package/src/is-segment-equal.ts +0 -15
- package/src/is-string.ts +0 -5
- package/src/is-type.ts +0 -5
- package/src/is-undefined.ts +0 -5
- package/src/keys.ts +0 -14
- package/src/last.ts +0 -9
- package/src/lower-case.ts +0 -7
- package/src/lower-first.ts +0 -8
- package/src/map-values.ts +0 -19
- package/src/map.ts +0 -17
- package/src/max-by.ts +0 -37
- package/src/max.ts +0 -28
- package/src/measure-text-width.ts +0 -25
- package/src/memoize.ts +0 -31
- package/src/merge.ts +0 -12
- package/src/min-by.ts +0 -38
- package/src/min.ts +0 -28
- package/src/mix.ts +0 -15
- package/src/mod.ts +0 -5
- package/src/noop.ts +0 -1
- package/src/number2color.ts +0 -17
- package/src/omit.ts +0 -15
- package/src/parse-radius.ts +0 -45
- package/src/pick.ts +0 -32
- package/src/pull-at.ts +0 -23
- package/src/pull.ts +0 -16
- package/src/reduce.ts +0 -17
- package/src/remove.ts +0 -30
- package/src/request-animation-frame.ts +0 -13
- package/src/set.ts +0 -29
- package/src/size.ts +0 -12
- package/src/some.ts +0 -14
- package/src/sort-by.ts +0 -42
- package/src/starts-with.ts +0 -10
- package/src/substitute.ts +0 -17
- package/src/throttle.ts +0 -48
- package/src/to-array.ts +0 -5
- package/src/to-degree.ts +0 -7
- package/src/to-integer.ts +0 -1
- package/src/to-radian.ts +0 -7
- package/src/to-string.ts +0 -6
- package/src/types/index.ts +0 -3
- package/src/union.ts +0 -7
- package/src/uniq.ts +0 -15
- package/src/unique-id.ts +0 -11
- package/src/upper-case.ts +0 -7
- package/src/upper-first.ts +0 -8
- package/src/values-of-key.ts +0 -29
- package/src/values.ts +0 -15
- package/src/wrap-behavior.ts +0 -20
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { parsePathString } from './parse-path-string';
|
|
2
|
+
|
|
3
|
+
const REGEX_MD = /[a-z]/;
|
|
4
|
+
|
|
5
|
+
function toSymmetry(p, c) {
|
|
6
|
+
// 点对称
|
|
7
|
+
return [c[0] + (c[0] - p[0]), c[1] + (c[1] - p[1])];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function path2Absolute(pathString: string) {
|
|
11
|
+
const pathArray = parsePathString(pathString);
|
|
12
|
+
|
|
13
|
+
if (!pathArray || !pathArray.length) {
|
|
14
|
+
return [['M', 0, 0]];
|
|
15
|
+
}
|
|
16
|
+
let needProcess = false; // 如果存在小写的命令或者 V,H,T,S 则需要处理
|
|
17
|
+
for (let i = 0; i < pathArray.length; i++) {
|
|
18
|
+
const cmd = pathArray[i][0];
|
|
19
|
+
// 如果存在相对位置的命令,则中断返回
|
|
20
|
+
if (REGEX_MD.test(cmd) || ['V', 'H', 'T', 'S'].indexOf(cmd) >= 0) {
|
|
21
|
+
needProcess = true;
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// 如果不存在相对命令,则直接返回
|
|
26
|
+
// 如果在业务上都写绝对路径,这种方式最快,仅做了一次检测
|
|
27
|
+
if (!needProcess) {
|
|
28
|
+
return pathArray;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const res = [];
|
|
32
|
+
let x = 0;
|
|
33
|
+
let y = 0;
|
|
34
|
+
let mx = 0;
|
|
35
|
+
let my = 0;
|
|
36
|
+
let start = 0;
|
|
37
|
+
const first = pathArray[0];
|
|
38
|
+
if (first[0] === 'M' || first[0] === 'm') {
|
|
39
|
+
x = +first[1];
|
|
40
|
+
y = +first[2];
|
|
41
|
+
mx = x;
|
|
42
|
+
my = y;
|
|
43
|
+
start++;
|
|
44
|
+
res[0] = ['M', x, y];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
for (let i = start, ii = pathArray.length; i < ii; i++) {
|
|
48
|
+
const pa = pathArray[i];
|
|
49
|
+
const preParams = res[i - 1]; // 取前一个已经处理后的节点,否则会出现问题
|
|
50
|
+
let r = [];
|
|
51
|
+
const cmd = pa[0];
|
|
52
|
+
const upCmd = cmd.toUpperCase();
|
|
53
|
+
if (cmd !== upCmd) {
|
|
54
|
+
r[0] = upCmd;
|
|
55
|
+
switch (upCmd) {
|
|
56
|
+
case 'A':
|
|
57
|
+
r[1] = pa[1];
|
|
58
|
+
r[2] = pa[2];
|
|
59
|
+
r[3] = pa[3];
|
|
60
|
+
r[4] = pa[4];
|
|
61
|
+
r[5] = pa[5];
|
|
62
|
+
r[6] = +pa[6] + x;
|
|
63
|
+
r[7] = +pa[7] + y;
|
|
64
|
+
break;
|
|
65
|
+
case 'V':
|
|
66
|
+
r[1] = +pa[1] + y;
|
|
67
|
+
break;
|
|
68
|
+
case 'H':
|
|
69
|
+
r[1] = +pa[1] + x;
|
|
70
|
+
break;
|
|
71
|
+
case 'M':
|
|
72
|
+
mx = +pa[1] + x;
|
|
73
|
+
my = +pa[2] + y;
|
|
74
|
+
r[1] = mx;
|
|
75
|
+
r[2] = my;
|
|
76
|
+
break; // for lint
|
|
77
|
+
default:
|
|
78
|
+
for (let j = 1, jj = pa.length; j < jj; j++) {
|
|
79
|
+
r[j] = +pa[j] + (j % 2 ? x : y);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
// 如果本来已经大写,则不处理
|
|
84
|
+
r = pathArray[i];
|
|
85
|
+
}
|
|
86
|
+
// 需要在外面统一做,同时处理 V,H,S,T 等特殊指令
|
|
87
|
+
switch (upCmd) {
|
|
88
|
+
case 'Z':
|
|
89
|
+
x = +mx;
|
|
90
|
+
y = +my;
|
|
91
|
+
break;
|
|
92
|
+
case 'H':
|
|
93
|
+
x = r[1];
|
|
94
|
+
r = ['L', x, y];
|
|
95
|
+
break;
|
|
96
|
+
case 'V':
|
|
97
|
+
y = r[1];
|
|
98
|
+
r = ['L', x, y];
|
|
99
|
+
break;
|
|
100
|
+
case 'T':
|
|
101
|
+
x = r[1];
|
|
102
|
+
y = r[2];
|
|
103
|
+
// 以 x, y 为中心的,上一个控制点的对称点
|
|
104
|
+
// 需要假设上一个节点的命令为 Q
|
|
105
|
+
const symetricT = toSymmetry([preParams[1], preParams[2]], [preParams[3], preParams[4]]);
|
|
106
|
+
r = ['Q', symetricT[0], symetricT[1], x, y];
|
|
107
|
+
break;
|
|
108
|
+
case 'S':
|
|
109
|
+
x = r[r.length - 2];
|
|
110
|
+
y = r[r.length - 1];
|
|
111
|
+
// 以 x,y 为中心,取上一个控制点,
|
|
112
|
+
// 需要假设上一个线段为 C 或者 S
|
|
113
|
+
const length = preParams.length;
|
|
114
|
+
const symetricS = toSymmetry(
|
|
115
|
+
[preParams[length - 4], preParams[length - 3]],
|
|
116
|
+
[preParams[length - 2], preParams[length - 1]],
|
|
117
|
+
);
|
|
118
|
+
r = ['C', symetricS[0], symetricS[1], r[1], r[2], x, y];
|
|
119
|
+
break;
|
|
120
|
+
case 'M':
|
|
121
|
+
mx = r[r.length - 2];
|
|
122
|
+
my = r[r.length - 1];
|
|
123
|
+
break; // for lint
|
|
124
|
+
default:
|
|
125
|
+
x = r[r.length - 2];
|
|
126
|
+
y = r[r.length - 1];
|
|
127
|
+
}
|
|
128
|
+
res.push(r);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return res;
|
|
132
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { path2Absolute } from './path-2-absolute';
|
|
2
|
+
import { segmentToCubic } from './process/segment-2-cubic';
|
|
3
|
+
import type { PathCommand, ProcessParams } from './types';
|
|
4
|
+
|
|
5
|
+
export function path2Curve(path: PathCommand[] | string, needZCommandIndexes = false) {
|
|
6
|
+
const pathArray = path2Absolute(path as string) as PathCommand[];
|
|
7
|
+
|
|
8
|
+
const params: ProcessParams = {
|
|
9
|
+
x1: 0,
|
|
10
|
+
y1: 0,
|
|
11
|
+
x2: 0,
|
|
12
|
+
y2: 0,
|
|
13
|
+
x: 0,
|
|
14
|
+
y: 0,
|
|
15
|
+
qx: null,
|
|
16
|
+
qy: null,
|
|
17
|
+
};
|
|
18
|
+
const allPathCommands = [];
|
|
19
|
+
let pathCommand = '';
|
|
20
|
+
let ii = pathArray.length;
|
|
21
|
+
let segment: PathCommand;
|
|
22
|
+
let seglen: number;
|
|
23
|
+
const zCommandIndexes: number[] = [];
|
|
24
|
+
|
|
25
|
+
for (let i = 0; i < ii; i += 1) {
|
|
26
|
+
if (pathArray[i]) [pathCommand] = pathArray[i];
|
|
27
|
+
|
|
28
|
+
allPathCommands[i] = pathCommand;
|
|
29
|
+
pathArray[i] = segmentToCubic(pathArray[i], params);
|
|
30
|
+
|
|
31
|
+
fixArc(pathArray, allPathCommands, i);
|
|
32
|
+
ii = pathArray.length; // solves curveArrays ending in Z
|
|
33
|
+
|
|
34
|
+
// keep Z command account for lineJoin
|
|
35
|
+
// @see https://github.com/antvis/util/issues/68
|
|
36
|
+
if (pathCommand === 'Z') {
|
|
37
|
+
zCommandIndexes.push(i);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
segment = pathArray[i];
|
|
41
|
+
seglen = segment.length;
|
|
42
|
+
|
|
43
|
+
params.x1 = +segment[seglen - 2];
|
|
44
|
+
params.y1 = +segment[seglen - 1];
|
|
45
|
+
params.x2 = +segment[seglen - 4] || params.x1;
|
|
46
|
+
params.y2 = +segment[seglen - 3] || params.y1;
|
|
47
|
+
}
|
|
48
|
+
if (needZCommandIndexes) {
|
|
49
|
+
return [pathArray, zCommandIndexes];
|
|
50
|
+
} else {
|
|
51
|
+
return pathArray;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function fixArc(pathArray: PathCommand[], allPathCommands: string[], i: number) {
|
|
56
|
+
if (pathArray[i].length > 7) {
|
|
57
|
+
pathArray[i].shift();
|
|
58
|
+
const pi = pathArray[i];
|
|
59
|
+
// const ni = i + 1;
|
|
60
|
+
let ni = i;
|
|
61
|
+
while (pi.length) {
|
|
62
|
+
// if created multiple C:s, their original seg is saved
|
|
63
|
+
allPathCommands[i] = 'A';
|
|
64
|
+
// @ts-ignore
|
|
65
|
+
pathArray.splice((ni += 1), 0, ['C'].concat(pi.splice(0, 6)));
|
|
66
|
+
}
|
|
67
|
+
pathArray.splice(i, 1);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { getArcParams, isSamePoint } from './get-arc-params';
|
|
2
|
+
import { parsePath } from './parse-path';
|
|
3
|
+
|
|
4
|
+
// 点对称
|
|
5
|
+
function toSymmetry(point, center) {
|
|
6
|
+
return [center[0] + (center[0] - point[0]), center[1] + (center[1] - point[1])];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function path2Segments(path) {
|
|
10
|
+
path = parsePath(path);
|
|
11
|
+
const segments = [];
|
|
12
|
+
let currentPoint = null; // 当前图形
|
|
13
|
+
let nextParams = null; // 下一节点的 path 参数
|
|
14
|
+
let startMovePoint = null; // 开始 M 的点,可能会有多个
|
|
15
|
+
let lastStartMovePointIndex = 0; // 最近一个开始点 M 的索引
|
|
16
|
+
const count = path.length;
|
|
17
|
+
for (let i = 0; i < count; i++) {
|
|
18
|
+
const params = path[i];
|
|
19
|
+
nextParams = path[i + 1];
|
|
20
|
+
const command = params[0];
|
|
21
|
+
// 数学定义上的参数,便于后面的计算
|
|
22
|
+
const segment = {
|
|
23
|
+
command,
|
|
24
|
+
prePoint: currentPoint,
|
|
25
|
+
params,
|
|
26
|
+
startTangent: null,
|
|
27
|
+
endTangent: null,
|
|
28
|
+
};
|
|
29
|
+
switch (command) {
|
|
30
|
+
case 'M':
|
|
31
|
+
startMovePoint = [params[1], params[2]];
|
|
32
|
+
lastStartMovePointIndex = i;
|
|
33
|
+
break;
|
|
34
|
+
case 'A':
|
|
35
|
+
const arcParams = getArcParams(currentPoint, params);
|
|
36
|
+
segment['arcParams'] = arcParams;
|
|
37
|
+
break;
|
|
38
|
+
default:
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
if (command === 'Z') {
|
|
42
|
+
// 有了 Z 后,当前节点从开始 M 的点开始
|
|
43
|
+
currentPoint = startMovePoint;
|
|
44
|
+
// 如果当前点的命令为 Z,相当于当前点为最近一个 M 点,则下一个点直接指向最近一个 M 点的下一个点
|
|
45
|
+
nextParams = path[lastStartMovePointIndex + 1];
|
|
46
|
+
} else {
|
|
47
|
+
const len = params.length;
|
|
48
|
+
currentPoint = [params[len - 2], params[len - 1]];
|
|
49
|
+
}
|
|
50
|
+
if (nextParams && nextParams[0] === 'Z') {
|
|
51
|
+
// 如果下一个点的命令为 Z,则下一个点直接指向最近一个 M 点
|
|
52
|
+
nextParams = path[lastStartMovePointIndex];
|
|
53
|
+
if (segments[lastStartMovePointIndex]) {
|
|
54
|
+
// 如果下一个点的命令为 Z,则最近一个 M 点的前一个点为当前点
|
|
55
|
+
segments[lastStartMovePointIndex].prePoint = currentPoint;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
segment['currentPoint'] = currentPoint;
|
|
59
|
+
// 如果当前点与最近一个 M 点相同,则最近一个 M 点的前一个点为当前点的前一个点
|
|
60
|
+
if (
|
|
61
|
+
segments[lastStartMovePointIndex] &&
|
|
62
|
+
isSamePoint(currentPoint, segments[lastStartMovePointIndex].currentPoint)
|
|
63
|
+
) {
|
|
64
|
+
segments[lastStartMovePointIndex].prePoint = segment.prePoint;
|
|
65
|
+
}
|
|
66
|
+
const nextPoint = nextParams ? [nextParams[nextParams.length - 2], nextParams[nextParams.length - 1]] : null;
|
|
67
|
+
segment['nextPoint'] = nextPoint;
|
|
68
|
+
// Add startTangent and endTangent
|
|
69
|
+
const { prePoint } = segment;
|
|
70
|
+
if (['L', 'H', 'V'].includes(command)) {
|
|
71
|
+
segment.startTangent = [prePoint[0] - currentPoint[0], prePoint[1] - currentPoint[1]];
|
|
72
|
+
segment.endTangent = [currentPoint[0] - prePoint[0], currentPoint[1] - prePoint[1]];
|
|
73
|
+
} else if (command === 'Q') {
|
|
74
|
+
// 二次贝塞尔曲线只有一个控制点
|
|
75
|
+
const cp = [params[1], params[2]];
|
|
76
|
+
// 二次贝塞尔曲线的终点为 currentPoint
|
|
77
|
+
segment.startTangent = [prePoint[0] - cp[0], prePoint[1] - cp[1]];
|
|
78
|
+
segment.endTangent = [currentPoint[0] - cp[0], currentPoint[1] - cp[1]];
|
|
79
|
+
} else if (command === 'T') {
|
|
80
|
+
const preSegment = segments[i - 1];
|
|
81
|
+
const cp = toSymmetry(preSegment.currentPoint, prePoint);
|
|
82
|
+
if (preSegment.command === 'Q') {
|
|
83
|
+
segment.command = 'Q';
|
|
84
|
+
segment.startTangent = [prePoint[0] - cp[0], prePoint[1] - cp[1]];
|
|
85
|
+
segment.endTangent = [currentPoint[0] - cp[0], currentPoint[1] - cp[1]];
|
|
86
|
+
} else {
|
|
87
|
+
segment.command = 'TL';
|
|
88
|
+
segment.startTangent = [prePoint[0] - currentPoint[0], prePoint[1] - currentPoint[1]];
|
|
89
|
+
segment.endTangent = [currentPoint[0] - prePoint[0], currentPoint[1] - prePoint[1]];
|
|
90
|
+
}
|
|
91
|
+
} else if (command === 'C') {
|
|
92
|
+
// 三次贝塞尔曲线有两个控制点
|
|
93
|
+
const cp1 = [params[1], params[2]];
|
|
94
|
+
const cp2 = [params[3], params[4]];
|
|
95
|
+
segment.startTangent = [prePoint[0] - cp1[0], prePoint[1] - cp1[1]];
|
|
96
|
+
segment.endTangent = [currentPoint[0] - cp2[0], currentPoint[1] - cp2[1]];
|
|
97
|
+
|
|
98
|
+
// horizontal line, eg. ['C', 100, 100, 100, 100, 200, 200]
|
|
99
|
+
if (segment.startTangent[0] === 0 && segment.startTangent[1] === 0) {
|
|
100
|
+
segment.startTangent = [cp1[0] - cp2[0], cp1[1] - cp2[1]];
|
|
101
|
+
}
|
|
102
|
+
if (segment.endTangent[0] === 0 && segment.endTangent[1] === 0) {
|
|
103
|
+
segment.endTangent = [cp2[0] - cp1[0], cp2[1] - cp1[1]];
|
|
104
|
+
}
|
|
105
|
+
} else if (command === 'S') {
|
|
106
|
+
const preSegment = segments[i - 1];
|
|
107
|
+
const cp1 = toSymmetry(preSegment.currentPoint, prePoint);
|
|
108
|
+
const cp2 = [params[1], params[2]];
|
|
109
|
+
if (preSegment.command === 'C') {
|
|
110
|
+
segment.command = 'C'; // 将 S 命令变换为 C 命令
|
|
111
|
+
segment.startTangent = [prePoint[0] - cp1[0], prePoint[1] - cp1[1]];
|
|
112
|
+
segment.endTangent = [currentPoint[0] - cp2[0], currentPoint[1] - cp2[1]];
|
|
113
|
+
} else {
|
|
114
|
+
segment.command = 'SQ'; // 将 S 命令变换为 SQ 命令
|
|
115
|
+
segment.startTangent = [prePoint[0] - cp2[0], prePoint[1] - cp2[1]];
|
|
116
|
+
segment.endTangent = [currentPoint[0] - cp2[0], currentPoint[1] - cp2[1]];
|
|
117
|
+
}
|
|
118
|
+
} else if (command === 'A') {
|
|
119
|
+
let d = 0.001;
|
|
120
|
+
const {
|
|
121
|
+
cx = 0,
|
|
122
|
+
cy = 0,
|
|
123
|
+
rx = 0,
|
|
124
|
+
ry = 0,
|
|
125
|
+
sweepFlag = 0,
|
|
126
|
+
startAngle = 0,
|
|
127
|
+
endAngle = 0,
|
|
128
|
+
} = segment['arcParams'] || {};
|
|
129
|
+
if (sweepFlag === 0) {
|
|
130
|
+
d *= -1;
|
|
131
|
+
}
|
|
132
|
+
const dx1 = rx * Math.cos(startAngle - d) + cx;
|
|
133
|
+
const dy1 = ry * Math.sin(startAngle - d) + cy;
|
|
134
|
+
segment.startTangent = [dx1 - startMovePoint[0], dy1 - startMovePoint[1]];
|
|
135
|
+
const dx2 = rx * Math.cos(startAngle + endAngle + d) + cx;
|
|
136
|
+
const dy2 = ry * Math.sin(startAngle + endAngle - d) + cy;
|
|
137
|
+
segment.endTangent = [prePoint[0] - dx2, prePoint[1] - dy2];
|
|
138
|
+
}
|
|
139
|
+
segments.push(segment);
|
|
140
|
+
}
|
|
141
|
+
return segments;
|
|
142
|
+
}
|