leafer-draw 1.0.9 → 1.1.0

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.
@@ -108,7 +108,7 @@ const MathHelper = {
108
108
  return rotation - oldRotation;
109
109
  },
110
110
  float(num, maxLength) {
111
- const a = maxLength ? pow$1(10, maxLength) : 1000000000000;
111
+ const a = maxLength !== undefined ? pow$1(10, maxLength) : 1000000000000;
112
112
  num = round(num * a) / a;
113
113
  return num === -0 ? 0 : num;
114
114
  },
@@ -1411,14 +1411,13 @@ const UICreator = {
1411
1411
  list: {},
1412
1412
  register(UI) {
1413
1413
  const { __tag: tag } = UI.prototype;
1414
- if (list$1[tag]) {
1414
+ if (list$1[tag])
1415
1415
  debug$d.repeat(tag);
1416
- }
1417
- else {
1418
- list$1[tag] = UI;
1419
- }
1416
+ list$1[tag] = UI;
1420
1417
  },
1421
1418
  get(tag, data, x, y, width, height) {
1419
+ if (!list$1[tag])
1420
+ debug$d.error('not register ' + tag);
1422
1421
  const ui = new list$1[tag](data);
1423
1422
  if (x !== undefined) {
1424
1423
  ui.x = x;
@@ -1442,7 +1441,7 @@ const EventCreator = {
1442
1441
  Object.keys(Event).forEach(key => {
1443
1442
  name = Event[key];
1444
1443
  if (typeof name === 'string')
1445
- nameList[name] ? debug$c.repeat(name) : nameList[name] = Event;
1444
+ nameList[name] && debug$c.repeat(name), nameList[name] = Event;
1446
1445
  });
1447
1446
  },
1448
1447
  changeName(oldName, newName) {
@@ -1644,7 +1643,7 @@ class LeafData {
1644
1643
  const t = this;
1645
1644
  if (t.blendMode === 'pass-through') {
1646
1645
  const leaf = this.__leaf;
1647
- if ((t.opacity < 1 && leaf.isBranch) || leaf.__hasEraser || t.eraser) {
1646
+ if ((t.opacity < 1 && (leaf.isBranch || t.__hasMultiPaint)) || leaf.__hasEraser || t.eraser) {
1648
1647
  t.__single = true;
1649
1648
  }
1650
1649
  else if (t.__single) {
@@ -2042,8 +2041,9 @@ class LeaferCanvasBase extends Canvas$1 {
2042
2041
  takeCanvas = this.getSameCanvas();
2043
2042
  takeCanvas.copyWorld(this);
2044
2043
  }
2045
- DataHelper.copyAttrs(this.size, size, canvasSizeAttrs);
2046
- this.size.pixelRatio || (this.size.pixelRatio = 1);
2044
+ const s = this.size;
2045
+ DataHelper.copyAttrs(s, size, canvasSizeAttrs);
2046
+ canvasSizeAttrs.forEach(key => s[key] || (s[key] = 1));
2047
2047
  this.bounds = new Bounds(0, 0, this.width, this.height);
2048
2048
  if (this.context && !this.unreal) {
2049
2049
  this.updateViewSize();
@@ -2157,6 +2157,17 @@ class LeaferCanvasBase extends Canvas$1 {
2157
2157
  if (!onlyResetTransform)
2158
2158
  this.useWorldTransform();
2159
2159
  }
2160
+ useGrayscaleAlpha(bounds) {
2161
+ this.setTempBounds(bounds, true, true);
2162
+ let alpha, pixel;
2163
+ const { context } = this, imageData = context.getImageData(tempBounds$1.x, tempBounds$1.y, tempBounds$1.width, tempBounds$1.height), { data } = imageData;
2164
+ for (let i = 0, len = data.length; i < len; i += 4) {
2165
+ pixel = data[i] * 0.299 + data[i + 1] * 0.587 + data[i + 2] * 0.114;
2166
+ if (alpha = data[i + 3])
2167
+ data[i + 3] = alpha === 255 ? pixel : alpha * (pixel / 255);
2168
+ }
2169
+ context.putImageData(imageData, tempBounds$1.x, tempBounds$1.y);
2170
+ }
2160
2171
  useMask(maskCanvas, fromBounds, toBounds) {
2161
2172
  this.copyWorld(maskCanvas, fromBounds, toBounds, 'destination-in');
2162
2173
  }
@@ -2167,7 +2178,7 @@ class LeaferCanvasBase extends Canvas$1 {
2167
2178
  if (blendMode)
2168
2179
  this.blendMode = blendMode;
2169
2180
  this.fillStyle = color;
2170
- tempBounds$1.set(bounds).scale(this.pixelRatio);
2181
+ this.setTempBounds(bounds);
2171
2182
  this.fillRect(tempBounds$1.x, tempBounds$1.y, tempBounds$1.width, tempBounds$1.height);
2172
2183
  if (blendMode)
2173
2184
  this.blendMode = 'source-over';
@@ -2176,22 +2187,18 @@ class LeaferCanvasBase extends Canvas$1 {
2176
2187
  if (blendMode)
2177
2188
  this.blendMode = blendMode;
2178
2189
  this.strokeStyle = color;
2179
- tempBounds$1.set(bounds).scale(this.pixelRatio);
2190
+ this.setTempBounds(bounds);
2180
2191
  this.strokeRect(tempBounds$1.x, tempBounds$1.y, tempBounds$1.width, tempBounds$1.height);
2181
2192
  if (blendMode)
2182
2193
  this.blendMode = 'source-over';
2183
2194
  }
2184
2195
  clearWorld(bounds, ceilPixel) {
2185
- tempBounds$1.set(bounds).scale(this.pixelRatio);
2186
- if (ceilPixel)
2187
- tempBounds$1.ceil();
2196
+ this.setTempBounds(bounds, ceilPixel);
2188
2197
  this.clearRect(tempBounds$1.x, tempBounds$1.y, tempBounds$1.width, tempBounds$1.height);
2189
2198
  }
2190
2199
  clipWorld(bounds, ceilPixel) {
2191
2200
  this.beginPath();
2192
- tempBounds$1.set(bounds).scale(this.pixelRatio);
2193
- if (ceilPixel)
2194
- tempBounds$1.ceil();
2201
+ this.setTempBounds(bounds, ceilPixel);
2195
2202
  this.rect(tempBounds$1.x, tempBounds$1.y, tempBounds$1.width, tempBounds$1.height);
2196
2203
  this.clip();
2197
2204
  }
@@ -2199,6 +2206,14 @@ class LeaferCanvasBase extends Canvas$1 {
2199
2206
  const { pixelRatio } = this;
2200
2207
  this.clearRect(0, 0, this.width * pixelRatio + 2, this.height * pixelRatio + 2);
2201
2208
  }
2209
+ setTempBounds(bounds, ceil, intersect) {
2210
+ tempBounds$1.set(bounds);
2211
+ if (intersect)
2212
+ tempBounds$1.intersect(this.bounds);
2213
+ tempBounds$1.scale(this.pixelRatio);
2214
+ if (ceil)
2215
+ tempBounds$1.ceil();
2216
+ }
2202
2217
  isSameSize(size) {
2203
2218
  return this.width === size.width && this.height === size.height && this.pixelRatio === size.pixelRatio;
2204
2219
  }
@@ -4150,20 +4165,17 @@ const LeafHelper = {
4150
4165
  }
4151
4166
  return true;
4152
4167
  },
4153
- moveWorld(t, x, y = 0, isInnerPoint) {
4168
+ moveWorld(t, x, y = 0, isInnerPoint, transition) {
4154
4169
  const local = typeof x === 'object' ? Object.assign({}, x) : { x, y };
4155
4170
  isInnerPoint ? toOuterPoint$1(t.localTransform, local, local, true) : (t.parent && toInnerPoint$1(t.parent.worldTransform, local, local, true));
4156
- L.moveLocal(t, local.x, local.y);
4171
+ L.moveLocal(t, local.x, local.y, transition);
4157
4172
  },
4158
- moveLocal(t, x, y = 0) {
4159
- if (typeof x === 'object') {
4160
- t.x += x.x;
4161
- t.y += x.y;
4162
- }
4163
- else {
4164
- t.x += x;
4165
- t.y += y;
4166
- }
4173
+ moveLocal(t, x, y = 0, transition) {
4174
+ if (typeof x === 'object')
4175
+ y = x.y, x = x.x;
4176
+ x += t.x;
4177
+ y += t.y;
4178
+ transition ? t.animate({ x, y }, transition) : (t.x = x, t.y = y);
4167
4179
  },
4168
4180
  zoomOfWorld(t, origin, scaleX, scaleY, resize) {
4169
4181
  L.zoomOfLocal(t, getTempLocal(t, origin), scaleX, scaleY, resize);
@@ -5212,13 +5224,14 @@ const BranchRender = {
5212
5224
  this.__.__checkSingle();
5213
5225
  },
5214
5226
  __render(canvas, options) {
5227
+ this.__nowWorld = this.__getNowWorld(options);
5215
5228
  if (this.__worldOpacity) {
5216
5229
  if (this.__.__single) {
5217
5230
  if (this.__.eraser === 'path')
5218
5231
  return this.__renderEraser(canvas, options);
5219
5232
  const tempCanvas = canvas.getSameCanvas(false, true);
5220
5233
  this.__renderBranch(tempCanvas, options);
5221
- const nowWorld = this.__getNowWorld(options);
5234
+ const nowWorld = this.__nowWorld;
5222
5235
  canvas.opacity = this.__.opacity;
5223
5236
  canvas.copyWorldByReset(tempCanvas, nowWorld, nowWorld, this.__.__blendMode, true);
5224
5237
  tempCanvas.recycle(nowWorld);
@@ -5542,11 +5555,11 @@ let Leaf = class Leaf {
5542
5555
  transform(matrix, resize) {
5543
5556
  transform(this, matrix, resize);
5544
5557
  }
5545
- move(x, y) {
5546
- moveLocal(this, x, y);
5558
+ move(x, y, transition) {
5559
+ moveLocal(this, x, y, transition);
5547
5560
  }
5548
- moveInner(x, y) {
5549
- moveWorld(this, x, y, true);
5561
+ moveInner(x, y, transition) {
5562
+ moveWorld(this, x, y, true, transition);
5550
5563
  }
5551
5564
  scaleOf(origin, scaleX, scaleY, resize) {
5552
5565
  zoomOfLocal(this, getLocalOrigin(this, origin), scaleX, scaleY, resize);
@@ -5560,8 +5573,8 @@ let Leaf = class Leaf {
5560
5573
  transformWorld(worldTransform, resize) {
5561
5574
  transformWorld(this, worldTransform, resize);
5562
5575
  }
5563
- moveWorld(x, y) {
5564
- moveWorld(this, x, y);
5576
+ moveWorld(x, y, transition) {
5577
+ moveWorld(this, x, y, false, transition);
5565
5578
  }
5566
5579
  scaleOfWorld(worldOrigin, scaleX, scaleY, resize) {
5567
5580
  zoomOfWorld(this, worldOrigin, scaleX, scaleY, resize);
@@ -5959,7 +5972,7 @@ class LeafLevelList {
5959
5972
  }
5960
5973
  }
5961
5974
 
5962
- const version = "1.0.9";
5975
+ const version = "1.1.0";
5963
5976
 
5964
5977
  const debug$5 = Debug.get('LeaferCanvas');
5965
5978
  class LeaferCanvas extends LeaferCanvasBase {
@@ -6601,7 +6614,8 @@ class Renderer {
6601
6614
  this.totalBounds = new Bounds();
6602
6615
  debug$3.log(target.innerName, '--->');
6603
6616
  try {
6604
- target.app.emit(RenderEvent.CHILD_START, target);
6617
+ if (!target.isApp)
6618
+ target.app.emit(RenderEvent.CHILD_START, target);
6605
6619
  this.emitRender(RenderEvent.START);
6606
6620
  this.renderOnce(callback);
6607
6621
  this.emitRender(RenderEvent.END, this.totalBounds);
@@ -6882,6 +6896,12 @@ class UIData extends LeafData {
6882
6896
  return strokeWidth;
6883
6897
  }
6884
6898
  get __hasStroke() { return this.stroke && this.strokeWidth; }
6899
+ get __hasMultiPaint() {
6900
+ const t = this;
6901
+ if ((t.__isFills && t.fill.length > 1) || (t.__isStrokes && t.stroke.length > 1) || t.__useEffect)
6902
+ return true;
6903
+ return t.fill && this.__hasStroke;
6904
+ }
6885
6905
  get __clipAfterFill() { return (this.cornerRadius || this.__pathInputed); }
6886
6906
  get __autoWidth() { return !this._width; }
6887
6907
  get __autoHeight() { return !this._height; }
@@ -7251,9 +7271,8 @@ const RectRender = {
7251
7271
  canvas.strokeRect(half, half, width, height);
7252
7272
  canvas.restore();
7253
7273
  }
7254
- else {
7274
+ else
7255
7275
  canvas.strokeRect(half, half, width, height);
7256
- }
7257
7276
  break;
7258
7277
  case 'outside':
7259
7278
  canvas.strokeRect(-half, -half, width + __strokeWidth, height + __strokeWidth);
@@ -7283,11 +7302,15 @@ let UI = UI_1 = class UI extends Leaf {
7283
7302
  super(data);
7284
7303
  }
7285
7304
  reset(_data) { }
7286
- set(data, isTemp) {
7287
- if (isTemp) {
7288
- this.lockNormalStyle = true;
7289
- Object.assign(this, data);
7290
- this.lockNormalStyle = false;
7305
+ set(data, transition) {
7306
+ if (transition) {
7307
+ if (transition === 'temp') {
7308
+ this.lockNormalStyle = true;
7309
+ Object.assign(this, data);
7310
+ this.lockNormalStyle = false;
7311
+ }
7312
+ else
7313
+ this.animate(data, transition);
7291
7314
  }
7292
7315
  else
7293
7316
  Object.assign(this, data);
@@ -7478,6 +7501,15 @@ __decorate([
7478
7501
  __decorate([
7479
7502
  boundsType(0)
7480
7503
  ], UI.prototype, "padding", void 0);
7504
+ __decorate([
7505
+ boundsType(false)
7506
+ ], UI.prototype, "lockRatio", void 0);
7507
+ __decorate([
7508
+ boundsType()
7509
+ ], UI.prototype, "widthRange", void 0);
7510
+ __decorate([
7511
+ boundsType()
7512
+ ], UI.prototype, "heightRange", void 0);
7481
7513
  __decorate([
7482
7514
  dataType(false)
7483
7515
  ], UI.prototype, "draggable", void 0);
@@ -7588,17 +7620,17 @@ let Group = class Group extends UI {
7588
7620
  if (!this.children)
7589
7621
  this.children = [];
7590
7622
  }
7591
- set(data, isTemp) {
7623
+ set(data, transition) {
7592
7624
  if (data.children) {
7593
7625
  const { children } = data;
7594
7626
  delete data.children;
7595
7627
  this.children ? this.clear() : this.__setBranch();
7596
- super.set(data, isTemp);
7628
+ super.set(data, transition);
7597
7629
  children.forEach(child => this.add(child));
7598
7630
  data.children = children;
7599
7631
  }
7600
7632
  else
7601
- super.set(data, isTemp);
7633
+ super.set(data, transition);
7602
7634
  }
7603
7635
  toJSON(options) {
7604
7636
  const data = super.toJSON(options);
@@ -7711,8 +7743,8 @@ let Leafer = Leafer_1 = class Leafer extends Group {
7711
7743
  }
7712
7744
  onInit() { }
7713
7745
  initType(_type) { }
7714
- set(data) {
7715
- this.waitInit(() => { super.set(data); });
7746
+ set(data, transition) {
7747
+ this.waitInit(() => { super.set(data, transition); });
7716
7748
  }
7717
7749
  start() {
7718
7750
  clearTimeout(this.__startTimer);
@@ -7769,8 +7801,6 @@ let Leafer = Leafer_1 = class Leafer extends Group {
7769
7801
  __onResize(event) {
7770
7802
  this.emitEvent(event);
7771
7803
  DataHelper.copyAttrs(this.__, event, canvasSizeAttrs);
7772
- if (!event.width || !event.height)
7773
- debug$1.warn('w = 0 or h = 0');
7774
7804
  setTimeout(() => { if (this.canvasManager)
7775
7805
  this.canvasManager.clearRecycled(); }, 0);
7776
7806
  }
@@ -8095,9 +8125,6 @@ __decorate([
8095
8125
  __decorate([
8096
8126
  dataType(false)
8097
8127
  ], Box.prototype, "resizeChildren", void 0);
8098
- __decorate([
8099
- dataType(false)
8100
- ], Box.prototype, "textBox", void 0);
8101
8128
  __decorate([
8102
8129
  affectRenderBoundsType('show')
8103
8130
  ], Box.prototype, "overflow", void 0);
@@ -8812,6 +8839,8 @@ function stroke(stroke, ui, canvas) {
8812
8839
  case 'center':
8813
8840
  canvas.setStroke(stroke, __strokeWidth, options);
8814
8841
  canvas.stroke();
8842
+ if (options.__useArrow)
8843
+ strokeArrow(ui, canvas);
8815
8844
  break;
8816
8845
  case 'inside':
8817
8846
  canvas.save();
@@ -8849,6 +8878,8 @@ function strokes(strokes, ui, canvas) {
8849
8878
  case 'center':
8850
8879
  canvas.setStroke(undefined, __strokeWidth, options);
8851
8880
  drawStrokesStyle(strokes, false, ui, canvas);
8881
+ if (options.__useArrow)
8882
+ strokeArrow(ui, canvas);
8852
8883
  break;
8853
8884
  case 'inside':
8854
8885
  canvas.save();
@@ -8874,6 +8905,14 @@ function strokes(strokes, ui, canvas) {
8874
8905
  }
8875
8906
  }
8876
8907
  }
8908
+ function strokeArrow(ui, canvas) {
8909
+ if (ui.__.dashPattern) {
8910
+ canvas.beginPath();
8911
+ ui.__drawPathByData(canvas, ui.__.__pathForArrow);
8912
+ canvas.dashPattern = null;
8913
+ canvas.stroke();
8914
+ }
8915
+ }
8877
8916
 
8878
8917
  const { getSpread, getOuterOf, getByMove, getIntersectData } = BoundsHelper;
8879
8918
  function shape(ui, current, options) {
@@ -9559,16 +9598,16 @@ const EffectModule = {
9559
9598
 
9560
9599
  const { excludeRenderBounds } = LeafBoundsHelper;
9561
9600
  Group.prototype.__renderMask = function (canvas, options) {
9562
- let child, maskCanvas, contentCanvas, maskOpacity, currentMask;
9601
+ let child, maskCanvas, contentCanvas, maskOpacity, currentMask, mask;
9563
9602
  const { children } = this;
9564
9603
  for (let i = 0, len = children.length; i < len; i++) {
9565
- child = children[i];
9566
- if (child.__.mask) {
9604
+ child = children[i], mask = child.__.mask;
9605
+ if (mask) {
9567
9606
  if (currentMask) {
9568
9607
  maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity);
9569
9608
  maskCanvas = contentCanvas = null;
9570
9609
  }
9571
- if (child.__.mask === 'path') {
9610
+ if (mask === 'path' || mask === 'clipping-path') {
9572
9611
  if (child.opacity < 1) {
9573
9612
  currentMask = 'opacity-path';
9574
9613
  maskOpacity = child.opacity;
@@ -9582,14 +9621,14 @@ Group.prototype.__renderMask = function (canvas, options) {
9582
9621
  child.__clip(contentCanvas || canvas, options);
9583
9622
  }
9584
9623
  else {
9585
- currentMask = 'alpha';
9624
+ currentMask = mask === 'grayscale' ? 'grayscale' : 'alpha';
9586
9625
  if (!maskCanvas)
9587
9626
  maskCanvas = getCanvas(canvas);
9588
9627
  if (!contentCanvas)
9589
9628
  contentCanvas = getCanvas(canvas);
9590
9629
  child.__render(maskCanvas, options);
9591
9630
  }
9592
- if (child.__.mask !== 'clipping')
9631
+ if (!(mask === 'clipping' || mask === 'clipping-path'))
9593
9632
  continue;
9594
9633
  }
9595
9634
  if (excludeRenderBounds(child, options))
@@ -9600,6 +9639,8 @@ Group.prototype.__renderMask = function (canvas, options) {
9600
9639
  };
9601
9640
  function maskEnd(leaf, maskMode, canvas, contentCanvas, maskCanvas, maskOpacity) {
9602
9641
  switch (maskMode) {
9642
+ case 'grayscale':
9643
+ maskCanvas.useGrayscaleAlpha(leaf.__nowWorld);
9603
9644
  case 'alpha':
9604
9645
  usePixelMask(leaf, canvas, contentCanvas, maskCanvas);
9605
9646
  break;
@@ -10304,15 +10345,12 @@ const canvas = LeaferCanvasBase.prototype;
10304
10345
  const debug = Debug.get('@leafer-ui/export');
10305
10346
  canvas.export = function (filename, options) {
10306
10347
  const { quality, blob } = FileHelper.getExportOptions(options);
10307
- if (filename.includes('.')) {
10348
+ if (filename.includes('.'))
10308
10349
  return this.saveAs(filename, quality);
10309
- }
10310
- else if (blob) {
10350
+ else if (blob)
10311
10351
  return this.toBlob(filename, quality);
10312
- }
10313
- else {
10352
+ else
10314
10353
  return this.toDataURL(filename, quality);
10315
- }
10316
10354
  };
10317
10355
  canvas.toBlob = function (type, quality) {
10318
10356
  return new Promise((resolve) => {