leafer-draw 1.0.0 → 1.0.1

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/web.js CHANGED
@@ -51,6 +51,8 @@ var LeaferUI = (function (exports) {
51
51
  const { round, pow: pow$1, PI: PI$4 } = Math;
52
52
  const MathHelper = {
53
53
  within(value, min, max) {
54
+ if (typeof min === 'object')
55
+ max = min.max, min = min.min;
54
56
  if (min !== undefined && value < min)
55
57
  value = min;
56
58
  if (max !== undefined && value > max)
@@ -153,10 +155,10 @@ var LeaferUI = (function (exports) {
153
155
  t.e += x;
154
156
  t.f += y;
155
157
  },
156
- translateInner(t, x, y, isMoveOrigin) {
158
+ translateInner(t, x, y, hasOrigin) {
157
159
  t.e += t.a * x + t.c * y;
158
160
  t.f += t.b * x + t.d * y;
159
- if (isMoveOrigin)
161
+ if (hasOrigin)
160
162
  t.e -= x, t.f -= y;
161
163
  },
162
164
  scale(t, scaleX, scaleY = scaleX) {
@@ -315,7 +317,7 @@ var LeaferUI = (function (exports) {
315
317
  to.y -= (f * a - e * b) * s;
316
318
  }
317
319
  },
318
- setLayout(t, layout, origin, bcChanged) {
320
+ setLayout(t, layout, origin, around, bcChanged) {
319
321
  const { x, y, scaleX, scaleY } = layout;
320
322
  if (bcChanged === undefined)
321
323
  bcChanged = layout.rotation || layout.skewX || layout.skewY;
@@ -347,10 +349,10 @@ var LeaferUI = (function (exports) {
347
349
  }
348
350
  t.e = x;
349
351
  t.f = y;
350
- if (origin)
351
- M$6.translateInner(t, -origin.x, -origin.y, true);
352
+ if (origin = origin || around)
353
+ M$6.translateInner(t, -origin.x, -origin.y, !around);
352
354
  },
353
- getLayout(t, origin, firstSkewY) {
355
+ getLayout(t, origin, around, firstSkewY) {
354
356
  const { a, b, c, d, e, f } = t;
355
357
  let x = e, y = f, scaleX, scaleY, rotation, skewX, skewY;
356
358
  if (b || c) {
@@ -379,9 +381,11 @@ var LeaferUI = (function (exports) {
379
381
  scaleY = d;
380
382
  rotation = skewX = skewY = 0;
381
383
  }
382
- if (origin) {
384
+ if (origin = around || origin) {
383
385
  x += origin.x * a + origin.y * c;
384
386
  y += origin.x * b + origin.y * d;
387
+ if (!around)
388
+ x -= origin.x, y -= origin.y;
385
389
  }
386
390
  return { x, y, scaleX, scaleY, rotation, skewX, skewY };
387
391
  },
@@ -709,12 +713,12 @@ var LeaferUI = (function (exports) {
709
713
  toInnerPoint(outer, to, distance) {
710
714
  MatrixHelper.toInnerPoint(this, outer, to, distance);
711
715
  }
712
- setLayout(data, origin) {
713
- MatrixHelper.setLayout(this, data, origin);
716
+ setLayout(data, origin, around) {
717
+ MatrixHelper.setLayout(this, data, origin, around);
714
718
  return this;
715
719
  }
716
- getLayout(origin, firstSkewY) {
717
- return MatrixHelper.getLayout(this, origin, firstSkewY);
720
+ getLayout(origin, around, firstSkewY) {
721
+ return MatrixHelper.getLayout(this, origin, around, firstSkewY);
718
722
  }
719
723
  withScale(scaleX, scaleY) {
720
724
  return MatrixHelper.withScale(this, scaleX, scaleY);
@@ -2002,7 +2006,7 @@ var LeaferUI = (function (exports) {
2002
2006
  DataHelper.copyAttrs(this.size, size, canvasSizeAttrs);
2003
2007
  this.size.pixelRatio || (this.size.pixelRatio = 1);
2004
2008
  this.bounds = new Bounds(0, 0, this.width, this.height);
2005
- if (!this.unreal) {
2009
+ if (this.context && !this.unreal) {
2006
2010
  this.updateViewSize();
2007
2011
  this.smooth = this.config.smooth;
2008
2012
  }
@@ -2175,7 +2179,7 @@ var LeaferUI = (function (exports) {
2175
2179
  this.manager ? this.manager.recycle(this) : this.destroy();
2176
2180
  }
2177
2181
  }
2178
- updateRender() { }
2182
+ updateRender(_bounds) { }
2179
2183
  unrealCanvas() { }
2180
2184
  destroy() {
2181
2185
  this.manager = this.view = this.parentView = null;
@@ -4069,8 +4073,13 @@ var LeaferUI = (function (exports) {
4069
4073
  zoomOfLocal(t, origin, scaleX, scaleY = scaleX, resize) {
4070
4074
  copy$6(matrix, t.__localMatrix);
4071
4075
  scaleOfOuter$2(matrix, origin, scaleX, scaleY);
4072
- moveByMatrix(t, matrix);
4073
- t.scaleResize(scaleX, scaleY, resize !== true);
4076
+ if (t.origin || t.around) {
4077
+ L.setTransform(t, matrix, resize);
4078
+ }
4079
+ else {
4080
+ moveByMatrix(t, matrix);
4081
+ t.scaleResize(scaleX, scaleY, resize !== true);
4082
+ }
4074
4083
  },
4075
4084
  rotateOfWorld(t, origin, angle) {
4076
4085
  L.rotateOfLocal(t, getTempLocal(t, origin), angle);
@@ -4078,8 +4087,13 @@ var LeaferUI = (function (exports) {
4078
4087
  rotateOfLocal(t, origin, angle) {
4079
4088
  copy$6(matrix, t.__localMatrix);
4080
4089
  rotateOfOuter$2(matrix, origin, angle);
4081
- moveByMatrix(t, matrix);
4082
- t.rotation = MathHelper.formatRotation(t.rotation + angle);
4090
+ if (t.origin || t.around) {
4091
+ L.setTransform(t, matrix);
4092
+ }
4093
+ else {
4094
+ moveByMatrix(t, matrix);
4095
+ t.rotation = MathHelper.formatRotation(t.rotation + angle);
4096
+ }
4083
4097
  },
4084
4098
  skewOfWorld(t, origin, skewX, skewY, resize) {
4085
4099
  L.skewOfLocal(t, getTempLocal(t, origin), skewX, skewY, resize);
@@ -4102,7 +4116,7 @@ var LeaferUI = (function (exports) {
4102
4116
  L.setTransform(t, matrix, resize);
4103
4117
  },
4104
4118
  setTransform(t, transform, resize) {
4105
- const layout = getLayout(transform);
4119
+ const layout = getLayout(transform, t.origin && L.getInnerOrigin(t, t.origin), t.around && L.getInnerOrigin(t, t.around));
4106
4120
  if (resize) {
4107
4121
  const scaleX = layout.scaleX / t.scaleX;
4108
4122
  const scaleY = layout.scaleY / t.scaleY;
@@ -4115,13 +4129,19 @@ var LeaferUI = (function (exports) {
4115
4129
  t.set(layout);
4116
4130
  }
4117
4131
  },
4132
+ getFlipTransform(t, axis) {
4133
+ const m = getMatrixData();
4134
+ const sign = axis === 'x' ? 1 : -1;
4135
+ scaleOfOuter$2(m, L.getLocalOrigin(t, 'center'), -1 * sign, 1 * sign);
4136
+ return m;
4137
+ },
4118
4138
  getLocalOrigin(t, origin) {
4119
4139
  return PointHelper.tempToOuterOf(L.getInnerOrigin(t, origin), t.localTransform);
4120
4140
  },
4121
4141
  getInnerOrigin(t, origin) {
4122
- if (typeof origin === 'string')
4123
- AroundHelper.toPoint(origin, t.boxBounds, origin = {});
4124
- return origin;
4142
+ const innerOrigin = {};
4143
+ AroundHelper.toPoint(origin, t.boxBounds, innerOrigin);
4144
+ return innerOrigin;
4125
4145
  },
4126
4146
  getRelativeWorld(t, relative, temp) {
4127
4147
  copy$6(matrix, t.worldTransform);
@@ -4548,7 +4568,10 @@ var LeaferUI = (function (exports) {
4548
4568
  on(type, listener, options) {
4549
4569
  let capture, once;
4550
4570
  if (options) {
4551
- if (typeof options === 'boolean') {
4571
+ if (options === 'once') {
4572
+ once = true;
4573
+ }
4574
+ else if (typeof options === 'boolean') {
4552
4575
  capture = options;
4553
4576
  }
4554
4577
  else {
@@ -4579,7 +4602,7 @@ var LeaferUI = (function (exports) {
4579
4602
  if (listener) {
4580
4603
  let capture;
4581
4604
  if (options)
4582
- capture = typeof options === 'boolean' ? options : options.capture;
4605
+ capture = typeof options === 'boolean' ? options : (options === 'once' ? false : options.capture);
4583
4606
  let events, index;
4584
4607
  const map = __getListenerMap(this, capture);
4585
4608
  typeList.forEach(type => {
@@ -4881,7 +4904,7 @@ var LeaferUI = (function (exports) {
4881
4904
  const layout = this.__layout, local = this.__local, data = this.__;
4882
4905
  if (layout.affectScaleOrRotation) {
4883
4906
  if (layout.scaleChanged || layout.rotationChanged) {
4884
- setLayout(local, data, null, layout.affectRotation);
4907
+ setLayout(local, data, null, null, layout.affectRotation);
4885
4908
  layout.scaleChanged = layout.rotationChanged = false;
4886
4909
  }
4887
4910
  }
@@ -4889,7 +4912,7 @@ var LeaferUI = (function (exports) {
4889
4912
  local.f = data.y + data.offsetY;
4890
4913
  if (data.around || data.origin) {
4891
4914
  toPoint$3(data.around || data.origin, layout.boxBounds, tempPoint$1);
4892
- translateInner(local, -tempPoint$1.x, -tempPoint$1.y, data.origin);
4915
+ translateInner(local, -tempPoint$1.x, -tempPoint$1.y, !data.around);
4893
4916
  }
4894
4917
  }
4895
4918
  this.__layout.matrixChanged = false;
@@ -5118,7 +5141,7 @@ var LeaferUI = (function (exports) {
5118
5141
  const { toInnerPoint, toOuterPoint, multiplyParent } = MatrixHelper;
5119
5142
  const { toOuterOf } = BoundsHelper;
5120
5143
  const { copy: copy$3 } = PointHelper;
5121
- const { moveLocal, zoomOfLocal, rotateOfLocal, skewOfLocal, moveWorld, zoomOfWorld, rotateOfWorld, skewOfWorld, transform, transformWorld, setTransform, getLocalOrigin, getRelativeWorld, drop } = LeafHelper;
5144
+ const { moveLocal, zoomOfLocal, rotateOfLocal, skewOfLocal, moveWorld, zoomOfWorld, rotateOfWorld, skewOfWorld, transform, transformWorld, setTransform, getFlipTransform, getLocalOrigin, getRelativeWorld, drop } = LeafHelper;
5122
5145
  exports.Leaf = class Leaf {
5123
5146
  get tag() { return this.__tag; }
5124
5147
  set tag(_value) { }
@@ -5144,6 +5167,8 @@ var LeaferUI = (function (exports) {
5144
5167
  get __ignoreHitWorld() { return (this.__hasMask || this.__hasEraser) && this.__.hitChildren; }
5145
5168
  get __inLazyBounds() { const { leafer } = this; return leafer && leafer.created && leafer.lazyBounds.hit(this.__world); }
5146
5169
  get pathInputed() { return this.__.__pathInputed; }
5170
+ set event(map) { let event; for (let key in map)
5171
+ event = map[key], event instanceof Array ? this.on(key, event[0], event[1]) : this.on(key, event); }
5147
5172
  constructor(data) {
5148
5173
  this.innerId = create(LEAF);
5149
5174
  this.reset(data);
@@ -5403,6 +5428,9 @@ var LeaferUI = (function (exports) {
5403
5428
  skewOfWorld(worldOrigin, skewX, skewY, resize) {
5404
5429
  skewOfWorld(this, worldOrigin, skewX, skewY, resize);
5405
5430
  }
5431
+ flip(axis) {
5432
+ transform(this, getFlipTransform(this, axis));
5433
+ }
5406
5434
  scaleResize(scaleX, scaleY = scaleX, _noResize) {
5407
5435
  this.scaleX *= scaleX;
5408
5436
  this.scaleY *= scaleY;
@@ -5760,7 +5788,7 @@ var LeaferUI = (function (exports) {
5760
5788
  }
5761
5789
  }
5762
5790
 
5763
- const version = "1.0.0-rc.30";
5791
+ const version = "1.0.1";
5764
5792
  const inviteCode = {};
5765
5793
 
5766
5794
  const debug$5 = Debug.get('LeaferCanvas');
@@ -5784,7 +5812,8 @@ var LeaferUI = (function (exports) {
5784
5812
  }
5785
5813
  }
5786
5814
  init() {
5787
- const { view } = this.config;
5815
+ const { config } = this;
5816
+ const view = config.view || config.canvas;
5788
5817
  view ? this.__createViewFrom(view) : this.__createView();
5789
5818
  const { style } = this.view;
5790
5819
  style.display || (style.display = 'block');
@@ -5799,7 +5828,7 @@ var LeaferUI = (function (exports) {
5799
5828
  }
5800
5829
  this.__createContext();
5801
5830
  if (!this.autoLayout)
5802
- this.resize(this.config);
5831
+ this.resize(config);
5803
5832
  }
5804
5833
  set backgroundColor(color) { this.view.style.backgroundColor = color; }
5805
5834
  get backgroundColor() { return this.view.style.backgroundColor; }
@@ -5855,26 +5884,37 @@ var LeaferUI = (function (exports) {
5855
5884
  this.clientBounds = this.view.getBoundingClientRect();
5856
5885
  }
5857
5886
  startAutoLayout(autoBounds, listener) {
5858
- this.autoBounds = autoBounds;
5859
5887
  this.resizeListener = listener;
5860
- try {
5861
- this.resizeObserver = new ResizeObserver((entries) => {
5862
- this.updateClientBounds();
5863
- for (const entry of entries)
5864
- this.checkAutoBounds(entry.contentRect);
5865
- });
5866
- const parent = this.parentView;
5867
- if (parent) {
5868
- this.resizeObserver.observe(parent);
5869
- this.checkAutoBounds(parent.getBoundingClientRect());
5888
+ if (autoBounds) {
5889
+ this.autoBounds = autoBounds;
5890
+ try {
5891
+ this.resizeObserver = new ResizeObserver((entries) => {
5892
+ this.updateClientBounds();
5893
+ for (const entry of entries)
5894
+ this.checkAutoBounds(entry.contentRect);
5895
+ });
5896
+ const parent = this.parentView;
5897
+ if (parent) {
5898
+ this.resizeObserver.observe(parent);
5899
+ this.checkAutoBounds(parent.getBoundingClientRect());
5900
+ }
5901
+ else {
5902
+ this.checkAutoBounds(this.view);
5903
+ debug$5.warn('no parent');
5904
+ }
5870
5905
  }
5871
- else {
5872
- this.checkAutoBounds(this.view);
5873
- debug$5.warn('no parent');
5906
+ catch (_a) {
5907
+ this.imitateResizeObserver();
5874
5908
  }
5875
5909
  }
5876
- catch (_a) {
5877
- this.imitateResizeObserver();
5910
+ else {
5911
+ window.addEventListener('resize', () => {
5912
+ const pixelRatio = Platform.devicePixelRatio;
5913
+ if (this.pixelRatio !== pixelRatio) {
5914
+ const { width, height } = this;
5915
+ this.emitResize({ width, height, pixelRatio });
5916
+ }
5917
+ });
5878
5918
  }
5879
5919
  }
5880
5920
  imitateResizeObserver() {
@@ -5887,17 +5927,12 @@ var LeaferUI = (function (exports) {
5887
5927
  checkAutoBounds(parentSize) {
5888
5928
  const view = this.view;
5889
5929
  const { x, y, width, height } = this.autoBounds.getBoundsFrom(parentSize);
5890
- if (width !== this.width || height !== this.height) {
5930
+ const size = { width, height, pixelRatio: Platform.devicePixelRatio };
5931
+ if (!this.isSameSize(size)) {
5891
5932
  const { style } = view;
5892
- const { pixelRatio } = this;
5893
5933
  style.marginLeft = x + 'px';
5894
5934
  style.marginTop = y + 'px';
5895
- const size = { width, height, pixelRatio };
5896
- const oldSize = {};
5897
- DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs);
5898
- this.resize(size);
5899
- if (this.width !== undefined)
5900
- this.resizeListener(new ResizeEvent(size, oldSize));
5935
+ this.emitResize(size);
5901
5936
  }
5902
5937
  }
5903
5938
  stopAutoLayout() {
@@ -5908,6 +5943,13 @@ var LeaferUI = (function (exports) {
5908
5943
  this.resizeObserver = null;
5909
5944
  }
5910
5945
  }
5946
+ emitResize(size) {
5947
+ const oldSize = {};
5948
+ DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs);
5949
+ this.resize(size);
5950
+ if (this.width !== undefined)
5951
+ this.resizeListener(new ResizeEvent(size, oldSize));
5952
+ }
5911
5953
  unrealCanvas() {
5912
5954
  if (!this.unreal && this.parentView) {
5913
5955
  const view = this.view;
@@ -5988,7 +6030,7 @@ var LeaferUI = (function (exports) {
5988
6030
  Platform.name = 'web';
5989
6031
  Platform.isMobile = 'ontouchstart' in window;
5990
6032
  Platform.requestRender = function (render) { window.requestAnimationFrame(render); };
5991
- Platform.devicePixelRatio = Math.max(1, devicePixelRatio);
6033
+ defineKey(Platform, 'devicePixelRatio', { get() { return Math.max(1, devicePixelRatio); } });
5992
6034
  const { userAgent } = navigator;
5993
6035
  if (userAgent.indexOf("Firefox") > -1) {
5994
6036
  Platform.conicGradientRotate90 = true;
@@ -6485,14 +6527,14 @@ var LeaferUI = (function (exports) {
6485
6527
  if (Debug.showRepaint)
6486
6528
  this.canvas.strokeWorld(bounds, 'red');
6487
6529
  this.target.__render(this.canvas, options);
6488
- this.renderBounds = realBounds || bounds;
6530
+ this.renderBounds = realBounds = realBounds || bounds;
6489
6531
  this.renderOptions = options;
6490
- this.totalBounds.isEmpty() ? this.totalBounds = this.renderBounds : this.totalBounds.add(this.renderBounds);
6532
+ this.totalBounds.isEmpty() ? this.totalBounds = realBounds : this.totalBounds.add(realBounds);
6491
6533
  if (Debug.showHitView)
6492
6534
  this.renderHitView(options);
6493
6535
  if (Debug.showBoundsView)
6494
6536
  this.renderBoundsView(options);
6495
- this.canvas.updateRender();
6537
+ this.canvas.updateRender(realBounds);
6496
6538
  }
6497
6539
  renderHitView(_options) { }
6498
6540
  renderBoundsView(_options) { }
@@ -7543,7 +7585,7 @@ var LeaferUI = (function (exports) {
7543
7585
  this.__controllers.push(this.renderer = Creator.renderer(this, canvas, config), this.watcher = Creator.watcher(this, config), this.layouter = Creator.layouter(this, config));
7544
7586
  if (this.isApp)
7545
7587
  this.__setApp();
7546
- this.__checkAutoLayout(config);
7588
+ this.__checkAutoLayout(config, parentApp);
7547
7589
  this.view = canvas.view;
7548
7590
  if (parentApp) {
7549
7591
  this.__bindApp(parentApp);
@@ -7644,9 +7686,10 @@ var LeaferUI = (function (exports) {
7644
7686
  this.leafer = leafer;
7645
7687
  this.__level = 1;
7646
7688
  }
7647
- __checkAutoLayout(config) {
7648
- if (!config.width || !config.height) {
7649
- this.autoLayout = new AutoBounds(config);
7689
+ __checkAutoLayout(config, parentApp) {
7690
+ if (!parentApp) {
7691
+ if (!config.width || !config.height)
7692
+ this.autoLayout = new AutoBounds(config);
7650
7693
  this.canvas.startAutoLayout(this.autoLayout, this.__onResize.bind(this));
7651
7694
  }
7652
7695
  }
@@ -7781,7 +7824,9 @@ var LeaferUI = (function (exports) {
7781
7824
  list.push(item);
7782
7825
  }
7783
7826
  }
7784
- zoom(_zoomType, _padding, _fixedScale) { return undefined; }
7827
+ zoom(_zoomType, _padding, _fixedScale) {
7828
+ return debug$1.error('need @leafer-in/view');
7829
+ }
7785
7830
  getValidMove(moveX, moveY) { return { x: moveX, y: moveY }; }
7786
7831
  getValidScale(changeScale) { return changeScale; }
7787
7832
  getWorldPointByClient(clientPoint, updateClient) {
@@ -8308,8 +8353,7 @@ var LeaferUI = (function (exports) {
8308
8353
  destroy() {
8309
8354
  if (this.canvas) {
8310
8355
  this.canvas.destroy();
8311
- this.canvas = null;
8312
- this.context = null;
8356
+ this.canvas = this.context = null;
8313
8357
  }
8314
8358
  super.destroy();
8315
8359
  }
@@ -9153,7 +9197,7 @@ var LeaferUI = (function (exports) {
9153
9197
  const { abs } = Math;
9154
9198
  function checkImage(ui, canvas, paint, allowPaint) {
9155
9199
  const { scaleX, scaleY } = ImageManager.patternLocked ? ui.__world : ui.__nowWorld;
9156
- if (!paint.data || paint.patternId === scaleX + '-' + scaleY) {
9200
+ if (!paint.data || (paint.patternId === scaleX + '-' + scaleY && !Export.running)) {
9157
9201
  return false;
9158
9202
  }
9159
9203
  else {
@@ -10131,7 +10175,10 @@ var LeaferUI = (function (exports) {
10131
10175
  const { x, y, width, height } = new Bounds(renderBounds).scale(scale);
10132
10176
  let canvas = Creator.canvas({ width: Math.round(width), height: Math.round(height), pixelRatio, smooth, contextSettings });
10133
10177
  const renderOptions = { matrix: matrix.scale(1 / scale).invert().translate(-x, -y).withScale(1 / scaleX * scale, 1 / scaleY * scale) };
10178
+ let sliceLeaf;
10134
10179
  if (slice) {
10180
+ sliceLeaf = leaf;
10181
+ sliceLeaf.__worldOpacity = 0;
10135
10182
  leaf = leafer;
10136
10183
  renderOptions.bounds = canvas.bounds;
10137
10184
  }
@@ -10146,6 +10193,8 @@ var LeaferUI = (function (exports) {
10146
10193
  leaf.__render(canvas, renderOptions);
10147
10194
  }
10148
10195
  canvas.restore();
10196
+ if (sliceLeaf)
10197
+ sliceLeaf.__updateWorldOpacity();
10149
10198
  if (trim) {
10150
10199
  trimBounds = getTrimBounds(canvas);
10151
10200
  const old = canvas, { width, height } = trimBounds;