@leafer-draw/worker 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$c.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$c.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$b.repeat(name) : nameList[name] = Event;
1444
+ nameList[name] && debug$b.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
  class LeaferCanvas extends LeaferCanvasBase {
5965
5978
  get allowBackgroundColor() { return true; }
@@ -6428,7 +6441,8 @@ class Renderer {
6428
6441
  this.totalBounds = new Bounds();
6429
6442
  debug$3.log(target.innerName, '--->');
6430
6443
  try {
6431
- target.app.emit(RenderEvent.CHILD_START, target);
6444
+ if (!target.isApp)
6445
+ target.app.emit(RenderEvent.CHILD_START, target);
6432
6446
  this.emitRender(RenderEvent.START);
6433
6447
  this.renderOnce(callback);
6434
6448
  this.emitRender(RenderEvent.END, this.totalBounds);
@@ -6709,6 +6723,12 @@ class UIData extends LeafData {
6709
6723
  return strokeWidth;
6710
6724
  }
6711
6725
  get __hasStroke() { return this.stroke && this.strokeWidth; }
6726
+ get __hasMultiPaint() {
6727
+ const t = this;
6728
+ if ((t.__isFills && t.fill.length > 1) || (t.__isStrokes && t.stroke.length > 1) || t.__useEffect)
6729
+ return true;
6730
+ return t.fill && this.__hasStroke;
6731
+ }
6712
6732
  get __clipAfterFill() { return (this.cornerRadius || this.__pathInputed); }
6713
6733
  get __autoWidth() { return !this._width; }
6714
6734
  get __autoHeight() { return !this._height; }
@@ -7078,9 +7098,8 @@ const RectRender = {
7078
7098
  canvas.strokeRect(half, half, width, height);
7079
7099
  canvas.restore();
7080
7100
  }
7081
- else {
7101
+ else
7082
7102
  canvas.strokeRect(half, half, width, height);
7083
- }
7084
7103
  break;
7085
7104
  case 'outside':
7086
7105
  canvas.strokeRect(-half, -half, width + __strokeWidth, height + __strokeWidth);
@@ -7110,11 +7129,15 @@ let UI = UI_1 = class UI extends Leaf {
7110
7129
  super(data);
7111
7130
  }
7112
7131
  reset(_data) { }
7113
- set(data, isTemp) {
7114
- if (isTemp) {
7115
- this.lockNormalStyle = true;
7116
- Object.assign(this, data);
7117
- this.lockNormalStyle = false;
7132
+ set(data, transition) {
7133
+ if (transition) {
7134
+ if (transition === 'temp') {
7135
+ this.lockNormalStyle = true;
7136
+ Object.assign(this, data);
7137
+ this.lockNormalStyle = false;
7138
+ }
7139
+ else
7140
+ this.animate(data, transition);
7118
7141
  }
7119
7142
  else
7120
7143
  Object.assign(this, data);
@@ -7305,6 +7328,15 @@ __decorate([
7305
7328
  __decorate([
7306
7329
  boundsType(0)
7307
7330
  ], UI.prototype, "padding", void 0);
7331
+ __decorate([
7332
+ boundsType(false)
7333
+ ], UI.prototype, "lockRatio", void 0);
7334
+ __decorate([
7335
+ boundsType()
7336
+ ], UI.prototype, "widthRange", void 0);
7337
+ __decorate([
7338
+ boundsType()
7339
+ ], UI.prototype, "heightRange", void 0);
7308
7340
  __decorate([
7309
7341
  dataType(false)
7310
7342
  ], UI.prototype, "draggable", void 0);
@@ -7415,17 +7447,17 @@ let Group = class Group extends UI {
7415
7447
  if (!this.children)
7416
7448
  this.children = [];
7417
7449
  }
7418
- set(data, isTemp) {
7450
+ set(data, transition) {
7419
7451
  if (data.children) {
7420
7452
  const { children } = data;
7421
7453
  delete data.children;
7422
7454
  this.children ? this.clear() : this.__setBranch();
7423
- super.set(data, isTemp);
7455
+ super.set(data, transition);
7424
7456
  children.forEach(child => this.add(child));
7425
7457
  data.children = children;
7426
7458
  }
7427
7459
  else
7428
- super.set(data, isTemp);
7460
+ super.set(data, transition);
7429
7461
  }
7430
7462
  toJSON(options) {
7431
7463
  const data = super.toJSON(options);
@@ -7538,8 +7570,8 @@ let Leafer = Leafer_1 = class Leafer extends Group {
7538
7570
  }
7539
7571
  onInit() { }
7540
7572
  initType(_type) { }
7541
- set(data) {
7542
- this.waitInit(() => { super.set(data); });
7573
+ set(data, transition) {
7574
+ this.waitInit(() => { super.set(data, transition); });
7543
7575
  }
7544
7576
  start() {
7545
7577
  clearTimeout(this.__startTimer);
@@ -7596,8 +7628,6 @@ let Leafer = Leafer_1 = class Leafer extends Group {
7596
7628
  __onResize(event) {
7597
7629
  this.emitEvent(event);
7598
7630
  DataHelper.copyAttrs(this.__, event, canvasSizeAttrs);
7599
- if (!event.width || !event.height)
7600
- debug$1.warn('w = 0 or h = 0');
7601
7631
  setTimeout(() => { if (this.canvasManager)
7602
7632
  this.canvasManager.clearRecycled(); }, 0);
7603
7633
  }
@@ -7922,9 +7952,6 @@ __decorate([
7922
7952
  __decorate([
7923
7953
  dataType(false)
7924
7954
  ], Box.prototype, "resizeChildren", void 0);
7925
- __decorate([
7926
- dataType(false)
7927
- ], Box.prototype, "textBox", void 0);
7928
7955
  __decorate([
7929
7956
  affectRenderBoundsType('show')
7930
7957
  ], Box.prototype, "overflow", void 0);
@@ -8639,6 +8666,8 @@ function stroke(stroke, ui, canvas) {
8639
8666
  case 'center':
8640
8667
  canvas.setStroke(stroke, __strokeWidth, options);
8641
8668
  canvas.stroke();
8669
+ if (options.__useArrow)
8670
+ strokeArrow(ui, canvas);
8642
8671
  break;
8643
8672
  case 'inside':
8644
8673
  canvas.save();
@@ -8676,6 +8705,8 @@ function strokes(strokes, ui, canvas) {
8676
8705
  case 'center':
8677
8706
  canvas.setStroke(undefined, __strokeWidth, options);
8678
8707
  drawStrokesStyle(strokes, false, ui, canvas);
8708
+ if (options.__useArrow)
8709
+ strokeArrow(ui, canvas);
8679
8710
  break;
8680
8711
  case 'inside':
8681
8712
  canvas.save();
@@ -8701,6 +8732,14 @@ function strokes(strokes, ui, canvas) {
8701
8732
  }
8702
8733
  }
8703
8734
  }
8735
+ function strokeArrow(ui, canvas) {
8736
+ if (ui.__.dashPattern) {
8737
+ canvas.beginPath();
8738
+ ui.__drawPathByData(canvas, ui.__.__pathForArrow);
8739
+ canvas.dashPattern = null;
8740
+ canvas.stroke();
8741
+ }
8742
+ }
8704
8743
 
8705
8744
  const { getSpread, getOuterOf, getByMove, getIntersectData } = BoundsHelper;
8706
8745
  function shape(ui, current, options) {
@@ -9386,16 +9425,16 @@ const EffectModule = {
9386
9425
 
9387
9426
  const { excludeRenderBounds } = LeafBoundsHelper;
9388
9427
  Group.prototype.__renderMask = function (canvas, options) {
9389
- let child, maskCanvas, contentCanvas, maskOpacity, currentMask;
9428
+ let child, maskCanvas, contentCanvas, maskOpacity, currentMask, mask;
9390
9429
  const { children } = this;
9391
9430
  for (let i = 0, len = children.length; i < len; i++) {
9392
- child = children[i];
9393
- if (child.__.mask) {
9431
+ child = children[i], mask = child.__.mask;
9432
+ if (mask) {
9394
9433
  if (currentMask) {
9395
9434
  maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity);
9396
9435
  maskCanvas = contentCanvas = null;
9397
9436
  }
9398
- if (child.__.mask === 'path') {
9437
+ if (mask === 'path' || mask === 'clipping-path') {
9399
9438
  if (child.opacity < 1) {
9400
9439
  currentMask = 'opacity-path';
9401
9440
  maskOpacity = child.opacity;
@@ -9409,14 +9448,14 @@ Group.prototype.__renderMask = function (canvas, options) {
9409
9448
  child.__clip(contentCanvas || canvas, options);
9410
9449
  }
9411
9450
  else {
9412
- currentMask = 'alpha';
9451
+ currentMask = mask === 'grayscale' ? 'grayscale' : 'alpha';
9413
9452
  if (!maskCanvas)
9414
9453
  maskCanvas = getCanvas(canvas);
9415
9454
  if (!contentCanvas)
9416
9455
  contentCanvas = getCanvas(canvas);
9417
9456
  child.__render(maskCanvas, options);
9418
9457
  }
9419
- if (child.__.mask !== 'clipping')
9458
+ if (!(mask === 'clipping' || mask === 'clipping-path'))
9420
9459
  continue;
9421
9460
  }
9422
9461
  if (excludeRenderBounds(child, options))
@@ -9427,6 +9466,8 @@ Group.prototype.__renderMask = function (canvas, options) {
9427
9466
  };
9428
9467
  function maskEnd(leaf, maskMode, canvas, contentCanvas, maskCanvas, maskOpacity) {
9429
9468
  switch (maskMode) {
9469
+ case 'grayscale':
9470
+ maskCanvas.useGrayscaleAlpha(leaf.__nowWorld);
9430
9471
  case 'alpha':
9431
9472
  usePixelMask(leaf, canvas, contentCanvas, maskCanvas);
9432
9473
  break;
@@ -10131,15 +10172,12 @@ const canvas = LeaferCanvasBase.prototype;
10131
10172
  const debug = Debug.get('@leafer-ui/export');
10132
10173
  canvas.export = function (filename, options) {
10133
10174
  const { quality, blob } = FileHelper.getExportOptions(options);
10134
- if (filename.includes('.')) {
10175
+ if (filename.includes('.'))
10135
10176
  return this.saveAs(filename, quality);
10136
- }
10137
- else if (blob) {
10177
+ else if (blob)
10138
10178
  return this.toBlob(filename, quality);
10139
- }
10140
- else {
10179
+ else
10141
10180
  return this.toDataURL(filename, quality);
10142
- }
10143
10181
  };
10144
10182
  canvas.toBlob = function (type, quality) {
10145
10183
  return new Promise((resolve) => {