@meta2d/core 1.0.26 → 1.0.28

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meta2d/core",
3
- "version": "1.0.26",
3
+ "version": "1.0.28",
4
4
  "description": "@meta2d/core: Powerful, Beautiful, Simple, Open - Web-Based 2D At Its Best .",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -13,6 +13,7 @@ import { MagnifierCanvas } from './magnifierCanvas';
13
13
  import { Meta2d } from '../core';
14
14
  import { Dialog } from '../dialog';
15
15
  import { Title } from '../title';
16
+ import { CanvasTemplate } from './canvasTemplate';
16
17
  export declare const movingSuffix: "-moving";
17
18
  export declare class Canvas {
18
19
  parent: Meta2d;
@@ -104,6 +105,7 @@ export declare class Canvas {
104
105
  mousePos: Point;
105
106
  scroll: Scroll;
106
107
  movingAnchor: Point;
108
+ canvasTemplate: CanvasTemplate;
107
109
  canvasImage: CanvasImage;
108
110
  canvasImageBottom: CanvasImage;
109
111
  magnifierCanvas: MagnifierCanvas;
@@ -200,6 +202,7 @@ export declare class Canvas {
200
202
  * @param pens 本次改变的 pens
201
203
  */
202
204
  initImageCanvas(pens: Pen[]): void;
205
+ initTemplateCanvas(pens: Pen[]): void;
203
206
  private hasImage;
204
207
  private clearDock;
205
208
  inactive(drawing?: boolean): void;
@@ -238,6 +241,8 @@ export declare class Canvas {
238
241
  loadImage(pen: Pen): void;
239
242
  private imageTimer;
240
243
  imageLoaded(): void;
244
+ private templateImageTimer;
245
+ templateImageLoaded(): void;
241
246
  setCalculativeByScale(pen: Pen): void;
242
247
  updatePenRect(pen: Pen, { worldRectIsReady, playingAnimate, }?: {
243
248
  worldRectIsReady?: boolean;
@@ -260,6 +265,10 @@ export declare class Canvas {
260
265
  x: number;
261
266
  y: number;
262
267
  }): void;
268
+ templateScale(scale: number, center?: {
269
+ x: number;
270
+ y: number;
271
+ }): void;
263
272
  rotatePens(e: Point): void;
264
273
  resizePens(e: Point): void;
265
274
  movePens(e: {
@@ -315,6 +324,14 @@ export declare class Canvas {
315
324
  * @param doing 是否持续移动
316
325
  */
317
326
  translatePens(pens: Pen[], x: number, y: number, doing?: boolean): void;
327
+ /**
328
+ * 移动 画笔们
329
+ * @param pens 画笔们,不包含子节点
330
+ * @param x 偏移 x
331
+ * @param y 偏移 y
332
+ * @param doing 是否持续移动
333
+ */
334
+ templateTranslatePens(pens: Pen[], x: number, y: number): void;
318
335
  private calcAutoAnchor;
319
336
  restoreNodeAnimate(pen: Pen): void;
320
337
  updateLines(pen: Pen, change?: boolean): void;
@@ -99,6 +99,7 @@ import { lockedError } from '../utils/error';
99
99
  import { Dialog } from '../dialog';
100
100
  import { setter } from '../utils/object';
101
101
  import { Title } from '../title';
102
+ import { CanvasTemplate } from './canvasTemplate';
102
103
  export var movingSuffix = '-moving';
103
104
  var Canvas = /** @class */ (function () {
104
105
  function Canvas(parent, parentElement, store) {
@@ -126,7 +127,7 @@ var Canvas = /** @class */ (function () {
126
127
  this.pointSize = 8;
127
128
  this.pasteOffset = true;
128
129
  this.opening = false;
129
- this.maxZindex = 4;
130
+ this.maxZindex = 5;
130
131
  this.canMoveLine = false; //moveConnectedLine=false
131
132
  this.inputParent = document.createElement('div');
132
133
  // input = document.createElement('textarea');
@@ -1886,7 +1887,11 @@ var Canvas = /** @class */ (function () {
1886
1887
  ((_c = _this.store.activeAnchor) === null || _c === void 0 ? void 0 : _c.twoWay) === TwoWay.Disable) {
1887
1888
  continue;
1888
1889
  }
1890
+ _this.title.hide();
1889
1891
  if (_this.inAnchor(pt, pen, anchor)) {
1892
+ var _anchor = deepClone(anchor);
1893
+ Object.assign(_anchor, pt);
1894
+ _this.title.show(_anchor, pen);
1890
1895
  return true;
1891
1896
  }
1892
1897
  }
@@ -1946,6 +1951,7 @@ var Canvas = /** @class */ (function () {
1946
1951
  var ctx = _this.canvas.getContext('2d');
1947
1952
  ctx.clearRect(0, 0, _this.canvas.width, _this.canvas.height);
1948
1953
  ctx.drawImage(_this.offscreen, 0, 0, _this.width, _this.height);
1954
+ _this.canvasTemplate.render();
1949
1955
  _this.canvasImageBottom.render();
1950
1956
  _this.canvasImage.render();
1951
1957
  _this.patchFlags = false;
@@ -1960,6 +1966,9 @@ var Canvas = /** @class */ (function () {
1960
1966
  if (!isFinite(pen.x)) {
1961
1967
  continue;
1962
1968
  }
1969
+ if (pen.template) {
1970
+ continue;
1971
+ }
1963
1972
  if (pen.calculative.inView) {
1964
1973
  renderPen(ctx, pen);
1965
1974
  }
@@ -2085,7 +2094,7 @@ var Canvas = /** @class */ (function () {
2085
2094
  }
2086
2095
  if (anchor.type === PointType.Line) {
2087
2096
  //旋转的情况
2088
- var _rotate = _this.store.pens[anchor.penId].rotate;
2097
+ var _rotate = _this.store.pens[anchor.penId].rotate || 0;
2089
2098
  if (_this.store.pens[anchor.penId].calculative.flipX) {
2090
2099
  _rotate *= -1;
2091
2100
  }
@@ -2357,6 +2366,7 @@ var Canvas = /** @class */ (function () {
2357
2366
  _this.inputDiv.scrollTop = _this.inputDiv.scrollHeight;
2358
2367
  _this.inputDiv.scrollLeft = _this.inputDiv.scrollWidth;
2359
2368
  pen.calculative.text = undefined;
2369
+ _this.initTemplateCanvas([pen]);
2360
2370
  _this.render();
2361
2371
  };
2362
2372
  this.setInputStyle = function (pen) {
@@ -2547,6 +2557,7 @@ var Canvas = /** @class */ (function () {
2547
2557
  });
2548
2558
  _this.store.emitter.emit('valueUpdate', pen);
2549
2559
  }
2560
+ _this.initTemplateCanvas([pen]);
2550
2561
  }
2551
2562
  _this.inputDiv.dataset.penId = undefined;
2552
2563
  _this.dropdown.style.display = 'none';
@@ -2649,23 +2660,25 @@ var Canvas = /** @class */ (function () {
2649
2660
  _this.render();
2650
2661
  _this.store.emitter.emit('valueUpdate', pen);
2651
2662
  };
2663
+ this.canvasTemplate = new CanvasTemplate(parentElement, store);
2664
+ this.canvasTemplate.canvas.style.zIndex = '1';
2652
2665
  this.canvasImageBottom = new CanvasImage(parentElement, store, true);
2653
- this.canvasImageBottom.canvas.style.zIndex = '1';
2666
+ this.canvasImageBottom.canvas.style.zIndex = '2';
2654
2667
  parentElement.appendChild(this.canvas);
2655
2668
  this.canvas.style.position = 'absolute';
2656
2669
  this.canvas.style.backgroundRepeat = 'no-repeat';
2657
2670
  this.canvas.style.backgroundSize = '100% 100%';
2658
- this.canvas.style.zIndex = '2';
2671
+ this.canvas.style.zIndex = '3';
2659
2672
  this.canvasImage = new CanvasImage(parentElement, store);
2660
- this.canvasImage.canvas.style.zIndex = '3';
2673
+ this.canvasImage.canvas.style.zIndex = '4';
2661
2674
  this.magnifierCanvas = new MagnifierCanvas(this, parentElement, store);
2662
- this.magnifierCanvas.canvas.style.zIndex = '4';
2675
+ this.magnifierCanvas.canvas.style.zIndex = '5';
2663
2676
  this.externalElements.style.position = 'absolute';
2664
2677
  this.externalElements.style.left = '0';
2665
2678
  this.externalElements.style.top = '0';
2666
2679
  this.externalElements.style.outline = 'none';
2667
2680
  this.externalElements.style.background = 'transparent';
2668
- this.externalElements.style.zIndex = '4';
2681
+ this.externalElements.style.zIndex = '5';
2669
2682
  parentElement.style.position = 'relative';
2670
2683
  parentElement.appendChild(this.externalElements);
2671
2684
  this.createInput();
@@ -3237,6 +3250,7 @@ var Canvas = /** @class */ (function () {
3237
3250
  // active 消息表示拖拽结束
3238
3251
  // this.store.emitter.emit('active', this.store.active);
3239
3252
  this.initImageCanvas(this.store.active);
3253
+ this.initTemplateCanvas(this.store.active);
3240
3254
  // 避免选中图元的出错情况,this.dock为undefined
3241
3255
  if (!this.dock)
3242
3256
  return;
@@ -3324,6 +3338,10 @@ var Canvas = /** @class */ (function () {
3324
3338
  pens.some(function (pen) { return _this.hasImage(pen, true); }) &&
3325
3339
  this.canvasImageBottom.init();
3326
3340
  };
3341
+ Canvas.prototype.initTemplateCanvas = function (pens) {
3342
+ pens.some(function (pen) { return pen.template !== undefined; }) &&
3343
+ this.canvasTemplate.init();
3344
+ };
3327
3345
  Canvas.prototype.hasImage = function (pen, isBottom) {
3328
3346
  var _this = this;
3329
3347
  var _a;
@@ -3572,6 +3590,7 @@ var Canvas = /** @class */ (function () {
3572
3590
  this.canvas.style.height = h + 'px';
3573
3591
  this.externalElements.style.width = w + 'px';
3574
3592
  this.externalElements.style.height = h + 'px';
3593
+ this.canvasTemplate.resize(w, h);
3575
3594
  this.canvasImage.resize(w, h);
3576
3595
  this.canvasImageBottom.resize(w, h);
3577
3596
  this.magnifierCanvas.resize(w, h);
@@ -3614,6 +3633,9 @@ var Canvas = /** @class */ (function () {
3614
3633
  this.offscreen
3615
3634
  .getContext('2d')
3616
3635
  .clearRect(0, 0, this.offscreen.width, this.offscreen.height);
3636
+ if (!this.store.data.template) {
3637
+ this.canvasTemplate.clear();
3638
+ }
3617
3639
  this.canvasImage.clear();
3618
3640
  this.canvasImageBottom.clear();
3619
3641
  };
@@ -3829,10 +3851,13 @@ var Canvas = /** @class */ (function () {
3829
3851
  break;
3830
3852
  }
3831
3853
  if (action.type === EditType.Update) {
3832
- this.initImageCanvas(__spreadArray(__spreadArray([], __read(action.pens), false), __read(action.initPens), false));
3854
+ var pens = __spreadArray(__spreadArray([], __read(action.pens), false), __read(action.initPens), false);
3855
+ this.initImageCanvas(pens);
3856
+ this.initTemplateCanvas(pens);
3833
3857
  }
3834
3858
  else {
3835
3859
  this.initImageCanvas(action.pens);
3860
+ this.initTemplateCanvas(action.pens);
3836
3861
  }
3837
3862
  this.parent.onSizeUpdate();
3838
3863
  this.render();
@@ -3844,6 +3869,15 @@ var Canvas = /** @class */ (function () {
3844
3869
  if (!pen.id) {
3845
3870
  pen.id = s8();
3846
3871
  }
3872
+ if (Math.abs(this.store.lastScale - this.store.data.scale) < 0.0001 &&
3873
+ this.store.sameTemplate &&
3874
+ this.store.templatePens[pen.id] &&
3875
+ pen.template) {
3876
+ pen = this.store.templatePens[pen.id];
3877
+ this.store.data.pens.push(pen);
3878
+ this.updatePenRect(pen);
3879
+ return;
3880
+ }
3847
3881
  this.store.data.pens.push(pen);
3848
3882
  this.store.pens[pen.id] = pen;
3849
3883
  // 集中存储path,避免数据冗余过大
@@ -4113,6 +4147,9 @@ var Canvas = /** @class */ (function () {
4113
4147
  pen.calculative.imgNaturalHeight = img.naturalHeight || pen.iconHeight;
4114
4148
  globalStore.htmlElements[pen.image] = img;
4115
4149
  _this.imageLoaded();
4150
+ if (pen.template) {
4151
+ _this.templateImageLoaded();
4152
+ }
4116
4153
  };
4117
4154
  };
4118
4155
  // send the request
@@ -4130,6 +4167,9 @@ var Canvas = /** @class */ (function () {
4130
4167
  pen.calculative.imgNaturalHeight =
4131
4168
  img.naturalHeight || pen.iconHeight;
4132
4169
  this.imageLoaded(); // TODO: 重绘图片层 有延时器,可能卡顿
4170
+ if (pen.template) {
4171
+ this.templateImageLoaded();
4172
+ }
4133
4173
  }
4134
4174
  else {
4135
4175
  if (navigator.userAgent.includes('Firefox') &&
@@ -4159,6 +4199,9 @@ var Canvas = /** @class */ (function () {
4159
4199
  img_1.naturalHeight || pen.iconHeight;
4160
4200
  globalStore.htmlElements[pen.image] = img_1;
4161
4201
  _this.imageLoaded();
4202
+ if (pen.template) {
4203
+ _this.templateImageLoaded();
4204
+ }
4162
4205
  };
4163
4206
  }
4164
4207
  }
@@ -4186,6 +4229,9 @@ var Canvas = /** @class */ (function () {
4186
4229
  pen.calculative.backgroundImg = img_2;
4187
4230
  globalStore.htmlElements[pen.backgroundImage] = img_2;
4188
4231
  _this.imageLoaded();
4232
+ if (pen.template) {
4233
+ _this.templateImageLoaded();
4234
+ }
4189
4235
  };
4190
4236
  }
4191
4237
  }
@@ -4212,6 +4258,9 @@ var Canvas = /** @class */ (function () {
4212
4258
  pen.calculative.strokeImg = img_3;
4213
4259
  globalStore.htmlElements[pen.strokeImage] = img_3;
4214
4260
  _this.imageLoaded();
4261
+ if (pen.template && pen.name !== 'gif') {
4262
+ _this.templateImageLoaded();
4263
+ }
4215
4264
  };
4216
4265
  }
4217
4266
  }
@@ -4229,6 +4278,16 @@ var Canvas = /** @class */ (function () {
4229
4278
  _this.render();
4230
4279
  }, 100);
4231
4280
  };
4281
+ // 避免初始化图片加载重复调用 render,此处防抖
4282
+ Canvas.prototype.templateImageLoaded = function () {
4283
+ var _this = this;
4284
+ this.templateImageTimer && clearTimeout(this.templateImageTimer);
4285
+ this.templateImageTimer = setTimeout(function () {
4286
+ // 加载完图片,重新渲染一次图片层
4287
+ _this.canvasTemplate.init();
4288
+ _this.render();
4289
+ }, 100);
4290
+ };
4232
4291
  Canvas.prototype.setCalculativeByScale = function (pen) {
4233
4292
  var scale = this.store.data.scale;
4234
4293
  pen.calculative.lineWidth = pen.lineWidth * scale;
@@ -4317,6 +4376,7 @@ var Canvas = /** @class */ (function () {
4317
4376
  this.store.data.x = Math.round(this.store.data.x);
4318
4377
  this.store.data.y = Math.round(this.store.data.y);
4319
4378
  setTimeout(function () {
4379
+ _this.canvasTemplate.init();
4320
4380
  _this.canvasImage.init();
4321
4381
  _this.canvasImageBottom.init();
4322
4382
  _this.render();
@@ -4374,7 +4434,8 @@ var Canvas = /** @class */ (function () {
4374
4434
  var _this = this;
4375
4435
  var _a;
4376
4436
  if (center === void 0) { center = { x: 0, y: 0 }; }
4377
- var _b = this.store.options, minScale = _b.minScale, maxScale = _b.maxScale;
4437
+ var minScale = this.store.data.minScale || this.store.options.minScale;
4438
+ var maxScale = this.store.data.maxScale || this.store.options.maxScale;
4378
4439
  if (!(scale >= minScale && scale <= maxScale)) {
4379
4440
  return;
4380
4441
  }
@@ -4411,6 +4472,7 @@ var Canvas = /** @class */ (function () {
4411
4472
  });
4412
4473
  this.calcActiveRect();
4413
4474
  setTimeout(function () {
4475
+ _this.canvasTemplate.init();
4414
4476
  _this.canvasImage.init();
4415
4477
  _this.canvasImageBottom.init();
4416
4478
  var map = _this.parent.map;
@@ -4421,6 +4483,42 @@ var Canvas = /** @class */ (function () {
4421
4483
  _this.store.emitter.emit('scale', _this.store.data.scale);
4422
4484
  });
4423
4485
  };
4486
+ Canvas.prototype.templateScale = function (scale, center) {
4487
+ var _this = this;
4488
+ if (center === void 0) { center = { x: 0, y: 0 }; }
4489
+ var _a = this.store.options, minScale = _a.minScale, maxScale = _a.maxScale;
4490
+ if (!(scale >= minScale && scale <= maxScale)) {
4491
+ return;
4492
+ }
4493
+ var s = scale / this.store.data.scale;
4494
+ this.store.data.scale = scale;
4495
+ this.store.data.center = { x: 0, y: 0 };
4496
+ this.store.data.origin = { x: 0, y: 0 };
4497
+ this.store.data.pens.forEach(function (pen) {
4498
+ if (pen.parentId) {
4499
+ return;
4500
+ }
4501
+ scalePen(pen, s, center);
4502
+ pen.onScale && pen.onScale(pen);
4503
+ if (pen.isRuleLine) {
4504
+ // 扩大线的比例,若是放大,即不缩小,若是缩小,会放大
4505
+ var lineScale = s > 1 ? 1 : 1 / s / s;
4506
+ // 中心点即为线的中心
4507
+ var lineCenter = pen.calculative.worldRect.center;
4508
+ if (!pen.width) {
4509
+ // 垂直线
4510
+ scalePen(pen, lineScale, lineCenter);
4511
+ }
4512
+ else if (!pen.height) {
4513
+ // 水平线
4514
+ scalePen(pen, lineScale, lineCenter);
4515
+ }
4516
+ }
4517
+ // this.updatePenRect(pen, { worldRectIsReady: true });
4518
+ _this.execPenResize(pen);
4519
+ });
4520
+ this.calcActiveRect();
4521
+ };
4424
4522
  Canvas.prototype.rotatePens = function (e) {
4425
4523
  var e_17, _a;
4426
4524
  var _this = this;
@@ -4459,6 +4557,7 @@ var Canvas = /** @class */ (function () {
4459
4557
  this.lastRotate = this.activeRect.rotate;
4460
4558
  this.getSizeCPs();
4461
4559
  this.initImageCanvas(this.store.active);
4560
+ this.initTemplateCanvas(this.store.active);
4462
4561
  this.render();
4463
4562
  this.store.emitter.emit('rotatePens', this.store.active);
4464
4563
  if (this.timer) {
@@ -4543,6 +4642,7 @@ var Canvas = /** @class */ (function () {
4543
4642
  });
4544
4643
  this.getSizeCPs();
4545
4644
  this.initImageCanvas(this.store.active);
4645
+ this.initTemplateCanvas(this.store.active);
4546
4646
  this.render();
4547
4647
  this.store.emitter.emit('resizePens', this.store.active);
4548
4648
  if (this.timer) {
@@ -5048,10 +5148,52 @@ var Canvas = /** @class */ (function () {
5048
5148
  initPens: initPens,
5049
5149
  });
5050
5150
  this.initImageCanvas(pens);
5151
+ this.initTemplateCanvas(pens);
5051
5152
  this.store.emitter.emit('translatePens', pens);
5052
5153
  }
5053
5154
  this.store.emitter.emit('translatingPens', pens);
5054
5155
  };
5156
+ /**
5157
+ * 移动 画笔们
5158
+ * @param pens 画笔们,不包含子节点
5159
+ * @param x 偏移 x
5160
+ * @param y 偏移 y
5161
+ * @param doing 是否持续移动
5162
+ */
5163
+ Canvas.prototype.templateTranslatePens = function (pens, x, y) {
5164
+ var _this = this;
5165
+ if (pens === void 0) { pens = this.store.active; }
5166
+ if (!pens || !pens.length) {
5167
+ return;
5168
+ }
5169
+ var containChildPens = this.getAllByPens(pens);
5170
+ pens.forEach(function (pen) {
5171
+ var _a;
5172
+ if (pen.type === PenType.Line) {
5173
+ if (!_this.store.options.moveConnectedLine && !_this.canMoveLine) {
5174
+ return;
5175
+ }
5176
+ translateLine(pen, x, y);
5177
+ _this.checkDisconnect(pen, containChildPens);
5178
+ _this.store.path2dMap.set(pen, globalStore.path2dDraws[pen.name](pen));
5179
+ }
5180
+ else {
5181
+ translateRect(pen.calculative.worldRect, x, y);
5182
+ _this.updatePenRect(pen, { worldRectIsReady: true });
5183
+ pen.calculative.x = pen.x;
5184
+ pen.calculative.y = pen.y;
5185
+ if (pen.calculative.initRect) {
5186
+ pen.calculative.initRect.x = pen.calculative.x;
5187
+ pen.calculative.initRect.y = pen.calculative.y;
5188
+ pen.calculative.initRect.ex =
5189
+ pen.calculative.x + pen.calculative.width;
5190
+ pen.calculative.initRect.ey =
5191
+ pen.calculative.y + pen.calculative.height;
5192
+ }
5193
+ }
5194
+ (_a = pen.onMove) === null || _a === void 0 ? void 0 : _a.call(pen, pen);
5195
+ });
5196
+ };
5055
5197
  Canvas.prototype.calcAutoAnchor = function (line, lineAnchor, pen, penConnection) {
5056
5198
  var from = getFromAnchor(line);
5057
5199
  var to = getToAnchor(line);
@@ -5754,6 +5896,7 @@ var Canvas = /** @class */ (function () {
5754
5896
  deletePens = [];
5755
5897
  this._del(pens, deletePens);
5756
5898
  this.initImageCanvas(deletePens);
5899
+ this.initTemplateCanvas(deletePens);
5757
5900
  this.inactive();
5758
5901
  this.clearHover();
5759
5902
  this.render();
@@ -6181,6 +6324,9 @@ var Canvas = /** @class */ (function () {
6181
6324
  else {
6182
6325
  this.initImageCanvas([pen]);
6183
6326
  }
6327
+ if (data.template !== undefined || pen.template) {
6328
+ this.initTemplateCanvas([pen]);
6329
+ }
6184
6330
  };
6185
6331
  /**
6186
6332
  * 执行 pen ,以及 pen 的子孙节点的 onResize 生命周期函数
@@ -6497,6 +6643,7 @@ var Canvas = /** @class */ (function () {
6497
6643
  this.store.data.x = this.canvas.clientWidth / 2 - x * rect.width - rect.x;
6498
6644
  this.store.data.y = this.canvas.clientHeight / 2 - y * rect.height - rect.y;
6499
6645
  this.onMovePens();
6646
+ this.canvasTemplate.init();
6500
6647
  this.canvasImage.init();
6501
6648
  this.canvasImageBottom.init();
6502
6649
  this.render();