leafer-draw 1.0.5 → 1.0.6

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.
@@ -552,6 +552,12 @@ const PointHelper = {
552
552
  to.y = t.y + sin$4(r) * distance;
553
553
  return to;
554
554
  },
555
+ toNumberPoints(originPoints) {
556
+ let points = originPoints;
557
+ if (typeof originPoints[0] === 'object')
558
+ points = [], originPoints.forEach(p => points.push(p.x, p.y));
559
+ return points;
560
+ },
555
561
  reset(t) {
556
562
  P$5.reset(t);
557
563
  }
@@ -1542,7 +1548,10 @@ const { assign } = DataHelper;
1542
1548
 
1543
1549
  class LeafData {
1544
1550
  get __useNaturalRatio() { return true; }
1545
- get __isLinePath() { return this.path && this.path.length === 6; }
1551
+ get __isLinePath() {
1552
+ const { path } = this;
1553
+ return path && path.length === 6 && path[0] === 1;
1554
+ }
1546
1555
  get __blendMode() {
1547
1556
  if (this.eraser && this.eraser !== 'path')
1548
1557
  return 'destination-out';
@@ -2305,11 +2314,12 @@ const RectHelper = {
2305
2314
 
2306
2315
  const { sin: sin$3, cos: cos$3, atan2: atan2$1, ceil: ceil$1, abs: abs$3, PI: PI$2, sqrt: sqrt$1, pow } = Math;
2307
2316
  const { setPoint: setPoint$2, addPoint: addPoint$2 } = TwoPointBoundsHelper;
2308
- const { set } = PointHelper;
2317
+ const { set, toNumberPoints } = PointHelper;
2309
2318
  const { M: M$5, L: L$6, C: C$5, Q: Q$4, Z: Z$5 } = PathCommandMap;
2310
2319
  const tempPoint$2 = {};
2311
2320
  const BezierHelper = {
2312
- points(data, points, curve, close) {
2321
+ points(data, originPoints, curve, close) {
2322
+ let points = toNumberPoints(originPoints);
2313
2323
  data.push(M$5, points[0], points[1]);
2314
2324
  if (curve && points.length > 5) {
2315
2325
  let aX, aY, bX, bY, cX, cY, c1X, c1Y, c2X, c2Y;
@@ -2818,6 +2828,27 @@ const PathConvert = {
2818
2828
  }
2819
2829
  return data;
2820
2830
  },
2831
+ objectToCanvasData(list) {
2832
+ const data = [];
2833
+ list.forEach(item => {
2834
+ switch (item.name) {
2835
+ case 'M':
2836
+ data.push(M$4, item.x, item.y);
2837
+ break;
2838
+ case 'L':
2839
+ data.push(L$5, item.x, item.y);
2840
+ break;
2841
+ case 'C':
2842
+ data.push(C$4, item.x1, item.y1, item.x2, item.y2, item.x, item.y);
2843
+ break;
2844
+ case 'Q':
2845
+ data.push(Q$3, item.x1, item.y1, item.x, item.y);
2846
+ break;
2847
+ case 'Z': data.push(Z$4);
2848
+ }
2849
+ });
2850
+ return data;
2851
+ },
2821
2852
  copyData(data, old, index, count) {
2822
2853
  for (let i = index, end = index + count; i < end; i++) {
2823
2854
  data.push(old[i]);
@@ -5159,7 +5190,7 @@ const LeafRender = {
5159
5190
  if (this.__worldOpacity) {
5160
5191
  canvas.setWorld(this.__nowWorld = this.__getNowWorld(options));
5161
5192
  this.__drawRenderPath(canvas);
5162
- this.__.windingRule ? canvas.clip(this.__.windingRule) : canvas.clip();
5193
+ this.windingRule ? canvas.clip(this.windingRule) : canvas.clip();
5163
5194
  }
5164
5195
  },
5165
5196
  __updateWorldOpacity() {
@@ -5676,11 +5707,17 @@ let Branch = class Branch extends Leaf {
5676
5707
  add(child, index) {
5677
5708
  if (child === this)
5678
5709
  return;
5679
- child.__ || (child = UICreator.get(child.tag, child));
5710
+ const noIndex = index === undefined;
5711
+ if (!child.__) {
5712
+ if (child instanceof Array)
5713
+ return child.forEach(item => { this.add(item, index); noIndex || index++; });
5714
+ else
5715
+ child = UICreator.get(child.tag, child);
5716
+ }
5680
5717
  if (child.parent)
5681
5718
  child.parent.remove(child);
5682
5719
  child.parent = this;
5683
- index === undefined ? this.children.push(child) : this.children.splice(index, 0, child);
5720
+ noIndex ? this.children.push(child) : this.children.splice(index, 0, child);
5684
5721
  if (child.isBranch)
5685
5722
  this.__.__childBranchNumber = (this.__.__childBranchNumber || 0) + 1;
5686
5723
  child.__layout.boxChanged || child.__layout.boxChange();
@@ -5694,9 +5731,7 @@ let Branch = class Branch extends Leaf {
5694
5731
  }
5695
5732
  this.__layout.affectChildrenSort && this.__layout.childrenSortChange();
5696
5733
  }
5697
- addMany(...children) {
5698
- children.forEach(child => this.add(child));
5699
- }
5734
+ addMany(...children) { this.add(children); }
5700
5735
  remove(child, destroy) {
5701
5736
  if (child) {
5702
5737
  if (child.__) {
@@ -5924,7 +5959,7 @@ class LeafLevelList {
5924
5959
  }
5925
5960
  }
5926
5961
 
5927
- const version = "1.0.5";
5962
+ const version = "1.0.6";
5928
5963
 
5929
5964
  const debug$5 = Debug.get('LeaferCanvas');
5930
5965
  class LeaferCanvas extends LeaferCanvasBase {
@@ -6082,7 +6117,7 @@ class LeaferCanvas extends LeaferCanvasBase {
6082
6117
  const oldSize = {};
6083
6118
  DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs);
6084
6119
  this.resize(size);
6085
- if (this.width !== undefined)
6120
+ if (this.resizeListener && this.width !== undefined)
6086
6121
  this.resizeListener(new ResizeEvent(size, oldSize));
6087
6122
  }
6088
6123
  unrealCanvas() {
@@ -6806,6 +6841,13 @@ function zoomLayerType() {
6806
6841
 
6807
6842
  const TextConvert = {};
6808
6843
  const ColorConvert = {};
6844
+ const UnitConvert = {
6845
+ number(value, percentRefer) {
6846
+ if (typeof value === 'object')
6847
+ return value.type === 'percent' ? value.value * percentRefer : value.value;
6848
+ return value;
6849
+ }
6850
+ };
6809
6851
  const PathArrow = {};
6810
6852
  const Paint = {};
6811
6853
  const PaintImage = {};
@@ -6826,7 +6868,7 @@ const Transition = {
6826
6868
  }
6827
6869
  };
6828
6870
 
6829
- const { parse } = PathConvert;
6871
+ const { parse, objectToCanvasData } = PathConvert;
6830
6872
  const emptyPaint = {};
6831
6873
  const debug$2 = Debug.get('UIData');
6832
6874
  class UIData extends LeafData {
@@ -6840,10 +6882,11 @@ class UIData extends LeafData {
6840
6882
  scaleX = -scaleX;
6841
6883
  return scaleX > 1 ? strokeWidth / scaleX : strokeWidth;
6842
6884
  }
6843
- else {
6885
+ else
6844
6886
  return strokeWidth;
6845
- }
6846
6887
  }
6888
+ get __hasStroke() { return this.stroke && this.strokeWidth; }
6889
+ get __clipAfterFill() { return (this.cornerRadius || this.__pathInputed); }
6847
6890
  get __autoWidth() { return !this._width; }
6848
6891
  get __autoHeight() { return !this._height; }
6849
6892
  get __autoSide() { return !this._width || !this._height; }
@@ -6860,9 +6903,8 @@ class UIData extends LeafData {
6860
6903
  this.__leaf.scaleX *= -1;
6861
6904
  debug$2.warn('width < 0, instead -scaleX ', this);
6862
6905
  }
6863
- else {
6906
+ else
6864
6907
  this._width = value;
6865
- }
6866
6908
  }
6867
6909
  setHeight(value) {
6868
6910
  if (value < 0) {
@@ -6870,9 +6912,8 @@ class UIData extends LeafData {
6870
6912
  this.__leaf.scaleY *= -1;
6871
6913
  debug$2.warn('height < 0, instead -scaleY', this);
6872
6914
  }
6873
- else {
6915
+ else
6874
6916
  this._height = value;
6875
- }
6876
6917
  }
6877
6918
  setFill(value) {
6878
6919
  if (this.__naturalWidth)
@@ -6913,9 +6954,10 @@ class UIData extends LeafData {
6913
6954
  }
6914
6955
  }
6915
6956
  setPath(value) {
6916
- if (typeof value === 'string') {
6957
+ const isString = typeof value === 'string';
6958
+ if (isString || (value && typeof value[0] === 'object')) {
6917
6959
  this.__setInput('path', value);
6918
- this._path = parse(value);
6960
+ this._path = isString ? parse(value) : objectToCanvasData(value);
6919
6961
  }
6920
6962
  else {
6921
6963
  if (this.__input)
@@ -6930,12 +6972,8 @@ class UIData extends LeafData {
6930
6972
  value = value.filter((item) => item.visible !== false);
6931
6973
  this._shadow = value.length ? value : null;
6932
6974
  }
6933
- else if (value) {
6934
- this._shadow = value.visible === false ? null : [value];
6935
- }
6936
- else {
6937
- this._shadow = null;
6938
- }
6975
+ else
6976
+ this._shadow = value && value.visible !== false ? [value] : null;
6939
6977
  }
6940
6978
  setInnerShadow(value) {
6941
6979
  this.__setInput('innerShadow', value);
@@ -6944,12 +6982,8 @@ class UIData extends LeafData {
6944
6982
  value = value.filter((item) => item.visible !== false);
6945
6983
  this._innerShadow = value.length ? value : null;
6946
6984
  }
6947
- else if (value) {
6948
- this._innerShadow = value.visible === false ? null : [value];
6949
- }
6950
- else {
6951
- this._innerShadow = null;
6952
- }
6985
+ else
6986
+ this._innerShadow = value && value.visible !== false ? [value] : null;
6953
6987
  }
6954
6988
  __computePaint() {
6955
6989
  const { fill, stroke } = this.__input;
@@ -6960,24 +6994,19 @@ class UIData extends LeafData {
6960
6994
  this.__needComputePaint = false;
6961
6995
  }
6962
6996
  }
6963
- const UnitConvert = {
6964
- number(value, percentRefer) {
6965
- if (typeof value === 'object')
6966
- return value.type === 'percent' ? value.value * percentRefer : value.value;
6967
- return value;
6968
- }
6969
- };
6970
6997
 
6971
6998
  class GroupData extends UIData {
6972
6999
  }
6973
7000
 
6974
7001
  class BoxData extends GroupData {
6975
7002
  get __boxStroke() { return !this.__pathInputed; }
7003
+ get __drawAfterFill() { return this.overflow === 'hide' && this.__clipAfterFill && this.__leaf.children.length; }
7004
+ get __clipAfterFill() { return this.__leaf.isOverflow || super.__clipAfterFill; }
6976
7005
  }
6977
7006
 
6978
7007
  class LeaferData extends GroupData {
6979
- __getInputData() {
6980
- const data = super.__getInputData();
7008
+ __getInputData(names, options) {
7009
+ const data = super.__getInputData(names, options);
6981
7010
  canvasSizeAttrs.forEach(key => delete data[key]);
6982
7011
  return data;
6983
7012
  }
@@ -7004,6 +7033,7 @@ class StarData extends UIData {
7004
7033
  }
7005
7034
 
7006
7035
  class PathData extends UIData {
7036
+ get __pathInputed() { return 2; }
7007
7037
  }
7008
7038
 
7009
7039
  class PenData extends GroupData {
@@ -7050,16 +7080,18 @@ class ImageData extends RectData {
7050
7080
  delete data.fill;
7051
7081
  return data;
7052
7082
  }
7053
- __getInputData() {
7054
- const data = super.__getInputData();
7083
+ __getInputData(names, options) {
7084
+ const data = super.__getInputData(names, options);
7055
7085
  delete data.fill;
7056
7086
  return data;
7057
7087
  }
7058
7088
  }
7059
7089
 
7060
7090
  class CanvasData extends RectData {
7061
- __getInputData() {
7062
- const data = super.__getInputData();
7091
+ get __isCanvas() { return true; }
7092
+ get __drawAfterFill() { return true; }
7093
+ __getInputData(names, options) {
7094
+ const data = super.__getInputData(names, options);
7063
7095
  data.url = this.__leaf.canvas.toDataURL('image/png');
7064
7096
  return data;
7065
7097
  }
@@ -7086,16 +7118,12 @@ const UIBounds = {
7086
7118
  let width = 0;
7087
7119
  const { shadow, innerShadow, blur, backgroundBlur } = this.__;
7088
7120
  if (shadow)
7089
- shadow.forEach(item => {
7090
- width = Math.max(width, Math.max(Math.abs(item.y), Math.abs(item.x)) + (item.spread > 0 ? item.spread : 0) + item.blur * 1.5);
7091
- });
7121
+ shadow.forEach(item => width = Math.max(width, Math.max(Math.abs(item.y), Math.abs(item.x)) + (item.spread > 0 ? item.spread : 0) + item.blur * 1.5));
7092
7122
  if (blur)
7093
7123
  width = Math.max(width, blur);
7094
7124
  let shapeWidth = width = Math.ceil(width);
7095
7125
  if (innerShadow)
7096
- innerShadow.forEach(item => {
7097
- shapeWidth = Math.max(shapeWidth, Math.max(Math.abs(item.y), Math.abs(item.x)) + (item.spread < 0 ? -item.spread : 0) + item.blur * 1.5);
7098
- });
7126
+ innerShadow.forEach(item => shapeWidth = Math.max(shapeWidth, Math.max(Math.abs(item.y), Math.abs(item.x)) + (item.spread < 0 ? -item.spread : 0) + item.blur * 1.5));
7099
7127
  if (backgroundBlur)
7100
7128
  shapeWidth = Math.max(shapeWidth, backgroundBlur);
7101
7129
  this.__layout.renderShapeSpread = shapeWidth;
@@ -7177,6 +7205,16 @@ const UIRender = {
7177
7205
  if (stroke && !ignoreStroke)
7178
7206
  this.__.__pixelStroke ? Paint.strokes(stroke, this, canvas) : Paint.stroke('#000000', this, canvas);
7179
7207
  }
7208
+ },
7209
+ __drawAfterFill(canvas, options) {
7210
+ if (this.__.__clipAfterFill) {
7211
+ canvas.save();
7212
+ this.windingRule ? canvas.clip(this.windingRule) : canvas.clip();
7213
+ this.__drawContent(canvas, options);
7214
+ canvas.restore();
7215
+ }
7216
+ else
7217
+ this.__drawContent(canvas, options);
7180
7218
  }
7181
7219
  };
7182
7220
  function drawFast(ui, canvas, options) {
@@ -7243,8 +7281,8 @@ let UI = UI_1 = class UI extends Leaf {
7243
7281
  return pen;
7244
7282
  }
7245
7283
  get editConfig() { return undefined; }
7246
- get editOuter() { return this.__.__isLinePath ? 'LineEditTool' : 'EditTool'; }
7247
- get editInner() { return 'PathEditor'; }
7284
+ get editOuter() { return ''; }
7285
+ get editInner() { return ''; }
7248
7286
  constructor(data) {
7249
7287
  super(data);
7250
7288
  }
@@ -7255,9 +7293,8 @@ let UI = UI_1 = class UI extends Leaf {
7255
7293
  Object.assign(this, data);
7256
7294
  this.lockNormalStyle = false;
7257
7295
  }
7258
- else {
7296
+ else
7259
7297
  Object.assign(this, data);
7260
- }
7261
7298
  }
7262
7299
  get(name) {
7263
7300
  return typeof name === 'string' ? this.__.__getInput(name) : this.__.__getInputData(name);
@@ -7559,23 +7596,13 @@ let Group = class Group extends UI {
7559
7596
  if (data.children) {
7560
7597
  const { children } = data;
7561
7598
  delete data.children;
7562
- if (!this.children) {
7563
- this.__setBranch();
7564
- }
7565
- else {
7566
- this.clear();
7567
- }
7599
+ this.children ? this.clear() : this.__setBranch();
7568
7600
  super.set(data, isTemp);
7569
- let child;
7570
- children.forEach(childData => {
7571
- child = childData.__ ? childData : UICreator.get(childData.tag, childData);
7572
- this.add(child);
7573
- });
7601
+ children.forEach(child => this.add(child));
7574
7602
  data.children = children;
7575
7603
  }
7576
- else {
7604
+ else
7577
7605
  super.set(data, isTemp);
7578
- }
7579
7606
  }
7580
7607
  toJSON(options) {
7581
7608
  const data = super.toJSON(options);
@@ -8007,9 +8034,7 @@ let Box = class Box extends Group {
8007
8034
  }
8008
8035
  __updateStrokeSpread() { return 0; }
8009
8036
  __updateRectRenderSpread() { return 0; }
8010
- __updateRenderSpread() {
8011
- return this.__updateRectRenderSpread() || -1;
8012
- }
8037
+ __updateRenderSpread() { return this.__updateRectRenderSpread() || -1; }
8013
8038
  __updateRectBoxBounds() { }
8014
8039
  __updateBoxBounds(_secondLayout) {
8015
8040
  const data = this.__;
@@ -8029,13 +8054,11 @@ let Box = class Box extends Group {
8029
8054
  }
8030
8055
  this.__updateNaturalSize();
8031
8056
  }
8032
- else {
8057
+ else
8033
8058
  this.__updateRectBoxBounds();
8034
- }
8035
8059
  }
8036
- else {
8060
+ else
8037
8061
  this.__updateRectBoxBounds();
8038
- }
8039
8062
  }
8040
8063
  __updateStrokeBounds() { }
8041
8064
  __updateRenderBounds() {
@@ -8045,14 +8068,13 @@ let Box = class Box extends Group {
8045
8068
  super.__updateRenderBounds();
8046
8069
  copy$2(childrenRenderBounds, renderBounds);
8047
8070
  this.__updateRectRenderBounds();
8048
- isOverflow = !includes$1(renderBounds, childrenRenderBounds) || !this.pathInputed || !this.__.cornerRadius;
8071
+ isOverflow = !includes$1(renderBounds, childrenRenderBounds);
8072
+ if (isOverflow && this.__.overflow !== 'hide')
8073
+ add(renderBounds, childrenRenderBounds);
8049
8074
  }
8050
- else {
8075
+ else
8051
8076
  this.__updateRectRenderBounds();
8052
- }
8053
- this.isOverflow !== isOverflow && (this.isOverflow = isOverflow);
8054
- if (!(this.__.__drawAfterFill = this.__.overflow === 'hide') && isOverflow)
8055
- add(renderBounds, childrenRenderBounds);
8077
+ !this.isOverflow !== !isOverflow && (this.isOverflow = isOverflow);
8056
8078
  }
8057
8079
  __updateRectRenderBounds() { }
8058
8080
  __updateRectChange() { }
@@ -8072,20 +8094,9 @@ let Box = class Box extends Group {
8072
8094
  this.__renderGroup(canvas, options);
8073
8095
  }
8074
8096
  }
8075
- __drawAfterFill(canvas, options) {
8076
- const { length } = this.children;
8077
- if (this.isOverflow) {
8078
- canvas.save();
8079
- canvas.clip();
8080
- if (length)
8081
- this.__renderGroup(canvas, options);
8082
- canvas.restore();
8083
- }
8084
- else {
8085
- if (length)
8086
- this.__renderGroup(canvas, options);
8087
- }
8088
- if (this.__.stroke && length) {
8097
+ __drawContent(canvas, options) {
8098
+ this.__renderGroup(canvas, options);
8099
+ if (this.__.__hasStroke) {
8089
8100
  canvas.setWorld(this.__nowWorld);
8090
8101
  this.__drawRenderPath(canvas);
8091
8102
  }
@@ -8249,17 +8260,15 @@ let Line = class Line extends UI {
8249
8260
  if (data.__useArrow)
8250
8261
  PathArrow.addArrows(this, false);
8251
8262
  }
8252
- else {
8263
+ else
8253
8264
  super.__updateRenderPath();
8254
- }
8255
8265
  }
8256
8266
  __updateBoxBounds() {
8257
8267
  if (this.points) {
8258
8268
  toBounds$1(this.__.__pathForRender, this.__layout.boxBounds);
8259
8269
  }
8260
- else {
8270
+ else
8261
8271
  super.__updateBoxBounds();
8262
- }
8263
8272
  }
8264
8273
  };
8265
8274
  __decorate([
@@ -8397,7 +8406,6 @@ let Canvas = class Canvas extends Rect {
8397
8406
  super(data);
8398
8407
  this.canvas = Creator.canvas(this.__);
8399
8408
  this.context = this.canvas.context;
8400
- this.__.__isCanvas = this.__.__drawAfterFill = true;
8401
8409
  if (data && data.url)
8402
8410
  this.drawImage(data.url);
8403
8411
  }
@@ -8410,8 +8418,7 @@ let Canvas = class Canvas extends Rect {
8410
8418
  });
8411
8419
  }
8412
8420
  draw(ui, offset, scale, rotation) {
8413
- ui.__layout.update();
8414
- const matrix = new Matrix(ui.__world).invert();
8421
+ const matrix = new Matrix(ui.worldTransform).invert();
8415
8422
  const m = new Matrix();
8416
8423
  if (offset)
8417
8424
  m.translate(offset.x, offset.y);
@@ -8426,17 +8433,9 @@ let Canvas = class Canvas extends Rect {
8426
8433
  paint() {
8427
8434
  this.forceRender();
8428
8435
  }
8429
- __drawAfterFill(canvas, _options) {
8430
- const { width, height, cornerRadius } = this.__, { view } = this.canvas;
8431
- if (cornerRadius || this.pathInputed) {
8432
- canvas.save();
8433
- canvas.clip();
8434
- canvas.drawImage(view, 0, 0, view.width, view.height, 0, 0, width, height);
8435
- canvas.restore();
8436
- }
8437
- else {
8438
- canvas.drawImage(view, 0, 0, view.width, view.height, 0, 0, width, height);
8439
- }
8436
+ __drawContent(canvas, _options) {
8437
+ const { width, height } = this.__, { view } = this.canvas;
8438
+ canvas.drawImage(view, 0, 0, view.width, view.height, 0, 0, width, height);
8440
8439
  }
8441
8440
  __updateSize() {
8442
8441
  const { canvas } = this;
@@ -8480,7 +8479,6 @@ Canvas = __decorate([
8480
8479
  const { copyAndSpread, includes, isSame: isSame$1, spread, setList } = BoundsHelper;
8481
8480
  let Text = class Text extends UI {
8482
8481
  get __tag() { return 'Text'; }
8483
- get editInner() { return 'TextEditor'; }
8484
8482
  get textDrawData() {
8485
8483
  this.__layout.update();
8486
8484
  return this.__.__textDrawData;
@@ -8646,7 +8644,6 @@ let Path = class Path extends UI {
8646
8644
  get __tag() { return 'Path'; }
8647
8645
  constructor(data) {
8648
8646
  super(data);
8649
- this.__.__pathInputed = 2;
8650
8647
  }
8651
8648
  };
8652
8649
  __decorate([
@@ -9324,7 +9321,7 @@ function checkImage(ui, canvas, paint, allowPaint) {
9324
9321
  }
9325
9322
  if (allowPaint) {
9326
9323
  canvas.save();
9327
- canvas.clip();
9324
+ ui.windingRule ? canvas.clip(ui.windingRule) : canvas.clip();
9328
9325
  if (paint.blendMode)
9329
9326
  canvas.blendMode = paint.blendMode;
9330
9327
  if (data.opacity)