@antv/g 5.2.0 → 5.2.3

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/dist/index.js CHANGED
@@ -7,17 +7,16 @@ var RBush = require('rbush');
7
7
  require('reflect-metadata');
8
8
  var tslib = require('tslib');
9
9
  var glMatrix = require('gl-matrix');
10
- var gMath = require('@antv/g-math');
11
10
  var eventemitter3 = require('eventemitter3');
12
11
  var d3 = require('d3-color');
13
- var memoize = require('lodash.memoize');
12
+ var lodashEs = require('lodash-es');
14
13
  var util = require('@antv/util');
14
+ var gMath = require('@antv/g-math');
15
15
  var requestAnimationFramePolyfill = require('request-animation-frame-polyfill');
16
16
 
17
17
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
18
18
 
19
19
  var RBush__default = /*#__PURE__*/_interopDefaultLegacy(RBush);
20
- var memoize__default = /*#__PURE__*/_interopDefaultLegacy(memoize);
21
20
 
22
21
  function _regeneratorRuntime() {
23
22
  /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
@@ -420,7 +419,7 @@ function _createClass(Constructor, protoProps, staticProps) {
420
419
  }
421
420
 
422
421
  function _extends() {
423
- _extends = Object.assign || function (target) {
422
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
424
423
  for (var i = 1; i < arguments.length; i++) {
425
424
  var source = arguments[i];
426
425
 
@@ -433,7 +432,6 @@ function _extends() {
433
432
 
434
433
  return target;
435
434
  };
436
-
437
435
  return _extends.apply(this, arguments);
438
436
  }
439
437
 
@@ -445,11 +443,10 @@ function _inheritsLoose(subClass, superClass) {
445
443
  }
446
444
 
447
445
  function _setPrototypeOf(o, p) {
448
- _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
446
+ _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
449
447
  o.__proto__ = p;
450
448
  return o;
451
449
  };
452
-
453
450
  return _setPrototypeOf(o, p);
454
451
  }
455
452
 
@@ -468,7 +465,7 @@ function _isNativeReflectConstruct() {
468
465
 
469
466
  function _construct(Parent, args, Class) {
470
467
  if (_isNativeReflectConstruct()) {
471
- _construct = Reflect.construct;
468
+ _construct = Reflect.construct.bind();
472
469
  } else {
473
470
  _construct = function _construct(Parent, args, Class) {
474
471
  var a = [null];
@@ -2578,210 +2575,134 @@ function formatAttribute(name, value) {
2578
2575
 
2579
2576
  var CanvasConfig = manaSyringe.Syringe.defineToken('CanvasConfig');
2580
2577
 
2581
- /**
2582
- * implements morph animation with cubic splitting
2583
- * @see http://thednp.github.io/kute.js/svgCubicMorph.html
2584
- */
2578
+ function hasArcOrBezier(path) {
2579
+ var hasArc = false;
2580
+ var count = path.length;
2585
2581
 
2586
- function midPoint(a, b, t) {
2587
- var ax = a[0];
2588
- var ay = a[1];
2589
- var bx = b[0];
2590
- var by = b[1];
2591
- return [ax + (bx - ax) * t, ay + (by - ay) * t];
2592
- }
2582
+ for (var i = 0; i < count; i++) {
2583
+ var params = path[i];
2584
+ var cmd = params[0];
2585
+
2586
+ if (cmd === 'C' || cmd === 'A' || cmd === 'Q') {
2587
+ hasArc = true;
2588
+ break;
2589
+ }
2590
+ }
2593
2591
 
2594
- function splitCubic(pts, t) {
2595
- if (t === void 0) {
2596
- t = 0.5;
2597
- }
2598
-
2599
- var p0 = pts.slice(0, 2);
2600
- var p1 = pts.slice(2, 4);
2601
- var p2 = pts.slice(4, 6);
2602
- var p3 = pts.slice(6, 8);
2603
- var p4 = midPoint(p0, p1, t);
2604
- var p5 = midPoint(p1, p2, t);
2605
- var p6 = midPoint(p2, p3, t);
2606
- var p7 = midPoint(p4, p5, t);
2607
- var p8 = midPoint(p5, p6, t);
2608
- var p9 = midPoint(p7, p8, t);
2609
- return [// @ts-ignore
2610
- ['C'].concat(p4, p7, p9), // @ts-ignore
2611
- ['C'].concat(p8, p6, p3)];
2592
+ return hasArc;
2612
2593
  }
2594
+ function extractPolygons(pathArray) {
2595
+ var polygons = [];
2596
+ var polylines = [];
2597
+ var points = []; // 防止第一个命令不是 'M'
2613
2598
 
2614
- function getCurveArray(segments) {
2615
- return segments.map(function (segment, i, pathArray) {
2616
- // @ts-ignore
2617
- var segmentData = i && pathArray[i - 1].slice(-2).concat(segment.slice(1)); // @ts-ignore
2599
+ for (var i = 0; i < pathArray.length; i++) {
2600
+ var params = pathArray[i];
2601
+ var cmd = params[0];
2602
+
2603
+ if (cmd === 'M') {
2604
+ // 遇到 'M' 判定是否是新数组,新数组中没有点
2605
+ if (points.length) {
2606
+ // 如果存在点,则说明没有遇到 'Z',开始了一个新的多边形
2607
+ polylines.push(points);
2608
+ points = []; // 创建新的点
2609
+ }
2618
2610
 
2619
- var curveLength = i ? gMath.Cubic.length.apply(gMath.Cubic, segmentData) : 0;
2620
- var subsegs;
2611
+ points.push([params[1], params[2]]);
2612
+ } else if (cmd === 'Z') {
2613
+ if (points.length) {
2614
+ // 存在点
2615
+ polygons.push(points);
2616
+ points = []; // 开始新的点集合
2617
+ } // 如果不存在点,同时 'Z',则说明是错误,不处理
2621
2618
 
2622
- if (i) {
2623
- // must be [segment,segment]
2624
- subsegs = curveLength ? splitCubic(segmentData) : [segment, segment];
2625
2619
  } else {
2626
- subsegs = [segment];
2620
+ points.push([params[1], params[2]]);
2627
2621
  }
2622
+ } // 说明 points 未放入 polygons 或者 polyline
2623
+ // 仅当只有一个 M,没有 Z 时会发生这种情况
2628
2624
 
2629
- return {
2630
- s: segment,
2631
- ss: subsegs,
2632
- l: curveLength
2633
- };
2634
- });
2635
- }
2636
2625
 
2637
- function equalizeSegments(path1, path2, TL) {
2638
- var c1 = getCurveArray(path1);
2639
- var c2 = getCurveArray(path2);
2640
- var L1 = c1.length;
2641
- var L2 = c2.length;
2642
- var l1 = c1.filter(function (x) {
2643
- return x.l;
2644
- }).length;
2645
- var l2 = c2.filter(function (x) {
2646
- return x.l;
2647
- }).length;
2648
- var m1 = c1.filter(function (x) {
2649
- return x.l;
2650
- }).reduce(function (a, _ref) {
2651
- var l = _ref.l;
2652
- return a + l;
2653
- }, 0) / l1 || 0;
2654
- var m2 = c2.filter(function (x) {
2655
- return x.l;
2656
- }).reduce(function (a, _ref2) {
2657
- var l = _ref2.l;
2658
- return a + l;
2659
- }, 0) / l2 || 0;
2660
- var tl = TL || Math.max(L1, L2);
2661
- var mm = [m1, m2];
2662
- var dif = [tl - L1, tl - L2];
2663
- var canSplit = 0;
2664
- var result = [c1, c2].map(function (x, i) {
2665
- return (// @ts-ignore
2666
- x.l === tl ? x.map(function (y) {
2667
- return y.s;
2668
- }) : x.map(function (y, j) {
2669
- canSplit = j && dif[i] && y.l >= mm[i];
2670
- dif[i] -= canSplit ? 1 : 0;
2671
- return canSplit ? y.ss : [y.s];
2672
- }).flat()
2673
- );
2674
- });
2675
- return result[0].length === result[1].length ? result : equalizeSegments(result[0], result[1], tl);
2676
- }
2677
- function getDrawDirection(pathArray) {
2678
- return getPathArea(pathArray) >= 0;
2626
+ if (points.length > 0) {
2627
+ polylines.push(points);
2628
+ }
2629
+
2630
+ return {
2631
+ polygons: polygons,
2632
+ polylines: polylines
2633
+ };
2679
2634
  }
2680
2635
 
2681
- function getCubicSegArea(x0, y0, x1, y1, x2, y2, x3, y3) {
2682
- // https://stackoverflow.com/a/15845996
2683
- return 3 * ((y3 - y0) * (x1 + x2) - (x3 - x0) * (y1 + y2) + y1 * (x0 - x2) - x1 * (y0 - y2) + y3 * (x2 + x0 / 3) - x3 * (y2 + y0 / 3)) / 20;
2636
+ function isSamePoint(point1, point2) {
2637
+ return point1[0] === point2[0] && point1[1] === point2[1];
2684
2638
  }
2685
2639
 
2686
- function getPathArea(pathArray) {
2687
- var x = 0;
2688
- var y = 0;
2689
- var mx = 0;
2690
- var my = 0;
2691
- var len = 0;
2692
- return pathArray.map(function (seg) {
2693
- switch (seg[0]) {
2640
+ function path2Segments(path) {
2641
+ var segments = [];
2642
+ var currentPoint = null; // 当前图形
2643
+
2644
+ var nextParams = null; // 下一节点的 path 参数
2645
+ // let startMovePoint = null; // 开始 M 的点,可能会有多个
2646
+
2647
+ var lastStartMovePointIndex = 0; // 最近一个开始点 M 的索引
2648
+
2649
+ var count = path.length;
2650
+
2651
+ for (var i = 0; i < count; i++) {
2652
+ var params = path[i];
2653
+ nextParams = path[i + 1];
2654
+ var command = params[0]; // 数学定义上的参数,便于后面的计算
2655
+
2656
+ var segment = {
2657
+ command: command,
2658
+ prePoint: currentPoint,
2659
+ params: params,
2660
+ startTangent: null,
2661
+ endTangent: null
2662
+ };
2663
+
2664
+ switch (command) {
2694
2665
  case 'M':
2695
- case 'Z':
2696
- mx = seg[0] === 'M' ? seg[1] : mx;
2697
- my = seg[0] === 'M' ? seg[2] : my;
2698
- x = mx;
2699
- y = my;
2700
- return 0;
2666
+ // startMovePoint = [params[1], params[2]];
2667
+ lastStartMovePointIndex = i;
2668
+ break;
2669
+ }
2701
2670
 
2702
- default:
2703
- // @ts-ignore
2704
- len = getCubicSegArea.apply(0, [x, y].concat(seg.slice(1)));
2705
-
2706
- var _seg$slice = seg.slice(-2);
2707
-
2708
- x = _seg$slice[0];
2709
- y = _seg$slice[1];
2710
- return len;
2711
- }
2712
- }).reduce(function (a, b) {
2713
- return a + b;
2714
- }, 0);
2715
- } // reverse CURVE based pathArray segments only
2716
-
2717
- function reverseCurve(pathArray) {
2718
- var rotatedCurve = pathArray.slice(1).map(function (x, i, curveOnly) {
2719
- return (// @ts-ignore
2720
- !i ? pathArray[0].slice(1).concat(x.slice(1)) : curveOnly[i - 1].slice(-2).concat(x.slice(1))
2721
- );
2722
- }) // @ts-ignore
2723
- .map(function (x) {
2724
- return x.map(function (y, i) {
2725
- return x[x.length - i - 2 * (1 - i % 2)];
2726
- });
2727
- }).reverse(); // @ts-ignore
2671
+ var len = params.length;
2672
+ currentPoint = [params[len - 2], params[len - 1]]; // @ts-ignore
2728
2673
 
2729
- return [['M'].concat(rotatedCurve[0].slice(0, 2))].concat(rotatedCurve.map(function (x) {
2730
- return ['C'].concat(x.slice(2));
2731
- }));
2732
- }
2733
- function clonePath(pathArray) {
2734
- // @ts-ignore
2735
- return pathArray.map(function (x) {
2736
- if (Array.isArray(x)) {
2737
- // @ts-ignore
2738
- return clonePath(x);
2674
+ segment.currentPoint = currentPoint; // 如果当前点与最近一个 M 点相同,则最近一个 M 点的前一个点为当前点的前一个点
2675
+
2676
+ if (segments[lastStartMovePointIndex] && isSamePoint(currentPoint, segments[lastStartMovePointIndex].currentPoint)) {
2677
+ segments[lastStartMovePointIndex].prePoint = segment.prePoint;
2739
2678
  }
2740
2679
 
2741
- return !Number.isNaN(+x) ? +x : x;
2742
- });
2743
- }
2680
+ var nextPoint = nextParams ? [nextParams[nextParams.length - 2], nextParams[nextParams.length - 1]] : null; // @ts-ignore
2744
2681
 
2745
- function getRotations(a) {
2746
- var segCount = a.length;
2747
- var pointCount = segCount - 1;
2748
- return a.map(function (f, idx) {
2749
- return a.map(function (p, i) {
2750
- var oldSegIdx = idx + i;
2751
- var seg;
2682
+ segment.nextPoint = nextPoint; // Add startTangent and endTangent
2752
2683
 
2753
- if (i === 0 || a[oldSegIdx] && a[oldSegIdx][0] === 'M') {
2754
- seg = a[oldSegIdx]; // @ts-ignore
2684
+ var prePoint = segment.prePoint;
2755
2685
 
2756
- return ['M'].concat(seg.slice(-2));
2686
+ if (command === 'C') {
2687
+ // 三次贝塞尔曲线有两个控制点
2688
+ var cp1 = [params[1], params[2]];
2689
+ var cp2 = [params[3], params[4]];
2690
+ segment.startTangent = [prePoint[0] - cp1[0], prePoint[1] - cp1[1]];
2691
+ segment.endTangent = [currentPoint[0] - cp2[0], currentPoint[1] - cp2[1]]; // horizontal line, eg. ['C', 100, 100, 100, 100, 200, 200]
2692
+
2693
+ if (segment.startTangent[0] === 0 && segment.startTangent[1] === 0) {
2694
+ segment.startTangent = [cp1[0] - cp2[0], cp1[1] - cp2[1]];
2757
2695
  }
2758
2696
 
2759
- if (oldSegIdx >= segCount) oldSegIdx -= pointCount;
2760
- return a[oldSegIdx];
2761
- });
2762
- });
2763
- }
2697
+ if (segment.endTangent[0] === 0 && segment.endTangent[1] === 0) {
2698
+ segment.endTangent = [cp2[0] - cp1[0], cp2[1] - cp1[1]];
2699
+ }
2700
+ }
2764
2701
 
2765
- function distanceSquareRoot(a, b) {
2766
- return Math.sqrt((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]));
2767
- }
2702
+ segments.push(segment);
2703
+ }
2768
2704
 
2769
- function getRotatedCurve(a, b) {
2770
- var segCount = a.length - 1;
2771
- var lineLengths = [];
2772
- var computedIndex = 0;
2773
- var sumLensSqrd = 0;
2774
- var rotations = getRotations(a);
2775
- rotations.forEach(function (r, i) {
2776
- a.slice(1).forEach(function (s, j) {
2777
- // @ts-ignore
2778
- sumLensSqrd += distanceSquareRoot(a[(i + j) % segCount].slice(-2), b[j % segCount].slice(-2));
2779
- });
2780
- lineLengths[i] = sumLensSqrd;
2781
- sumLensSqrd = 0;
2782
- });
2783
- computedIndex = lineLengths.indexOf(Math.min.apply(null, lineLengths));
2784
- return rotations[computedIndex];
2705
+ return segments;
2785
2706
  }
2786
2707
 
2787
2708
  function commandsToPathString(commands, object, applyLocalTransform) {
@@ -2966,6 +2887,37 @@ function convertToPath(object, applyLocalTransform) {
2966
2887
  return commandsToPathString(commands, object, applyLocalTransform);
2967
2888
  }
2968
2889
  }
2890
+ function translatePathToString(pathArray, x, y) {
2891
+ var newValue = pathArray.map(function (params) {
2892
+ var command = params[0];
2893
+
2894
+ switch (command) {
2895
+ case 'M':
2896
+ return "M " + (params[1] - x) + "," + (params[2] - y);
2897
+
2898
+ case 'L':
2899
+ return "L " + (params[1] - x) + "," + (params[2] - y);
2900
+
2901
+ case 'Q':
2902
+ return "Q " + (params[1] - x) + " " + (params[2] - y) + "," + (params[3] - x) + " " + (params[4] - y);
2903
+
2904
+ case 'C':
2905
+ return "C " + (params[1] - x) + " " + (params[2] - y) + "," + (params[3] - x) + " " + (params[4] - y) + "," + (params[5] - x) + " " + (params[6] - y);
2906
+
2907
+ case 'A':
2908
+ return "A " + params[1] + " " + params[2] + " " + params[3] + " " + params[4] + " " + params[5] + " " + (params[6] - x) + " " + (params[7] - y);
2909
+
2910
+ case 'Z':
2911
+ return 'Z';
2912
+ }
2913
+ }).join(' ');
2914
+
2915
+ if (~newValue.indexOf('NaN')) {
2916
+ return '';
2917
+ }
2918
+
2919
+ return newValue;
2920
+ }
2969
2921
 
2970
2922
  // @ts-nocheck
2971
2923
  // interface HookInterceptor<T, R, AdditionalOptions = UnsetAdditionalOptions> {
@@ -7111,7 +7063,7 @@ var SideOrCornerToDegMap = {
7111
7063
  'right bottom': 135 - 90,
7112
7064
  'bottom right': 135 - 90
7113
7065
  };
7114
- var angleToDeg = memoize__default['default'](function (orientation) {
7066
+ var angleToDeg = lodashEs.memoize(function (orientation) {
7115
7067
  var angle;
7116
7068
 
7117
7069
  if (orientation.type === 'angular') {
@@ -7122,7 +7074,7 @@ var angleToDeg = memoize__default['default'](function (orientation) {
7122
7074
 
7123
7075
  return angle;
7124
7076
  });
7125
- var positonToPercentage = memoize__default['default'](function (position) {
7077
+ var positonToPercentage = lodashEs.memoize(function (position) {
7126
7078
  var cx = 0.5;
7127
7079
  var cy = 0.5;
7128
7080
 
@@ -7173,7 +7125,7 @@ var positonToPercentage = memoize__default['default'](function (position) {
7173
7125
  cy: cy
7174
7126
  };
7175
7127
  });
7176
- var parseGradient$1 = memoize__default['default'](function (colorStr) {
7128
+ var parseGradient$1 = lodashEs.memoize(function (colorStr) {
7177
7129
  if (colorStr.indexOf('linear') > -1 || colorStr.indexOf('radial') > -1) {
7178
7130
  var ast = parseGradient(colorStr);
7179
7131
  return ast.map(function (_ref) {
@@ -7326,7 +7278,7 @@ function isPattern(object) {
7326
7278
  * @see https://github.com/WebKit/WebKit/blob/main/Source/WebCore/css/parser/CSSParser.cpp#L97
7327
7279
  */
7328
7280
 
7329
- var parseColor = memoize__default['default'](function (colorStr) {
7281
+ var parseColor = lodashEs.memoize(function (colorStr) {
7330
7282
  if (isPattern(colorStr)) {
7331
7283
  return _extends({
7332
7284
  repetition: 'repeat'
@@ -8181,31 +8133,40 @@ function mergeNumberLists(left, right) {
8181
8133
  }
8182
8134
 
8183
8135
  function parsePath(path, object) {
8184
- var absolutePath = util.path2Absolute(path);
8136
+ var absolutePath;
8137
+
8138
+ try {
8139
+ absolutePath = util.normalizePath(path);
8140
+ } catch (e) {
8141
+ absolutePath = util.normalizePath('');
8142
+ console.error("[g]: Invalid SVG Path definition: " + path);
8143
+ }
8144
+
8185
8145
  var hasArc = hasArcOrBezier(absolutePath);
8186
- var clonedAbsolutePath = absolutePath.slice();
8187
8146
 
8188
- var _extractPolygons = extractPolygons(clonedAbsolutePath),
8147
+ var _extractPolygons = extractPolygons(absolutePath),
8189
8148
  polygons = _extractPolygons.polygons,
8190
8149
  polylines = _extractPolygons.polylines; // convert to curves to do morphing & picking later
8191
8150
  // @see http://thednp.github.io/kute.js/svgCubicMorph.html
8192
8151
 
8193
8152
 
8194
- var _path2Curve = util.path2Curve(clonedAbsolutePath, true),
8153
+ var _path2Curve = util.path2Curve(absolutePath, true),
8195
8154
  curve = _path2Curve[0],
8196
- zCommandIndexes = _path2Curve[1];
8155
+ zCommandIndexes = _path2Curve[1]; // for later use
8197
8156
 
8198
- var segments = util.path2Segments(clonedAbsolutePath);
8199
8157
 
8200
- var _calcLength = calcLength(curve),
8201
- totalLength = _calcLength.totalLength,
8202
- curveSegments = _calcLength.curveSegments;
8158
+ var segments = path2Segments(curve);
8203
8159
 
8204
- var rect = getPathBBox(curve);
8160
+ var _getPathBBoxTotalLeng = util.getPathBBoxTotalLength(absolutePath),
8161
+ x = _getPathBBoxTotalLeng.x,
8162
+ y = _getPathBBoxTotalLeng.y,
8163
+ width = _getPathBBoxTotalLeng.width,
8164
+ height = _getPathBBoxTotalLeng.height,
8165
+ length = _getPathBBoxTotalLeng.length;
8205
8166
 
8206
8167
  if (object) {
8207
- object.parsedStyle.defX = rect.x;
8208
- object.parsedStyle.defY = rect.y;
8168
+ object.parsedStyle.defX = x;
8169
+ object.parsedStyle.defY = y;
8209
8170
  }
8210
8171
 
8211
8172
  return {
@@ -8215,169 +8176,22 @@ function parsePath(path, object) {
8215
8176
  polygons: polygons,
8216
8177
  polylines: polylines,
8217
8178
  curve: curve,
8218
- totalLength: totalLength,
8219
- curveSegments: curveSegments,
8179
+ totalLength: length,
8220
8180
  zCommandIndexes: zCommandIndexes,
8221
- rect: rect
8181
+ rect: new Rectangle(x, y, width, height)
8222
8182
  };
8223
8183
  }
8224
-
8225
- function hasArcOrBezier(path) {
8226
- var hasArc = false;
8227
- var count = path.length;
8228
-
8229
- for (var i = 0; i < count; i++) {
8230
- var params = path[i];
8231
- var cmd = params[0];
8232
-
8233
- if (cmd === 'C' || cmd === 'A' || cmd === 'Q') {
8234
- hasArc = true;
8235
- break;
8236
- }
8237
- }
8238
-
8239
- return hasArc;
8240
- }
8241
-
8242
- function extractPolygons(pathArray) {
8243
- var polygons = [];
8244
- var polylines = [];
8245
- var points = []; // 防止第一个命令不是 'M'
8246
-
8247
- for (var i = 0; i < pathArray.length; i++) {
8248
- var params = pathArray[i];
8249
- var cmd = params[0];
8250
-
8251
- if (cmd === 'M') {
8252
- // 遇到 'M' 判定是否是新数组,新数组中没有点
8253
- if (points.length) {
8254
- // 如果存在点,则说明没有遇到 'Z',开始了一个新的多边形
8255
- polylines.push(points);
8256
- points = []; // 创建新的点
8257
- }
8258
-
8259
- points.push([params[1], params[2]]);
8260
- } else if (cmd === 'Z') {
8261
- if (points.length) {
8262
- // 存在点
8263
- polygons.push(points);
8264
- points = []; // 开始新的点集合
8265
- } // 如果不存在点,同时 'Z',则说明是错误,不处理
8266
-
8267
- } else {
8268
- points.push([params[1], params[2]]);
8269
- }
8270
- } // 说明 points 未放入 polygons 或者 polyline
8271
- // 仅当只有一个 M,没有 Z 时会发生这种情况
8272
-
8273
-
8274
- if (points.length > 0) {
8275
- polylines.push(points);
8276
- }
8277
-
8278
- return {
8279
- polygons: polygons,
8280
- polylines: polylines
8281
- };
8282
- }
8283
-
8284
- function calcLength(curve) {
8285
- var totalLength = 0;
8286
- var tempLength = 0; // 每段 curve 对应起止点的长度比例列表,形如: [[0, 0.25], [0.25, 0.6]. [0.6, 0.9], [0.9, 1]]
8287
-
8288
- var curveSegments = [];
8289
- var segmentT;
8290
- var segmentL;
8291
- var segmentN;
8292
- var l;
8293
-
8294
- if (!curve) {
8295
- return {
8296
- curveSegments: [],
8297
- totalLength: totalLength
8298
- };
8299
- }
8300
-
8301
- curve.forEach(function (segment, i) {
8302
- segmentN = curve[i + 1];
8303
- l = segment.length;
8304
-
8305
- if (segmentN) {
8306
- totalLength += gMath.Cubic.length(segment[l - 2], segment[l - 1], segmentN[1], segmentN[2], segmentN[3], segmentN[4], segmentN[5], segmentN[6]) || 0;
8307
- }
8308
- });
8309
-
8310
- if (totalLength === 0) {
8311
- return {
8312
- curveSegments: [],
8313
- totalLength: totalLength
8314
- };
8315
- }
8316
-
8317
- curve.forEach(function (segment, i) {
8318
- segmentN = curve[i + 1];
8319
- l = segment.length;
8320
-
8321
- if (segmentN) {
8322
- segmentT = [];
8323
- segmentT[0] = tempLength / totalLength;
8324
- segmentL = gMath.Cubic.length(segment[l - 2], segment[l - 1], segmentN[1], segmentN[2], segmentN[3], segmentN[4], segmentN[5], segmentN[6]); // 当 path 不连续时,segmentL 可能为空,为空时需要作为 0 处理
8325
-
8326
- tempLength += segmentL || 0;
8327
- segmentT[1] = tempLength / totalLength;
8328
- curveSegments.push(segmentT);
8329
- }
8330
- });
8331
- return {
8332
- curveSegments: curveSegments,
8333
- totalLength: totalLength
8334
- };
8335
- }
8336
-
8337
- function getPathBBox(segments) {
8338
- var x = 0;
8339
- var y = 0;
8340
- var X = [];
8341
- var Y = [];
8342
- segments.forEach(function (segment) {
8343
- var _segment$slice = segment.slice(-2),
8344
- s1 = _segment$slice[0],
8345
- s2 = _segment$slice[1];
8346
-
8347
- if (segment[0] === 'M') {
8348
- x = s1;
8349
- y = s2;
8350
- X.push(s1);
8351
- Y.push(s2);
8352
- } else if (segment[0] === 'C') {
8353
- // @ts-ignore
8354
- var dim = gMath.Cubic.box.apply(gMath.Cubic, [x, y].concat(segment.slice(1)));
8355
- X = X.concat(dim.x, dim.x + dim.width);
8356
- Y = Y.concat(dim.y, dim.y + dim.height);
8357
- x = s1;
8358
- y = s2;
8359
- }
8360
- });
8361
- var xTop = Math.min.apply(0, X);
8362
- var yTop = Math.min.apply(0, Y);
8363
- var xBot = Math.max.apply(0, X);
8364
- var yBot = Math.max.apply(0, Y);
8365
- var width = xBot - xTop;
8366
- var height = yBot - yTop;
8367
- return new Rectangle(xTop, yTop, width, height);
8368
- }
8369
-
8370
8184
  function mergePaths(left, right, object) {
8371
8185
  var curve1 = left.curve;
8372
8186
  var curve2 = right.curve;
8373
8187
  var curves = [curve1, curve2];
8374
8188
 
8375
8189
  if (curve1.length !== curve2.length) {
8376
- curves = equalizeSegments(curve1, curve2);
8190
+ curves = util.equalizeSegments(curve1, curve2);
8377
8191
  }
8378
8192
 
8379
- var curve0 = getDrawDirection(curves[0]) !== getDrawDirection(curves[1]) ? reverseCurve(curves[0]) : clonePath(curves[0]);
8380
- return [curve0, getRotatedCurve(curves[1], curve0), function (pathArray) {
8193
+ var curve0 = util.getDrawDirection(curves[0]) !== util.getDrawDirection(curves[1]) ? util.reverseCurve(curves[0]) : util.clonePath(curves[0]);
8194
+ return [curve0, util.getRotatedCurve(curves[1], curve0), function (pathArray) {
8381
8195
  // need converting to path string?
8382
8196
  return pathArray;
8383
8197
  }];
@@ -12302,37 +12116,13 @@ var Path = /*#__PURE__*/function (_DisplayObject) {
12302
12116
  ;
12303
12117
 
12304
12118
  _proto.getPoint = function getPoint(ratio) {
12305
- var subt = 0;
12306
- var index = 0;
12307
- var curve = this.parsedStyle.path.curve;
12119
+ var _this$parsedStyle$pat = this.parsedStyle.path,
12120
+ totalLength = _this$parsedStyle$pat.totalLength,
12121
+ absolutePath = _this$parsedStyle$pat.absolutePath;
12308
12122
 
12309
- if (!this.parsedStyle.path.curveSegments.length) {
12310
- if (curve) {
12311
- return new Point(curve[0][1], curve[0][2]);
12312
- }
12313
-
12314
- return null;
12315
- }
12316
-
12317
- this.parsedStyle.path.curveSegments.forEach(function (v, i) {
12318
- if (ratio >= v[0] && ratio <= v[1]) {
12319
- subt = (ratio - v[0]) / (v[1] - v[0]);
12320
- index = i;
12321
- }
12322
- });
12323
- var seg = curve[index];
12324
-
12325
- if (isNil(seg) || isNil(index)) {
12326
- return null;
12327
- }
12328
-
12329
- var l = seg.length;
12330
- var nextSeg = curve[index + 1];
12331
-
12332
- var _CubicUtil$pointAt = gMath.Cubic.pointAt( // @ts-ignore
12333
- seg[l - 2], seg[l - 1], nextSeg[1], nextSeg[2], nextSeg[3], nextSeg[4], nextSeg[5], nextSeg[6], subt),
12334
- x = _CubicUtil$pointAt.x,
12335
- y = _CubicUtil$pointAt.y;
12123
+ var _getPointAtLength = util.getPointAtLength(absolutePath, ratio * totalLength),
12124
+ x = _getPointAtLength.x,
12125
+ y = _getPointAtLength.y;
12336
12126
 
12337
12127
  return new Point(x, y);
12338
12128
  }
@@ -18385,143 +18175,6 @@ exports.PathUpdater = tslib.__decorate([manaSyringe.singleton({
18385
18175
  named: exports.Shape.PATH
18386
18176
  }
18387
18177
  })], exports.PathUpdater);
18388
- // let xArr = [];
18389
- // let yArr = [];
18390
- // const segmentsWithAngle = [];
18391
- // for (let i = 0; i < segments.length; i++) {
18392
- // const segment = segments[i];
18393
- // const { currentPoint, params, prePoint } = segment;
18394
- // let box;
18395
- // switch (segment.command) {
18396
- // case 'Q':
18397
- // box = QuadUtil.box(prePoint[0], prePoint[1], params[1], params[2], params[3], params[4]);
18398
- // break;
18399
- // case 'C':
18400
- // box = CubicUtil.box(
18401
- // prePoint[0],
18402
- // prePoint[1],
18403
- // params[1],
18404
- // params[2],
18405
- // params[3],
18406
- // params[4],
18407
- // params[5],
18408
- // params[6],
18409
- // );
18410
- // break;
18411
- // case 'A':
18412
- // // eslint-disable-next-line no-case-declarations
18413
- // const { arcParams } = segment;
18414
- // box = EllipseArcUtil.box(
18415
- // arcParams.cx,
18416
- // arcParams.cy,
18417
- // arcParams.rx,
18418
- // arcParams.ry,
18419
- // arcParams.xRotation,
18420
- // arcParams.startAngle,
18421
- // arcParams.endAngle,
18422
- // );
18423
- // break;
18424
- // default:
18425
- // xArr.push(currentPoint[0]);
18426
- // yArr.push(currentPoint[1]);
18427
- // break;
18428
- // }
18429
- // if (box) {
18430
- // segment.box = box;
18431
- // xArr.push(box.x, box.x + box.width);
18432
- // yArr.push(box.y, box.y + box.height);
18433
- // }
18434
- // if (
18435
- // lineWidth &&
18436
- // (segment.command === 'L' || segment.command === 'M') &&
18437
- // segment.prePoint &&
18438
- // segment.nextPoint
18439
- // ) {
18440
- // segmentsWithAngle.push(segment);
18441
- // }
18442
- // }
18443
- // // bbox calculation should ignore NaN for path attribute
18444
- // // ref: https://github.com/antvis/g/issues/210
18445
- // xArr = xArr.filter((item) => !Number.isNaN(item));
18446
- // yArr = yArr.filter((item) => !Number.isNaN(item));
18447
- // let minX = min(xArr);
18448
- // let minY = min(yArr);
18449
- // let maxX = max(xArr);
18450
- // let maxY = max(yArr);
18451
- // if (segmentsWithAngle.length === 0) {
18452
- // return {
18453
- // x: minX,
18454
- // y: minY,
18455
- // width: maxX - minX,
18456
- // height: maxY - minY,
18457
- // };
18458
- // }
18459
- // for (let i = 0; i < segmentsWithAngle.length; i++) {
18460
- // const segment = segmentsWithAngle[i];
18461
- // const { currentPoint } = segment;
18462
- // let extra;
18463
- // if (currentPoint[0] === minX) {
18464
- // extra = getExtraFromSegmentWithAngle(segment, lineWidth);
18465
- // minX -= extra.xExtra;
18466
- // } else if (currentPoint[0] === maxX) {
18467
- // extra = getExtraFromSegmentWithAngle(segment, lineWidth);
18468
- // maxX += extra.xExtra;
18469
- // }
18470
- // if (currentPoint[1] === minY) {
18471
- // extra = getExtraFromSegmentWithAngle(segment, lineWidth);
18472
- // minY -= extra.yExtra;
18473
- // } else if (currentPoint[1] === maxY) {
18474
- // extra = getExtraFromSegmentWithAngle(segment, lineWidth);
18475
- // maxY += extra.yExtra;
18476
- // }
18477
- // }
18478
- // return {
18479
- // x: minX,
18480
- // y: minY,
18481
- // width: maxX - minX,
18482
- // height: maxY - minY,
18483
- // };
18484
- // }
18485
- // function getExtraFromSegmentWithAngle(segment: any, lineWidth: number) {
18486
- // const { prePoint, currentPoint, nextPoint } = segment;
18487
- // const currentAndPre =
18488
- // Math.pow(currentPoint[0] - prePoint[0], 2) + Math.pow(currentPoint[1] - prePoint[1], 2);
18489
- // const currentAndNext =
18490
- // Math.pow(currentPoint[0] - nextPoint[0], 2) + Math.pow(currentPoint[1] - nextPoint[1], 2);
18491
- // const preAndNext =
18492
- // Math.pow(prePoint[0] - nextPoint[0], 2) + Math.pow(prePoint[1] - nextPoint[1], 2);
18493
- // // 以 currentPoint 为顶点的夹角
18494
- // const currentAngle = Math.acos(
18495
- // (currentAndPre + currentAndNext - preAndNext) /
18496
- // (2 * Math.sqrt(currentAndPre) * Math.sqrt(currentAndNext)),
18497
- // );
18498
- // // 夹角为空、 0 或 PI 时,不需要计算夹角处的额外宽度
18499
- // // 注意: 由于计算精度问题,夹角为 0 的情况计算出来的角度可能是一个很小的值,还需要判断其与 0 是否近似相等
18500
- // if (!currentAngle || Math.sin(currentAngle) === 0 || isNumberEqual(currentAngle, 0)) {
18501
- // return {
18502
- // xExtra: 0,
18503
- // yExtra: 0,
18504
- // };
18505
- // }
18506
- // let xAngle = Math.abs(Math.atan2(nextPoint[1] - currentPoint[1], nextPoint[0] - currentPoint[0]));
18507
- // let yAngle = Math.abs(Math.atan2(nextPoint[0] - currentPoint[0], nextPoint[1] - currentPoint[1]));
18508
- // // 将夹角转为锐角
18509
- // xAngle = xAngle > Math.PI / 2 ? Math.PI - xAngle : xAngle;
18510
- // yAngle = yAngle > Math.PI / 2 ? Math.PI - yAngle : yAngle;
18511
- // // 这里不考虑在水平和垂直方向的投影,直接使用最大差值
18512
- // // 由于上层统一加减了二分之一线宽,这里需要进行弥补
18513
- // const extra = {
18514
- // // 水平方向投影
18515
- // xExtra:
18516
- // Math.cos(currentAngle / 2 - xAngle) * ((lineWidth / 2) * (1 / Math.sin(currentAngle / 2))) -
18517
- // lineWidth / 2 || 0,
18518
- // // 垂直方向投影
18519
- // yExtra:
18520
- // Math.cos(yAngle - currentAngle / 2) * ((lineWidth / 2) * (1 / Math.sin(currentAngle / 2))) -
18521
- // lineWidth / 2 || 0,
18522
- // };
18523
- // return extra;
18524
- // }
18525
18178
 
18526
18179
  /**
18527
18180
  * @see /zh/docs/api/basic/display-object#clippath
@@ -18974,7 +18627,6 @@ exports.CSSPropertyPath = /*#__PURE__*/function () {
18974
18627
  polylines: [],
18975
18628
  curve: [],
18976
18629
  totalLength: 0,
18977
- curveSegments: [],
18978
18630
  zCommandIndexes: [],
18979
18631
  rect: new Rectangle(0, 0, 0, 0)
18980
18632
  };
@@ -20103,8 +19755,16 @@ exports.DefaultStyleValueRegistry = /*#__PURE__*/function () {
20103
19755
 
20104
19756
  var center = glMatrix.vec3.fromValues((1 - (anchor && anchor[0].value || 0) * 2) * width / 2 + offsetX, (1 - (anchor && anchor[1].value || 0) * 2) * height / 2 + offsetY, (1 - (anchor && ((_anchor$ = anchor[2]) === null || _anchor$ === void 0 ? void 0 : _anchor$.value) || 0) * 2) * halfExtents[2] + offsetZ); // update geometry's AABB
20105
19757
 
20106
- geometry.contentBounds.update(center, halfExtents);
20107
- var halfLineWidth = (((lineWidth === null || lineWidth === void 0 ? void 0 : lineWidth.value) || 0) + ((increasedLineWidthForHitTesting === null || increasedLineWidthForHitTesting === void 0 ? void 0 : increasedLineWidthForHitTesting.value) || 0)) / 2; // append border
19758
+ geometry.contentBounds.update(center, halfExtents); // @see https://github.molgen.mpg.de/git-mirror/cairo/blob/master/src/cairo-stroke-style.c#L97..L128
19759
+
19760
+ var expansion = object.nodeName === exports.Shape.POLYLINE || object.nodeName === exports.Shape.POLYGON || object.nodeName === exports.Shape.PATH ? Math.SQRT2 : 0.5; // if (lineCap?.value === 'square') {
19761
+ // expansion = Math.SQRT1_2;
19762
+ // }
19763
+ // if (lineJoin?.value === 'miter' && expansion < Math.SQRT2 * miterLimit) {
19764
+ // expansion = Math.SQRT1_2 * miterLimit;
19765
+ // }
19766
+
19767
+ var halfLineWidth = (((lineWidth === null || lineWidth === void 0 ? void 0 : lineWidth.value) || 0) + ((increasedLineWidthForHitTesting === null || increasedLineWidthForHitTesting === void 0 ? void 0 : increasedLineWidthForHitTesting.value) || 0)) * expansion; // append border
20108
19768
 
20109
19769
  glMatrix.vec3.add(halfExtents, halfExtents, glMatrix.vec3.fromValues(halfLineWidth, halfLineWidth, 0));
20110
19770
  geometry.renderBounds.update(center, halfExtents); // account for shadow, only support constant value now
@@ -21925,7 +21585,6 @@ exports.camelCase = camelCase;
21925
21585
  exports.clamp = clamp;
21926
21586
  exports.clampedMergeNumbers = clampedMergeNumbers;
21927
21587
  exports.cleanExistedCanvas = cleanExistedCanvas;
21928
- exports.clonePath = clonePath;
21929
21588
  exports.colorStopToString = colorStopToString;
21930
21589
  exports.compareAnimations = compareAnimations;
21931
21590
  exports.computeLinearGradient = computeLinearGradient;
@@ -21941,22 +21600,20 @@ exports.deg2rad = deg2rad;
21941
21600
  exports.deg2turn = deg2turn;
21942
21601
  exports.dirtifyToRoot = dirtifyToRoot;
21943
21602
  exports.equalSet = equalSet;
21944
- exports.equalizeSegments = equalizeSegments;
21603
+ exports.extractPolygons = extractPolygons;
21945
21604
  exports.formatAttribute = formatAttribute;
21946
21605
  exports.fromRotationTranslationScale = fromRotationTranslationScale;
21947
21606
  exports.getAngle = getAngle;
21948
- exports.getDrawDirection = getDrawDirection;
21949
21607
  exports.getEasingFunction = getEasingFunction;
21950
21608
  exports.getEuler = getEuler;
21951
21609
  exports.getHeight = getHeight;
21952
- exports.getPathArea = getPathArea;
21953
- exports.getRotatedCurve = getRotatedCurve;
21954
21610
  exports.getRotationInRadians = getRotationInRadians;
21955
21611
  exports.getScaling = getScaling;
21956
21612
  exports.getStyle = getStyle;
21957
21613
  exports.getTranslation = getTranslation;
21958
21614
  exports.getWidth = getWidth;
21959
21615
  exports.grad2deg = grad2deg;
21616
+ exports.hasArcOrBezier = hasArcOrBezier;
21960
21617
  exports.isBoolean = isBoolean;
21961
21618
  exports.isBrowser = isBrowser;
21962
21619
  exports.isComparison = isComparison;
@@ -21998,11 +21655,12 @@ exports.parsePoints = parsePoints;
21998
21655
  exports.parseTransform = parseTransform;
21999
21656
  exports.parseTransformOrigin = parseTransformOrigin;
22000
21657
  exports.parserPercentage = parserPercentage;
21658
+ exports.path2Segments = path2Segments;
22001
21659
  exports.rad2deg = rad2deg;
22002
- exports.reverseCurve = reverseCurve;
22003
21660
  exports.setDOMSize = setDOMSize;
22004
21661
  exports.sortByZIndex = sortByZIndex;
22005
21662
  exports.toFontString = toFontString;
21663
+ exports.translatePathToString = translatePathToString;
22006
21664
  exports.turn2deg = turn2deg;
22007
21665
  exports.typeCheck = typeCheck;
22008
21666
  exports.unitTypeToBaseType = unitTypeToBaseType;