UNPKG

1.52 kBJavaScriptView Raw
1import { __read, __spreadArray } from "tslib";
2import { Line as LineUtil, Polyline as PolylineUtil } from '@antv/g-math';
3import { isString } from '../../utils';
4/**
5 * @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/points
6 *
7 * @example
8 * points="100,10 250,150 200,110"
9 */
10
11export function parsePoints(pointsOrStr, object) {
12 var points;
13
14 if (isString(pointsOrStr)) {
15 points = pointsOrStr.split(' ').map(function (pointStr) {
16 var _a = __read(pointStr.split(','), 2),
17 x = _a[0],
18 y = _a[1];
19
20 return [Number(x), Number(y)];
21 });
22 } else {
23 points = pointsOrStr;
24 }
25
26 var segments = [];
27 var tempLength = 0;
28 var segmentT;
29 var segmentL;
30 var totalLength = PolylineUtil.length(points);
31 points.forEach(function (p, i) {
32 if (points[i + 1]) {
33 segmentT = [0, 0];
34 segmentT[0] = tempLength / totalLength;
35 segmentL = LineUtil.length(p[0], p[1], points[i + 1][0], points[i + 1][1]);
36 tempLength += segmentL;
37 segmentT[1] = tempLength / totalLength;
38 segments.push(segmentT);
39 }
40 });
41 var minX = Math.min.apply(Math, __spreadArray([], __read(points.map(function (point) {
42 return point[0];
43 })), false));
44 var minY = Math.min.apply(Math, __spreadArray([], __read(points.map(function (point) {
45 return point[1];
46 })), false));
47
48 if (object) {
49 object.parsedStyle.defX = minX;
50 object.parsedStyle.defY = minY;
51 }
52
53 return {
54 points: points,
55 totalLength: totalLength,
56 segments: segments
57 };
58}
\No newline at end of file