@leafer/core 1.6.7 → 1.8.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.
package/lib/core.cjs CHANGED
@@ -45,7 +45,7 @@ const IncrementId = {
45
45
  };
46
46
  const I$1 = IncrementId;
47
47
 
48
- const { round: round$3, pow: pow$1, PI: PI$2 } = Math;
48
+ const { round: round$3, pow: pow$1, PI: PI$1 } = Math;
49
49
  const MathHelper = {
50
50
  within(value, min, max) {
51
51
  if (typeof min === 'object')
@@ -141,9 +141,9 @@ const MathHelper = {
141
141
  function randInt(num) {
142
142
  return Math.round(Math.random() * num);
143
143
  }
144
- const OneRadian = PI$2 / 180;
145
- const PI2 = PI$2 * 2;
146
- const PI_2 = PI$2 / 2;
144
+ const OneRadian = PI$1 / 180;
145
+ const PI2 = PI$1 * 2;
146
+ const PI_2 = PI$1 / 2;
147
147
  function getPointData() { return { x: 0, y: 0 }; }
148
148
  function getBoundsData() { return { x: 0, y: 0, width: 0, height: 0 }; }
149
149
  function getMatrixData() { return { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 }; }
@@ -438,7 +438,7 @@ const MatrixHelper = {
438
438
  const M$6 = MatrixHelper;
439
439
 
440
440
  const { toInnerPoint: toInnerPoint$2, toOuterPoint: toOuterPoint$3 } = MatrixHelper;
441
- const { sin: sin$2, cos: cos$2, abs: abs$2, sqrt: sqrt$2, atan2: atan2$2, min: min$1, round: round$2, PI: PI$1 } = Math;
441
+ const { sin: sin$2, cos: cos$2, abs: abs$2, sqrt: sqrt$2, atan2: atan2$2, min: min$1, round: round$2 } = Math;
442
442
  const PointHelper = {
443
443
  defaultPoint: getPointData(),
444
444
  tempPoint: {},
@@ -551,10 +551,11 @@ const PointHelper = {
551
551
  getRadianFrom(fromX, fromY, originX, originY, toX, toY, toOriginX, toOriginY) {
552
552
  if (toOriginX === undefined)
553
553
  toOriginX = originX, toOriginY = originY;
554
- let fromAngle = atan2$2(fromY - originY, fromX - originX);
555
- let toAngle = atan2$2(toY - toOriginY, toX - toOriginX);
556
- const radian = toAngle - fromAngle;
557
- return radian < -PI$1 ? radian + PI2 : radian;
554
+ const a = fromX - originX;
555
+ const b = fromY - originY;
556
+ const c = toX - toOriginX;
557
+ const d = toY - toOriginY;
558
+ return Math.atan2(a * d - b * c, a * c + b * d);
558
559
  },
559
560
  getAtan2(t, to) {
560
561
  return atan2$2(to.y - t.y, to.x - t.x);
@@ -861,6 +862,12 @@ const AroundHelper = {
861
862
  }
862
863
  if (!onlyBoxSize)
863
864
  to.x += box.x, to.y += box.y;
865
+ },
866
+ getPoint(around, box, to) {
867
+ if (!to)
868
+ to = {};
869
+ AroundHelper.toPoint(around, box, to, true);
870
+ return to;
864
871
  }
865
872
  };
866
873
  function get(around) {
@@ -1789,10 +1796,13 @@ function contextAttr(realName) {
1789
1796
  return (target, key) => {
1790
1797
  if (!realName)
1791
1798
  realName = key;
1792
- Object.defineProperty(target, key, {
1799
+ const property = {
1793
1800
  get() { return this.context[realName]; },
1794
1801
  set(value) { this.context[realName] = value; }
1795
- });
1802
+ };
1803
+ if (key === 'strokeCap')
1804
+ property.set = function (value) { this.context[realName] = value === 'none' ? 'butt' : value; };
1805
+ Object.defineProperty(target, key, property);
1796
1806
  };
1797
1807
  }
1798
1808
  const contextMethodNameList = [];
@@ -2075,8 +2085,8 @@ class LeaferCanvasBase extends Canvas {
2075
2085
  get width() { return this.size.width; }
2076
2086
  get height() { return this.size.height; }
2077
2087
  get pixelRatio() { return this.size.pixelRatio; }
2078
- get pixelWidth() { return this.width * this.pixelRatio; }
2079
- get pixelHeight() { return this.height * this.pixelRatio; }
2088
+ get pixelWidth() { return this.width * this.pixelRatio || 0; }
2089
+ get pixelHeight() { return this.height * this.pixelRatio || 0; }
2080
2090
  get pixelSnap() { return this.config.pixelSnap; }
2081
2091
  set pixelSnap(value) { this.config.pixelSnap = value; }
2082
2092
  get allowBackgroundColor() { return this.view && this.parentView; }
@@ -2163,20 +2173,33 @@ class LeaferCanvasBase extends Canvas {
2163
2173
  if (w)
2164
2174
  this.setTransform(w.a, w.b, w.c, w.d, w.e, w.f);
2165
2175
  }
2166
- setStroke(color, strokeWidth, options) {
2176
+ setStroke(color, strokeWidth, options, childOptions) {
2167
2177
  if (strokeWidth)
2168
2178
  this.strokeWidth = strokeWidth;
2169
2179
  if (color)
2170
2180
  this.strokeStyle = color;
2171
2181
  if (options)
2172
- this.setStrokeOptions(options);
2173
- }
2174
- setStrokeOptions(options) {
2175
- this.strokeCap = options.strokeCap === 'none' ? 'butt' : options.strokeCap;
2176
- this.strokeJoin = options.strokeJoin;
2177
- this.dashPattern = options.dashPattern;
2178
- this.dashOffset = options.dashOffset;
2179
- this.miterLimit = options.miterLimit;
2182
+ this.setStrokeOptions(options, childOptions);
2183
+ }
2184
+ setStrokeOptions(options, childOptions) {
2185
+ let { strokeCap, strokeJoin, dashPattern, dashOffset, miterLimit } = options;
2186
+ if (childOptions) {
2187
+ if (childOptions.strokeCap)
2188
+ strokeCap = childOptions.strokeCap;
2189
+ if (childOptions.strokeJoin)
2190
+ strokeJoin = childOptions.strokeJoin;
2191
+ if (childOptions.dashPattern !== undefined)
2192
+ dashPattern = childOptions.dashPattern;
2193
+ if (childOptions.dashOffset !== undefined)
2194
+ dashOffset = childOptions.dashOffset;
2195
+ if (childOptions.miterLimit)
2196
+ miterLimit = childOptions.miterLimit;
2197
+ }
2198
+ this.strokeCap = strokeCap;
2199
+ this.strokeJoin = strokeJoin;
2200
+ this.dashPattern = dashPattern;
2201
+ this.dashOffset = dashOffset;
2202
+ this.miterLimit = miterLimit;
2180
2203
  }
2181
2204
  saveBlendMode(blendMode) {
2182
2205
  this.savedBlendMode = this.blendMode;
@@ -3341,7 +3364,7 @@ const { getCenterX, getCenterY } = PointHelper;
3341
3364
  const { arcTo } = PathCommandDataHelper;
3342
3365
  const PathCorner = {
3343
3366
  smooth(data, cornerRadius, _cornerSmoothing) {
3344
- let command, commandLen;
3367
+ let command, lastCommand, commandLen;
3345
3368
  let i = 0, x = 0, y = 0, startX = 0, startY = 0, secondX = 0, secondY = 0, lastX = 0, lastY = 0;
3346
3369
  const len = data.length;
3347
3370
  const smooth = [];
@@ -3379,8 +3402,10 @@ const PathCorner = {
3379
3402
  lastY = y;
3380
3403
  break;
3381
3404
  case Z:
3382
- arcTo(smooth, startX, startY, secondX, secondY, cornerRadius, lastX, lastY);
3383
- smooth.push(Z);
3405
+ if (lastCommand !== Z) {
3406
+ arcTo(smooth, startX, startY, secondX, secondY, cornerRadius, lastX, lastY);
3407
+ smooth.push(Z);
3408
+ }
3384
3409
  i += 1;
3385
3410
  break;
3386
3411
  default:
@@ -3389,6 +3414,7 @@ const PathCorner = {
3389
3414
  smooth.push(data[i + j]);
3390
3415
  i += commandLen;
3391
3416
  }
3417
+ lastCommand = command;
3392
3418
  }
3393
3419
  if (command !== Z) {
3394
3420
  smooth[1] = startX;
@@ -3885,12 +3911,12 @@ class LeaferImage {
3885
3911
  try {
3886
3912
  if (transform && pattern.setTransform) {
3887
3913
  pattern.setTransform(transform);
3888
- transform = null;
3914
+ transform = undefined;
3889
3915
  }
3890
3916
  }
3891
3917
  catch (_a) { }
3892
3918
  if (paint)
3893
- paint.transform = transform;
3919
+ DataHelper.stintSet(paint, 'transform', transform);
3894
3920
  return pattern;
3895
3921
  }
3896
3922
  destroy() {
@@ -3908,6 +3934,13 @@ function defineKey(target, key, descriptor, noConfigurable) {
3908
3934
  function getDescriptor(object, name) {
3909
3935
  return Object.getOwnPropertyDescriptor(object, name);
3910
3936
  }
3937
+ function createDescriptor(key, defaultValue) {
3938
+ const privateKey = '_' + key;
3939
+ return {
3940
+ get() { const v = this[privateKey]; return v === undefined ? defaultValue : v; },
3941
+ set(value) { this[privateKey] = value; }
3942
+ };
3943
+ }
3911
3944
  function getNames(object) {
3912
3945
  return Object.getOwnPropertyNames(object);
3913
3946
  }
@@ -3995,10 +4028,14 @@ function pathInputType(defaultValue) {
3995
4028
  }));
3996
4029
  }
3997
4030
  const pathType = boundsType;
3998
- function affectStrokeBoundsType(defaultValue) {
4031
+ function affectStrokeBoundsType(defaultValue, useStroke) {
3999
4032
  return decorateLeafAttr(defaultValue, (key) => attr({
4000
4033
  set(value) {
4001
- this.__setAttr(key, value) && doStrokeType(this);
4034
+ if (this.__setAttr(key, value)) {
4035
+ doStrokeType(this);
4036
+ if (useStroke)
4037
+ this.__.__useStroke = true;
4038
+ }
4002
4039
  }
4003
4040
  }));
4004
4041
  }
@@ -4136,15 +4173,7 @@ function defineDataProcessor(target, key, defaultValue) {
4136
4173
  const data = target.__DataProcessor.prototype;
4137
4174
  const computedKey = '_' + key;
4138
4175
  const setMethodName = getSetMethodName(key);
4139
- const property = {
4140
- get() {
4141
- const v = this[computedKey];
4142
- return v === undefined ? defaultValue : v;
4143
- },
4144
- set(value) {
4145
- this[computedKey] = value;
4146
- }
4147
- };
4176
+ const property = createDescriptor(key, defaultValue);
4148
4177
  if (defaultValue === undefined) {
4149
4178
  property.get = function () { return this[computedKey]; };
4150
4179
  }
@@ -4331,6 +4360,14 @@ const LeafHelper = {
4331
4360
  }
4332
4361
  return true;
4333
4362
  },
4363
+ copyCanvasByWorld(leaf, currentCanvas, fromCanvas, fromWorld, blendMode, onlyResetTransform) {
4364
+ if (!fromWorld)
4365
+ fromWorld = leaf.__nowWorld;
4366
+ if (leaf.__worldFlipped || Platform.fullImageShadow)
4367
+ currentCanvas.copyWorldByReset(fromCanvas, fromWorld, leaf.__nowWorld, blendMode, onlyResetTransform);
4368
+ else
4369
+ currentCanvas.copyWorldToInner(fromCanvas, fromWorld, leaf.__layout.renderBounds, blendMode);
4370
+ },
4334
4371
  moveWorld(t, x, y = 0, isInnerPoint, transition) {
4335
4372
  const local = typeof x === 'object' ? Object.assign({}, x) : { x, y };
4336
4373
  isInnerPoint ? toOuterPoint$1(t.localTransform, local, local, true) : (t.parent && toInnerPoint$1(t.parent.worldTransform, local, local, true));
@@ -5263,13 +5300,14 @@ const { setLayout, multiplyParent: multiplyParent$1, translateInner, defaultWorl
5263
5300
  const { toPoint, tempPoint } = AroundHelper;
5264
5301
  const LeafMatrix = {
5265
5302
  __updateWorldMatrix() {
5266
- multiplyParent$1(this.__local || this.__layout, this.parent ? this.parent.__world : defaultWorld, this.__world, !!this.__layout.affectScaleOrRotation, this.__, this.parent && this.parent.__);
5303
+ const { parent, __layout } = this;
5304
+ multiplyParent$1(this.__local || __layout, parent ? parent.__world : defaultWorld, this.__world, !!__layout.affectScaleOrRotation, this.__, parent && (parent.scrollY || parent.scrollX) && parent.__);
5267
5305
  },
5268
5306
  __updateLocalMatrix() {
5269
5307
  if (this.__local) {
5270
5308
  const layout = this.__layout, local = this.__local, data = this.__;
5271
5309
  if (layout.affectScaleOrRotation) {
5272
- if ((layout.scaleChanged && (layout.resized = 'scale')) || layout.rotationChanged) {
5310
+ if ((layout.scaleChanged && (layout.resized || (layout.resized = 'scale'))) || layout.rotationChanged) {
5273
5311
  setLayout(local, data, null, null, layout.affectRotation);
5274
5312
  layout.scaleChanged = layout.rotationChanged = undefined;
5275
5313
  }
@@ -5424,6 +5462,8 @@ const LeafBounds = {
5424
5462
 
5425
5463
  const LeafRender = {
5426
5464
  __render(canvas, options) {
5465
+ if (options.shape)
5466
+ return this.__renderShape(canvas, options);
5427
5467
  if (this.__worldOpacity) {
5428
5468
  const data = this.__;
5429
5469
  canvas.setWorld(this.__nowWorld = this.__getNowWorld(options));
@@ -5433,12 +5473,7 @@ const LeafRender = {
5433
5473
  return this.__renderEraser(canvas, options);
5434
5474
  const tempCanvas = canvas.getSameCanvas(true, true);
5435
5475
  this.__draw(tempCanvas, options, canvas);
5436
- if (this.__worldFlipped) {
5437
- canvas.copyWorldByReset(tempCanvas, this.__nowWorld, null, data.__blendMode, true);
5438
- }
5439
- else {
5440
- canvas.copyWorldToInner(tempCanvas, this.__nowWorld, this.__layout.renderBounds, data.__blendMode);
5441
- }
5476
+ LeafHelper.copyCanvasByWorld(this, canvas, tempCanvas, this.__nowWorld, data.__blendMode, true);
5442
5477
  tempCanvas.recycle(this.__nowWorld);
5443
5478
  }
5444
5479
  else {
@@ -5448,6 +5483,12 @@ const LeafRender = {
5448
5483
  Debug.drawBounds(this, canvas, options);
5449
5484
  }
5450
5485
  },
5486
+ __renderShape(canvas, options) {
5487
+ if (this.__worldOpacity) {
5488
+ canvas.setWorld(this.__nowWorld = this.__getNowWorld(options));
5489
+ this.__drawShape(canvas, options);
5490
+ }
5491
+ },
5451
5492
  __clip(canvas, options) {
5452
5493
  if (this.__worldOpacity) {
5453
5494
  canvas.setWorld(this.__nowWorld = this.__getNowWorld(options));
@@ -5480,7 +5521,7 @@ const BranchRender = {
5480
5521
  options.dimOpacity = data.dim === true ? 0.2 : data.dim;
5481
5522
  else if (data.dimskip)
5482
5523
  options.dimOpacity && (options.dimOpacity = 0);
5483
- if (data.__single) {
5524
+ if (data.__single && !this.isBranchLeaf) {
5484
5525
  if (data.eraser === 'path')
5485
5526
  return this.__renderEraser(canvas, options);
5486
5527
  const tempCanvas = canvas.getSameCanvas(false, true);
@@ -5502,9 +5543,7 @@ const BranchRender = {
5502
5543
  else {
5503
5544
  const { children } = this;
5504
5545
  for (let i = 0, len = children.length; i < len; i++) {
5505
- if (excludeRenderBounds(children[i], options))
5506
- continue;
5507
- children[i].__render(canvas, options);
5546
+ excludeRenderBounds(children[i], options) || children[i].__render(canvas, options);
5508
5547
  }
5509
5548
  }
5510
5549
  },
@@ -5512,14 +5551,13 @@ const BranchRender = {
5512
5551
  if (this.__worldOpacity) {
5513
5552
  const { children } = this;
5514
5553
  for (let i = 0, len = children.length; i < len; i++) {
5515
- if (excludeRenderBounds(children[i], options))
5516
- continue;
5517
- children[i].__clip(canvas, options);
5554
+ excludeRenderBounds(children[i], options) || children[i].__clip(canvas, options);
5518
5555
  }
5519
5556
  }
5520
5557
  }
5521
5558
  };
5522
5559
 
5560
+ const tempScaleData = {};
5523
5561
  const { LEAF, create } = IncrementId;
5524
5562
  const { toInnerPoint, toOuterPoint, multiplyParent } = MatrixHelper;
5525
5563
  const { toOuterOf } = BoundsHelper;
@@ -5712,6 +5750,22 @@ exports.Leaf = class Leaf {
5712
5750
  return this.__world;
5713
5751
  }
5714
5752
  }
5753
+ getClampRenderScale() {
5754
+ let { scaleX } = this.__nowWorld || this.__world;
5755
+ if (scaleX < 0)
5756
+ scaleX = -scaleX;
5757
+ return scaleX > 1 ? scaleX : 1;
5758
+ }
5759
+ getRenderScaleData(abs, scaleFixed) {
5760
+ const { scaleX, scaleY } = ImageManager.patternLocked ? this.__world : this.__nowWorld;
5761
+ if (scaleFixed)
5762
+ tempScaleData.scaleX = tempScaleData.scaleY = 1;
5763
+ else if (abs)
5764
+ tempScaleData.scaleX = scaleX < 0 ? -scaleX : scaleX, tempScaleData.scaleY = scaleY < 0 ? -scaleY : scaleY;
5765
+ else
5766
+ tempScaleData.scaleX = scaleX, tempScaleData.scaleY = scaleY;
5767
+ return tempScaleData;
5768
+ }
5715
5769
  getTransform(relative) {
5716
5770
  return this.__layout.getTransform(relative || 'local');
5717
5771
  }
@@ -5867,7 +5921,8 @@ exports.Leaf = class Leaf {
5867
5921
  __drawFast(_canvas, _options) { }
5868
5922
  __draw(_canvas, _options, _originCanvas) { }
5869
5923
  __clip(_canvas, _options) { }
5870
- __renderShape(_canvas, _options, _ignoreFill, _ignoreStroke) { }
5924
+ __renderShape(_canvas, _options) { }
5925
+ __drawShape(_canvas, _options) { }
5871
5926
  __updateWorldOpacity() { }
5872
5927
  __updateChange() { }
5873
5928
  __drawPath(_canvas) { }
@@ -6235,7 +6290,7 @@ class LeafLevelList {
6235
6290
  }
6236
6291
  }
6237
6292
 
6238
- const version = "1.6.7";
6293
+ const version = "1.8.0";
6239
6294
 
6240
6295
  exports.AlignHelper = AlignHelper;
6241
6296
  exports.AroundHelper = AroundHelper;
@@ -6315,6 +6370,7 @@ exports.autoLayoutType = autoLayoutType;
6315
6370
  exports.boundsType = boundsType;
6316
6371
  exports.canvasPatch = canvasPatch;
6317
6372
  exports.canvasSizeAttrs = canvasSizeAttrs;
6373
+ exports.createDescriptor = createDescriptor;
6318
6374
  exports.cursorType = cursorType;
6319
6375
  exports.dataProcessor = dataProcessor;
6320
6376
  exports.dataType = dataType;