@leafer/core 1.0.0-rc.12 → 1.0.0-rc.17

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/lib/core.cjs CHANGED
@@ -405,6 +405,10 @@ const PointHelper = {
405
405
  t.x = point.x;
406
406
  t.y = point.y;
407
407
  },
408
+ copyFrom(t, x, y) {
409
+ t.x = x;
410
+ t.y = y;
411
+ },
408
412
  move(t, x, y) {
409
413
  t.x += x;
410
414
  t.y += y;
@@ -432,19 +436,19 @@ const PointHelper = {
432
436
  },
433
437
  tempToInnerOf(t, matrix) {
434
438
  const { tempPoint: temp } = P$5;
435
- P$5.copy(temp, t);
439
+ copy$6(temp, t);
436
440
  toInnerPoint$2(matrix, temp, temp);
437
441
  return temp;
438
442
  },
439
443
  tempToOuterOf(t, matrix) {
440
444
  const { tempPoint: temp } = P$5;
441
- P$5.copy(temp, t);
445
+ copy$6(temp, t);
442
446
  toOuterPoint$2(matrix, temp, temp);
443
447
  return temp;
444
448
  },
445
449
  tempToInnerRadiusPointOf(t, matrix) {
446
450
  const { tempRadiusPoint: temp } = P$5;
447
- P$5.copy(temp, t);
451
+ copy$6(temp, t);
448
452
  P$5.toInnerRadiusPointOf(t, matrix, temp);
449
453
  return temp;
450
454
  },
@@ -470,7 +474,7 @@ const PointHelper = {
470
474
  return y1 + (y2 - y1) / 2;
471
475
  },
472
476
  getDistance(t, point) {
473
- return P$5.getDistanceFrom(t.x, t.y, point.x, point.y);
477
+ return getDistanceFrom(t.x, t.y, point.x, point.y);
474
478
  },
475
479
  getDistanceFrom(x1, y1, x2, y2) {
476
480
  const x = abs$2(x2 - x1);
@@ -478,10 +482,10 @@ const PointHelper = {
478
482
  return sqrt$2(x * x + y * y);
479
483
  },
480
484
  getMinDistanceFrom(x1, y1, x2, y2, x3, y3) {
481
- return min$1(P$5.getDistanceFrom(x1, y1, x2, y2), P$5.getDistanceFrom(x2, y2, x3, y3));
485
+ return min$1(getDistanceFrom(x1, y1, x2, y2), getDistanceFrom(x2, y2, x3, y3));
482
486
  },
483
487
  getAngle(t, to) {
484
- return P$5.getAtan2(t, to) / OneRadian;
488
+ return getAtan2(t, to) / OneRadian;
485
489
  },
486
490
  getRotation(t, origin, to, toOrigin) {
487
491
  if (!toOrigin)
@@ -499,15 +503,19 @@ const PointHelper = {
499
503
  getAtan2(t, to) {
500
504
  return atan2$2(to.y - t.y, to.x - t.x);
501
505
  },
502
- getDistancePoint(t, to, distance) {
503
- const r = P$5.getAtan2(t, to);
504
- return { x: t.x + cos$2(r) * distance, y: t.y + sin$2(r) * distance };
506
+ getDistancePoint(t, to, distance, changeTo) {
507
+ const r = getAtan2(t, to);
508
+ to = changeTo ? to : {};
509
+ to.x = t.x + cos$2(r) * distance;
510
+ to.y = t.y + sin$2(r) * distance;
511
+ return to;
505
512
  },
506
513
  reset(t) {
507
514
  P$5.reset(t);
508
515
  }
509
516
  };
510
517
  const P$5 = PointHelper;
518
+ const { getDistanceFrom, copy: copy$6, getAtan2 } = P$5;
511
519
 
512
520
  class Point {
513
521
  constructor(x, y) {
@@ -561,8 +569,8 @@ class Point {
561
569
  getDistance(to) {
562
570
  return PointHelper.getDistance(this, to);
563
571
  }
564
- getDistancePoint(to, distance) {
565
- return new Point(PointHelper.getDistancePoint(this, to, distance));
572
+ getDistancePoint(to, distance, changeTo) {
573
+ return new Point(PointHelper.getDistancePoint(this, to, distance, changeTo));
566
574
  }
567
575
  getAngle(to) {
568
576
  return PointHelper.getAngle(this, to);
@@ -713,7 +721,7 @@ const TwoPointBoundsHelper = {
713
721
  };
714
722
  const { addPoint: addPoint$3 } = TwoPointBoundsHelper;
715
723
 
716
- const { tempPointBounds: tempPointBounds$1, setPoint: setPoint$2, addPoint: addPoint$2, toBounds: toBounds$1 } = TwoPointBoundsHelper;
724
+ const { tempPointBounds: tempPointBounds$1, setPoint: setPoint$2, addPoint: addPoint$2, toBounds: toBounds$2 } = TwoPointBoundsHelper;
717
725
  const { toOuterPoint: toOuterPoint$1 } = MatrixHelper;
718
726
  const { float } = MathHelper;
719
727
  const { floor, ceil: ceil$1 } = Math;
@@ -824,7 +832,7 @@ const BoundsHelper = {
824
832
  point.x = t.x;
825
833
  toOuterPoint$1(matrix, point, toPoint$1);
826
834
  addPoint$2(tempPointBounds$1, toPoint$1.x, toPoint$1.y);
827
- toBounds$1(tempPointBounds$1, to);
835
+ toBounds$2(tempPointBounds$1, to);
828
836
  }
829
837
  },
830
838
  toInnerOf(t, matrix, to) {
@@ -908,7 +916,7 @@ const BoundsHelper = {
908
916
  },
909
917
  setPoints(t, points) {
910
918
  points.forEach((point, index) => index === 0 ? setPoint$2(tempPointBounds$1, point.x, point.y) : addPoint$2(tempPointBounds$1, point.x, point.y));
911
- toBounds$1(tempPointBounds$1, t);
919
+ toBounds$2(tempPointBounds$1, t);
912
920
  },
913
921
  getPoints(t) {
914
922
  const { x, y, width, height } = t;
@@ -1433,6 +1441,8 @@ class LeafData {
1433
1441
  if (value !== undefined)
1434
1442
  return value;
1435
1443
  }
1444
+ if (name === 'path' && !this.__pathInputed)
1445
+ return;
1436
1446
  return this['_' + name];
1437
1447
  }
1438
1448
  __removeInput(name) {
@@ -1446,6 +1456,8 @@ class LeafData {
1446
1456
  if (key[0] !== '_') {
1447
1457
  value = this['_' + key];
1448
1458
  if (value !== undefined) {
1459
+ if (key === 'path' && !this.__pathInputed)
1460
+ continue;
1449
1461
  inputValue = __input ? __input[key] : undefined;
1450
1462
  data[key] = (inputValue === undefined) ? value : inputValue;
1451
1463
  }
@@ -1475,6 +1487,9 @@ class LeafData {
1475
1487
  t.__single = true;
1476
1488
  }
1477
1489
  }
1490
+ __removeNaturalSize() {
1491
+ this.__naturalWidth = this.__naturalHeight = undefined;
1492
+ }
1478
1493
  destroy() {
1479
1494
  this.__input = this.__middle = null;
1480
1495
  }
@@ -1962,7 +1977,7 @@ class LeaferCanvasBase extends Canvas {
1962
1977
  this.setStrokeOptions(options);
1963
1978
  }
1964
1979
  setStrokeOptions(options) {
1965
- this.strokeCap = options.strokeCap;
1980
+ this.strokeCap = options.strokeCap === 'none' ? 'butt' : options.strokeCap;
1966
1981
  this.strokeJoin = options.strokeJoin;
1967
1982
  this.dashPattern = options.dashPattern;
1968
1983
  this.dashOffset = options.dashOffset;
@@ -2785,22 +2800,12 @@ const PathCommandDataHelper = {
2785
2800
  }
2786
2801
  },
2787
2802
  drawEllipse(data, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise) {
2788
- if (rotation === undefined)
2789
- rotation = 0;
2790
- if (startAngle === undefined)
2791
- startAngle = 0;
2792
- if (endAngle === undefined)
2793
- endAngle = 360;
2794
- BezierHelper.ellipse(null, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise, null, null, startPoint);
2803
+ BezierHelper.ellipse(null, x, y, radiusX, radiusY, rotation === undefined ? 0 : rotation, startAngle === undefined ? 0 : startAngle, endAngle === undefined ? 360 : endAngle, anticlockwise, null, null, startPoint);
2795
2804
  data.push(M$3, startPoint.x, startPoint.y);
2796
2805
  ellipse$2(data, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise);
2797
2806
  },
2798
2807
  drawArc(data, x, y, radius, startAngle, endAngle, anticlockwise) {
2799
- if (startAngle === undefined)
2800
- startAngle = 0;
2801
- if (endAngle === undefined)
2802
- endAngle = 360;
2803
- BezierHelper.arc(null, x, y, radius, startAngle, endAngle, anticlockwise, null, null, startPoint);
2808
+ BezierHelper.arc(null, x, y, radius, startAngle === undefined ? 0 : startAngle, endAngle === undefined ? 360 : endAngle, anticlockwise, null, null, startPoint);
2804
2809
  data.push(M$3, startPoint.x, startPoint.y);
2805
2810
  arc$2(data, x, y, radius, startAngle, endAngle, anticlockwise);
2806
2811
  },
@@ -2812,68 +2817,70 @@ const { ellipse: ellipse$2, arc: arc$2 } = PathCommandDataHelper;
2812
2817
 
2813
2818
  const { moveTo, lineTo, quadraticCurveTo, bezierCurveTo, closePath, beginPath, rect, roundRect: roundRect$1, ellipse: ellipse$1, arc: arc$1, arcTo: arcTo$2, drawEllipse, drawArc, drawPoints } = PathCommandDataHelper;
2814
2819
  class PathCreator {
2820
+ set path(value) { this.__path = value; }
2821
+ get path() { return this.__path; }
2815
2822
  constructor(path) {
2816
2823
  if (path) {
2817
- this.path = typeof path === 'string' ? PathHelper.parse(path) : path;
2824
+ this.__path = typeof path === 'string' ? PathHelper.parse(path) : path;
2818
2825
  }
2819
2826
  else {
2820
- this.path = [];
2827
+ this.__path = [];
2821
2828
  }
2822
2829
  }
2823
2830
  beginPath() {
2824
- beginPath(this.path);
2831
+ beginPath(this.__path);
2825
2832
  return this;
2826
2833
  }
2827
2834
  moveTo(x, y) {
2828
- moveTo(this.path, x, y);
2835
+ moveTo(this.__path, x, y);
2829
2836
  return this;
2830
2837
  }
2831
2838
  lineTo(x, y) {
2832
- lineTo(this.path, x, y);
2839
+ lineTo(this.__path, x, y);
2833
2840
  return this;
2834
2841
  }
2835
2842
  bezierCurveTo(x1, y1, x2, y2, x, y) {
2836
- bezierCurveTo(this.path, x1, y1, x2, y2, x, y);
2843
+ bezierCurveTo(this.__path, x1, y1, x2, y2, x, y);
2837
2844
  return this;
2838
2845
  }
2839
2846
  quadraticCurveTo(x1, y1, x, y) {
2840
- quadraticCurveTo(this.path, x1, y1, x, y);
2847
+ quadraticCurveTo(this.__path, x1, y1, x, y);
2841
2848
  return this;
2842
2849
  }
2843
2850
  closePath() {
2844
- closePath(this.path);
2851
+ closePath(this.__path);
2845
2852
  return this;
2846
2853
  }
2847
2854
  rect(x, y, width, height) {
2848
- rect(this.path, x, y, width, height);
2855
+ rect(this.__path, x, y, width, height);
2849
2856
  return this;
2850
2857
  }
2851
2858
  roundRect(x, y, width, height, cornerRadius) {
2852
- roundRect$1(this.path, x, y, width, height, cornerRadius);
2859
+ roundRect$1(this.__path, x, y, width, height, cornerRadius);
2853
2860
  return this;
2854
2861
  }
2855
2862
  ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise) {
2856
- ellipse$1(this.path, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise);
2863
+ ellipse$1(this.__path, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise);
2857
2864
  return this;
2858
2865
  }
2859
2866
  arc(x, y, radius, startAngle, endAngle, anticlockwise) {
2860
- arc$1(this.path, x, y, radius, startAngle, endAngle, anticlockwise);
2867
+ arc$1(this.__path, x, y, radius, startAngle, endAngle, anticlockwise);
2861
2868
  return this;
2862
2869
  }
2863
2870
  arcTo(x1, y1, x2, y2, radius) {
2864
- arcTo$2(this.path, x1, y1, x2, y2, radius);
2871
+ arcTo$2(this.__path, x1, y1, x2, y2, radius);
2865
2872
  return this;
2866
2873
  }
2867
2874
  drawEllipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise) {
2868
- drawEllipse(this.path, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise);
2875
+ drawEllipse(this.__path, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise);
2869
2876
  return this;
2870
2877
  }
2871
2878
  drawArc(x, y, radius, startAngle, endAngle, anticlockwise) {
2872
- drawArc(this.path, x, y, radius, startAngle, endAngle, anticlockwise);
2879
+ drawArc(this.__path, x, y, radius, startAngle, endAngle, anticlockwise);
2873
2880
  return this;
2874
2881
  }
2875
2882
  drawPoints(points, curve, close) {
2876
- drawPoints(this.path, points, curve, close);
2883
+ drawPoints(this.__path, points, curve, close);
2877
2884
  return this;
2878
2885
  }
2879
2886
  }
@@ -2951,7 +2958,7 @@ const PathDrawer = {
2951
2958
 
2952
2959
  const { M: M$1, L: L$2, C: C$1, Q, Z: Z$1, N, D, X, G, F, O, P, U } = PathCommandMap;
2953
2960
  const { toTwoPointBounds, toTwoPointBoundsByQuadraticCurve, arcTo: arcTo$1, arc, ellipse } = BezierHelper;
2954
- const { addPointBounds, copy: copy$3, addPoint, setPoint, addBounds, toBounds } = TwoPointBoundsHelper;
2961
+ const { addPointBounds, copy: copy$3, addPoint, setPoint, addBounds, toBounds: toBounds$1 } = TwoPointBoundsHelper;
2955
2962
  const debug$2 = Debug.get('PathBounds');
2956
2963
  let radius, radiusX, radiusY;
2957
2964
  const tempPointBounds = {};
@@ -2960,7 +2967,7 @@ const setEndPoint = {};
2960
2967
  const PathBounds = {
2961
2968
  toBounds(data, setBounds) {
2962
2969
  PathBounds.toTwoPointBounds(data, setPointBounds);
2963
- toBounds(setPointBounds, setBounds);
2970
+ toBounds$1(setPointBounds, setBounds);
2964
2971
  },
2965
2972
  toTwoPointBounds(data, setPointBounds) {
2966
2973
  if (!data || !data.length)
@@ -3622,9 +3629,35 @@ function boundsType(defaultValue) {
3622
3629
  defineLeafAttr(target, key, defaultValue, {
3623
3630
  set(value) {
3624
3631
  this.__setAttr(key, value);
3625
- this.__layout.boxChanged || this.__layout.boxChange();
3626
- if (this.__hasAutoLayout)
3627
- this.__layout.matrixChanged || this.__layout.matrixChange();
3632
+ doBoundsType(this);
3633
+ }
3634
+ });
3635
+ };
3636
+ }
3637
+ function naturalBoundsType(defaultValue) {
3638
+ return (target, key) => {
3639
+ defineLeafAttr(target, key, defaultValue, {
3640
+ set(value) {
3641
+ this.__setAttr(key, value);
3642
+ doBoundsType(this);
3643
+ this.__.__removeNaturalSize();
3644
+ }
3645
+ });
3646
+ };
3647
+ }
3648
+ function doBoundsType(leaf) {
3649
+ leaf.__layout.boxChanged || leaf.__layout.boxChange();
3650
+ if (leaf.__hasAutoLayout)
3651
+ leaf.__layout.matrixChanged || leaf.__layout.matrixChange();
3652
+ }
3653
+ function pathInputType(defaultValue) {
3654
+ return (target, key) => {
3655
+ defineLeafAttr(target, key, defaultValue, {
3656
+ set(value) {
3657
+ if (this.__.__pathInputed !== 2)
3658
+ this.__.__pathInputed = value ? 1 : 0;
3659
+ this.__setAttr(key, value);
3660
+ doBoundsType(this);
3628
3661
  }
3629
3662
  });
3630
3663
  };
@@ -3635,11 +3668,14 @@ function affectStrokeBoundsType(defaultValue) {
3635
3668
  defineLeafAttr(target, key, defaultValue, {
3636
3669
  set(value) {
3637
3670
  this.__setAttr(key, value);
3638
- this.__layout.strokeChanged || this.__layout.strokeChange();
3671
+ doStrokeType(this);
3639
3672
  }
3640
3673
  });
3641
3674
  };
3642
3675
  }
3676
+ function doStrokeType(leaf) {
3677
+ leaf.__layout.strokeChanged || leaf.__layout.strokeChange();
3678
+ }
3643
3679
  const strokeType = affectStrokeBoundsType;
3644
3680
  function affectRenderBoundsType(defaultValue) {
3645
3681
  return (target, key) => {
@@ -4259,6 +4295,7 @@ class ChildEvent extends Event {
4259
4295
  }
4260
4296
  ChildEvent.ADD = 'child.add';
4261
4297
  ChildEvent.REMOVE = 'child.remove';
4298
+ ChildEvent.DESTROY = 'child.destroy';
4262
4299
 
4263
4300
  class PropertyEvent extends Event {
4264
4301
  constructor(type, target, attrName, oldValue, newValue) {
@@ -4706,6 +4743,7 @@ const WaitHelper = {
4706
4743
  const { updateMatrix, updateAllMatrix, hasParentAutoLayout } = LeafHelper;
4707
4744
  const { updateBounds } = BranchHelper;
4708
4745
  const { toOuterOf: toOuterOf$1, copyAndSpread } = BoundsHelper;
4746
+ const { toBounds } = PathBounds;
4709
4747
  const LeafBounds = {
4710
4748
  __updateWorldBounds() {
4711
4749
  toOuterOf$1(this.__layout.renderBounds, this.__world, this.__world);
@@ -4717,7 +4755,8 @@ const LeafBounds = {
4717
4755
  __updateLocalBounds() {
4718
4756
  const layout = this.__layout;
4719
4757
  if (layout.boxChanged) {
4720
- this.__updatePath();
4758
+ if (!this.__.__pathInputed)
4759
+ this.__updatePath();
4721
4760
  this.__updateRenderPath();
4722
4761
  this.__updateBoxBounds();
4723
4762
  layout.boxChanged = false;
@@ -4784,11 +4823,16 @@ const LeafBounds = {
4784
4823
  },
4785
4824
  __updateBoxBounds() {
4786
4825
  const b = this.__layout.boxBounds;
4787
- const { width, height } = this.__;
4788
- b.x = 0;
4789
- b.y = 0;
4790
- b.width = width;
4791
- b.height = height;
4826
+ const data = this.__;
4827
+ if (data.__pathInputed) {
4828
+ toBounds(data.__pathForRender, b);
4829
+ }
4830
+ else {
4831
+ b.x = 0;
4832
+ b.y = 0;
4833
+ b.width = data.width;
4834
+ b.height = data.height;
4835
+ }
4792
4836
  },
4793
4837
  __updateAutoLayout() {
4794
4838
  this.__layout.matrixChanged = true;
@@ -4917,6 +4961,9 @@ exports.Leaf = class Leaf {
4917
4961
  get innerName() { return this.__.name || this.tag + this.innerId; }
4918
4962
  get __DataProcessor() { return LeafData; }
4919
4963
  get __LayoutProcessor() { return LeafLayout; }
4964
+ get isLeafer() { return false; }
4965
+ get isBranch() { return false; }
4966
+ get isBranchLeaf() { return false; }
4920
4967
  get __localMatrix() { return this.__local || this.__layout; }
4921
4968
  get __localBoxBounds() { return this.__local || this.__layout; }
4922
4969
  get worldTransform() { return this.__layout.getTransform('world'); }
@@ -4930,6 +4977,7 @@ exports.Leaf = class Leaf {
4930
4977
  get __worldFlipped() { return this.__world.scaleX < 0 || this.__world.scaleY < 0; }
4931
4978
  get __onlyHitMask() { return this.__hasMask && !this.__.hitChildren; }
4932
4979
  get __ignoreHitWorld() { return (this.__hasMask || this.__hasEraser) && this.__.hitChildren; }
4980
+ get pathInputed() { return !!this.__.__pathInputed; }
4933
4981
  constructor(data) {
4934
4982
  this.innerId = create(LEAF);
4935
4983
  this.reset(data);
@@ -4981,7 +5029,7 @@ exports.Leaf = class Leaf {
4981
5029
  }
4982
5030
  }
4983
5031
  set(_data) { }
4984
- get() { return undefined; }
5032
+ get(_name) { return undefined; }
4985
5033
  toJSON() {
4986
5034
  return this.__.__getInputData();
4987
5035
  }
@@ -5198,10 +5246,13 @@ exports.Leaf = class Leaf {
5198
5246
  }
5199
5247
  destroy() {
5200
5248
  if (!this.destroyed) {
5201
- if (this.parent)
5249
+ const { parent } = this;
5250
+ if (parent)
5202
5251
  this.remove();
5203
5252
  if (this.children)
5204
5253
  this.removeAll(true);
5254
+ if (this.hasEvent(ChildEvent.DESTROY))
5255
+ this.emitEvent(new ChildEvent(ChildEvent.DESTROY, this, parent));
5205
5256
  this.__.destroy();
5206
5257
  this.__layout.destroy();
5207
5258
  this.__captureMap = this.__bubbleMap = this.__parentWait = this.__leaferWait = null;
@@ -5221,11 +5272,6 @@ const { setListWithFn } = BoundsHelper;
5221
5272
  const { sort } = BranchHelper;
5222
5273
  const { localBoxBounds, localStrokeBounds, localRenderBounds, maskLocalBoxBounds, maskLocalStrokeBounds, maskLocalRenderBounds } = LeafBoundsHelper;
5223
5274
  exports.Branch = class Branch extends exports.Leaf {
5224
- constructor() {
5225
- super();
5226
- this.isBranch = true;
5227
- this.children = [];
5228
- }
5229
5275
  __updateStrokeSpread() {
5230
5276
  const { children } = this;
5231
5277
  for (let i = 0, len = children.length; i < len; i++) {
@@ -5265,6 +5311,8 @@ exports.Branch = class Branch extends exports.Leaf {
5265
5311
  }
5266
5312
  }
5267
5313
  add(child, index) {
5314
+ if (child === this)
5315
+ return;
5268
5316
  if (child.parent)
5269
5317
  child.parent.remove(child);
5270
5318
  child.parent = this;
@@ -5581,6 +5629,8 @@ exports.dataType = dataType;
5581
5629
  exports.defineDataProcessor = defineDataProcessor;
5582
5630
  exports.defineKey = defineKey;
5583
5631
  exports.defineLeafAttr = defineLeafAttr;
5632
+ exports.doBoundsType = doBoundsType;
5633
+ exports.doStrokeType = doStrokeType;
5584
5634
  exports.eraserType = eraserType;
5585
5635
  exports.getBoundsData = getBoundsData;
5586
5636
  exports.getDescriptor = getDescriptor;
@@ -5589,7 +5639,9 @@ exports.getPointData = getPointData;
5589
5639
  exports.hitType = hitType;
5590
5640
  exports.layoutProcessor = layoutProcessor;
5591
5641
  exports.maskType = maskType;
5642
+ exports.naturalBoundsType = naturalBoundsType;
5592
5643
  exports.opacityType = opacityType;
5644
+ exports.pathInputType = pathInputType;
5593
5645
  exports.pathType = pathType;
5594
5646
  exports.positionType = positionType;
5595
5647
  exports.registerUI = registerUI;