@idraw/renderer 0.3.0 → 0.4.0-alpha.2

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.
Files changed (73) hide show
  1. package/README.md +1 -1
  2. package/dist/esm/draw/base.d.ts +14 -0
  3. package/dist/esm/draw/base.js +245 -0
  4. package/dist/esm/draw/circle.d.ts +2 -0
  5. package/dist/esm/draw/circle.js +28 -0
  6. package/dist/esm/draw/elements.d.ts +2 -0
  7. package/dist/esm/draw/elements.js +16 -0
  8. package/dist/esm/draw/group.d.ts +3 -0
  9. package/dist/esm/draw/group.js +110 -0
  10. package/dist/esm/draw/html.d.ts +2 -0
  11. package/dist/esm/draw/html.js +14 -0
  12. package/dist/esm/draw/image.d.ts +2 -0
  13. package/dist/esm/draw/image.js +14 -0
  14. package/dist/esm/draw/index.d.ts +7 -0
  15. package/dist/esm/draw/index.js +7 -0
  16. package/dist/esm/draw/path.d.ts +2 -0
  17. package/dist/esm/draw/path.js +49 -0
  18. package/dist/esm/draw/rect.d.ts +2 -0
  19. package/dist/esm/draw/rect.js +23 -0
  20. package/dist/esm/draw/svg.d.ts +2 -0
  21. package/dist/esm/draw/svg.js +14 -0
  22. package/dist/esm/draw/text.d.ts +2 -0
  23. package/dist/esm/draw/text.js +113 -0
  24. package/dist/esm/index.d.ts +9 -24
  25. package/dist/esm/index.js +46 -124
  26. package/dist/esm/loader.d.ts +17 -0
  27. package/dist/esm/loader.js +151 -0
  28. package/dist/index.global.js +767 -856
  29. package/dist/index.global.min.js +1 -1
  30. package/package.json +5 -5
  31. package/LICENSE +0 -21
  32. package/dist/esm/constant/element.d.ts +0 -2
  33. package/dist/esm/constant/element.js +0 -10
  34. package/dist/esm/constant/static.d.ts +0 -11
  35. package/dist/esm/constant/static.js +0 -13
  36. package/dist/esm/lib/calculate.d.ts +0 -5
  37. package/dist/esm/lib/calculate.js +0 -64
  38. package/dist/esm/lib/diff.d.ts +0 -6
  39. package/dist/esm/lib/diff.js +0 -82
  40. package/dist/esm/lib/draw/base.d.ts +0 -5
  41. package/dist/esm/lib/draw/base.js +0 -89
  42. package/dist/esm/lib/draw/circle.d.ts +0 -2
  43. package/dist/esm/lib/draw/circle.js +0 -28
  44. package/dist/esm/lib/draw/html.d.ts +0 -3
  45. package/dist/esm/lib/draw/html.js +0 -9
  46. package/dist/esm/lib/draw/image.d.ts +0 -3
  47. package/dist/esm/lib/draw/image.js +0 -9
  48. package/dist/esm/lib/draw/index.d.ts +0 -3
  49. package/dist/esm/lib/draw/index.js +0 -55
  50. package/dist/esm/lib/draw/rect.d.ts +0 -2
  51. package/dist/esm/lib/draw/rect.js +0 -4
  52. package/dist/esm/lib/draw/svg.d.ts +0 -3
  53. package/dist/esm/lib/draw/svg.js +0 -9
  54. package/dist/esm/lib/draw/text.d.ts +0 -3
  55. package/dist/esm/lib/draw/text.js +0 -128
  56. package/dist/esm/lib/index.d.ts +0 -6
  57. package/dist/esm/lib/index.js +0 -6
  58. package/dist/esm/lib/loader-event.d.ts +0 -33
  59. package/dist/esm/lib/loader-event.js +0 -50
  60. package/dist/esm/lib/loader.d.ts +0 -25
  61. package/dist/esm/lib/loader.js +0 -246
  62. package/dist/esm/lib/parse.d.ts +0 -2
  63. package/dist/esm/lib/parse.js +0 -32
  64. package/dist/esm/lib/renderer-event.d.ts +0 -32
  65. package/dist/esm/lib/renderer-event.js +0 -50
  66. package/dist/esm/lib/temp.d.ts +0 -22
  67. package/dist/esm/lib/temp.js +0 -29
  68. package/dist/esm/lib/transform.d.ts +0 -4
  69. package/dist/esm/lib/transform.js +0 -20
  70. package/dist/esm/lib/value.d.ts +0 -2
  71. package/dist/esm/lib/value.js +0 -7
  72. package/dist/esm/util/filter.d.ts +0 -1
  73. package/dist/esm/util/filter.js +0 -3
@@ -1,13 +1,7 @@
1
- var iDrawRenderer = function() {
1
+ var iDrawRenderer = function(exports) {
2
2
  "use strict";
3
3
  function isColorStr(color2) {
4
- return typeof color2 === "string" && /^\#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(color2);
5
- }
6
- function createUUID() {
7
- function str4() {
8
- return ((1 + Math.random()) * 65536 | 0).toString(16).substring(1);
9
- }
10
- return `${str4()}${str4()}-${str4()}-${str4()}-${str4()}-${str4()}${str4()}${str4()}`;
4
+ return typeof color2 === "string" && (/^\#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(color2) || /^[a-z]{1,}$/i.test(color2));
11
5
  }
12
6
  function deepClone(target) {
13
7
  function _clone(t) {
@@ -26,17 +20,21 @@ var iDrawRenderer = function() {
26
20
  keys.forEach((key) => {
27
21
  obj[key] = _clone(t[key]);
28
22
  });
23
+ const symbolKeys = Object.getOwnPropertySymbols(t);
24
+ symbolKeys.forEach((key) => {
25
+ obj[key] = _clone(t[key]);
26
+ });
29
27
  return obj;
30
28
  }
31
29
  }
32
30
  return _clone(target);
33
31
  }
34
- function is$1(data) {
35
- return Object.prototype.toString.call(data).replace(/[\]|\[]{1,1}/ig, "").split(" ")[1];
32
+ function is$1(target) {
33
+ return Object.prototype.toString.call(target).replace(/[\]|\[]{1,1}/gi, "").split(" ")[1];
36
34
  }
37
35
  function parsePrototype(data) {
38
36
  const typeStr = Object.prototype.toString.call(data) || "";
39
- const result = typeStr.replace(/(\[object|\])/ig, "").trim();
37
+ const result = typeStr.replace(/(\[object|\])/gi, "").trim();
40
38
  return result;
41
39
  }
42
40
  const istype = {
@@ -76,7 +74,10 @@ var iDrawRenderer = function() {
76
74
  const { width, height } = opts;
77
75
  return new Promise((resolve, reject) => {
78
76
  const _svg = `
79
- <svg xmlns="http://www.w3.org/2000/svg" width="${width || ""}" height = "${height || ""}">
77
+ <svg
78
+ xmlns="http://www.w3.org/2000/svg"
79
+ width="${width || ""}"
80
+ height = "${height || ""}">
80
81
  <foreignObject width="100%" height="100%">
81
82
  <div xmlns = "http://www.w3.org/1999/xhtml">
82
83
  ${html2}
@@ -161,7 +162,7 @@ var iDrawRenderer = function() {
161
162
  });
162
163
  }
163
164
  function filterAmpersand(str) {
164
- return str.replace(/\&/ig, "&amp;");
165
+ return str.replace(/\&/gi, "&amp;");
165
166
  }
166
167
  function loadHTML(html2, opts) {
167
168
  return __awaiter(this, void 0, void 0, function* () {
@@ -171,196 +172,6 @@ var iDrawRenderer = function() {
171
172
  return image;
172
173
  });
173
174
  }
174
- class Context {
175
- constructor(ctx, opts) {
176
- this._opts = opts;
177
- this._ctx = ctx;
178
- this._transform = {
179
- scale: 1,
180
- scrollX: 0,
181
- scrollY: 0
182
- };
183
- }
184
- getContext() {
185
- return this._ctx;
186
- }
187
- resetSize(opts) {
188
- this._opts = Object.assign(Object.assign({}, this._opts), opts);
189
- }
190
- calcDeviceNum(num) {
191
- return num * this._opts.devicePixelRatio;
192
- }
193
- calcScreenNum(num) {
194
- return num / this._opts.devicePixelRatio;
195
- }
196
- getSize() {
197
- return {
198
- width: this._opts.width,
199
- height: this._opts.height,
200
- contextWidth: this._opts.contextWidth,
201
- contextHeight: this._opts.contextHeight,
202
- devicePixelRatio: this._opts.devicePixelRatio
203
- };
204
- }
205
- setTransform(config) {
206
- this._transform = Object.assign(Object.assign({}, this._transform), config);
207
- }
208
- getTransform() {
209
- return {
210
- scale: this._transform.scale,
211
- scrollX: this._transform.scrollX,
212
- scrollY: this._transform.scrollY
213
- };
214
- }
215
- setFillStyle(color2) {
216
- this._ctx.fillStyle = color2;
217
- }
218
- fill(fillRule) {
219
- return this._ctx.fill(fillRule || "nonzero");
220
- }
221
- arc(x2, y2, radius, startAngle, endAngle, anticlockwise) {
222
- return this._ctx.arc(this._doSize(x2), this._doSize(y2), this._doSize(radius), startAngle, endAngle, anticlockwise);
223
- }
224
- rect(x2, y2, w2, h2) {
225
- return this._ctx.rect(this._doSize(x2), this._doSize(y2), this._doSize(w2), this._doSize(h2));
226
- }
227
- fillRect(x2, y2, w2, h2) {
228
- return this._ctx.fillRect(this._doSize(x2), this._doSize(y2), this._doSize(w2), this._doSize(h2));
229
- }
230
- clearRect(x2, y2, w2, h2) {
231
- return this._ctx.clearRect(this._doSize(x2), this._doSize(y2), this._doSize(w2), this._doSize(h2));
232
- }
233
- beginPath() {
234
- return this._ctx.beginPath();
235
- }
236
- closePath() {
237
- return this._ctx.closePath();
238
- }
239
- lineTo(x2, y2) {
240
- return this._ctx.lineTo(this._doSize(x2), this._doSize(y2));
241
- }
242
- moveTo(x2, y2) {
243
- return this._ctx.moveTo(this._doSize(x2), this._doSize(y2));
244
- }
245
- arcTo(x1, y1, x2, y2, radius) {
246
- return this._ctx.arcTo(this._doSize(x1), this._doSize(y1), this._doSize(x2), this._doSize(y2), this._doSize(radius));
247
- }
248
- setLineWidth(w2) {
249
- return this._ctx.lineWidth = this._doSize(w2);
250
- }
251
- setLineDash(nums) {
252
- return this._ctx.setLineDash(nums.map((n) => this._doSize(n)));
253
- }
254
- isPointInPath(x2, y2) {
255
- return this._ctx.isPointInPath(this._doX(x2), this._doY(y2));
256
- }
257
- isPointInPathWithoutScroll(x2, y2) {
258
- return this._ctx.isPointInPath(this._doSize(x2), this._doSize(y2));
259
- }
260
- setStrokeStyle(color2) {
261
- this._ctx.strokeStyle = color2;
262
- }
263
- stroke() {
264
- return this._ctx.stroke();
265
- }
266
- translate(x2, y2) {
267
- return this._ctx.translate(this._doSize(x2), this._doSize(y2));
268
- }
269
- rotate(angle2) {
270
- return this._ctx.rotate(angle2);
271
- }
272
- drawImage(...args) {
273
- const image = args[0];
274
- const sx = args[1];
275
- const sy = args[2];
276
- const sw = args[3];
277
- const sh = args[4];
278
- const dx = args[args.length - 4];
279
- const dy = args[args.length - 3];
280
- const dw = args[args.length - 2];
281
- const dh = args[args.length - 1];
282
- if (args.length === 9) {
283
- return this._ctx.drawImage(image, this._doSize(sx), this._doSize(sy), this._doSize(sw), this._doSize(sh), this._doSize(dx), this._doSize(dy), this._doSize(dw), this._doSize(dh));
284
- } else {
285
- return this._ctx.drawImage(image, this._doSize(dx), this._doSize(dy), this._doSize(dw), this._doSize(dh));
286
- }
287
- }
288
- createPattern(image, repetition) {
289
- return this._ctx.createPattern(image, repetition);
290
- }
291
- measureText(text2) {
292
- return this._ctx.measureText(text2);
293
- }
294
- setTextAlign(align) {
295
- this._ctx.textAlign = align;
296
- }
297
- fillText(text2, x2, y2, maxWidth) {
298
- if (maxWidth !== void 0) {
299
- return this._ctx.fillText(text2, this._doSize(x2), this._doSize(y2), this._doSize(maxWidth));
300
- } else {
301
- return this._ctx.fillText(text2, this._doSize(x2), this._doSize(y2));
302
- }
303
- }
304
- strokeText(text2, x2, y2, maxWidth) {
305
- if (maxWidth !== void 0) {
306
- return this._ctx.strokeText(text2, this._doSize(x2), this._doSize(y2), this._doSize(maxWidth));
307
- } else {
308
- return this._ctx.strokeText(text2, this._doSize(x2), this._doSize(y2));
309
- }
310
- }
311
- setFont(opts) {
312
- const strList = [];
313
- if (opts.fontWeight === "bold") {
314
- strList.push(`${opts.fontWeight}`);
315
- }
316
- strList.push(`${this._doSize(opts.fontSize || 12)}px`);
317
- strList.push(`${opts.fontFamily || "sans-serif"}`);
318
- this._ctx.font = `${strList.join(" ")}`;
319
- }
320
- setTextBaseline(baseline) {
321
- this._ctx.textBaseline = baseline;
322
- }
323
- setGlobalAlpha(alpha) {
324
- this._ctx.globalAlpha = alpha;
325
- }
326
- save() {
327
- this._ctx.save();
328
- }
329
- restore() {
330
- this._ctx.restore();
331
- }
332
- scale(ratioX, ratioY) {
333
- this._ctx.scale(ratioX, ratioY);
334
- }
335
- setShadowColor(color2) {
336
- this._ctx.shadowColor = color2;
337
- }
338
- setShadowOffsetX(offsetX) {
339
- this._ctx.shadowOffsetX = this._doSize(offsetX);
340
- }
341
- setShadowOffsetY(offsetY) {
342
- this._ctx.shadowOffsetY = this._doSize(offsetY);
343
- }
344
- setShadowBlur(blur) {
345
- this._ctx.shadowBlur = this._doSize(blur);
346
- }
347
- ellipse(x2, y2, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) {
348
- this._ctx.ellipse(this._doSize(x2), this._doSize(y2), this._doSize(radiusX), this._doSize(radiusY), rotation, startAngle, endAngle, counterclockwise);
349
- }
350
- _doSize(num) {
351
- return this._opts.devicePixelRatio * num;
352
- }
353
- _doX(x2) {
354
- const { scale, scrollX } = this._transform;
355
- const _x = (x2 - scrollX) / scale;
356
- return this._doSize(_x);
357
- }
358
- _doY(y2) {
359
- const { scale, scrollY } = this._transform;
360
- const _y = (y2 - scrollY) / scale;
361
- return this._doSize(_y);
362
- }
363
- }
364
175
  function number(value) {
365
176
  return typeof value === "number" && (value > 0 || value <= 0);
366
177
  }
@@ -433,6 +244,9 @@ var iDrawRenderer = function() {
433
244
  function fontWeight(value) {
434
245
  return ["bold"].includes(value);
435
246
  }
247
+ function numberStr(value) {
248
+ return /^(-?\d+(?:\.\d+)?)$/.test(`${value}`);
249
+ }
436
250
  const is = {
437
251
  x,
438
252
  y,
@@ -440,6 +254,7 @@ var iDrawRenderer = function() {
440
254
  h,
441
255
  angle,
442
256
  number,
257
+ numberStr,
443
258
  borderWidth,
444
259
  borderRadius,
445
260
  color,
@@ -456,22 +271,60 @@ var iDrawRenderer = function() {
456
271
  fontWeight,
457
272
  strokeWidth
458
273
  };
274
+ class EventEmitter {
275
+ constructor() {
276
+ this._listeners = /* @__PURE__ */ new Map();
277
+ }
278
+ on(eventKey, callback) {
279
+ if (this._listeners.has(eventKey)) {
280
+ const callbacks = this._listeners.get(eventKey) || [];
281
+ callbacks === null || callbacks === void 0 ? void 0 : callbacks.push(callback);
282
+ this._listeners.set(eventKey, callbacks);
283
+ } else {
284
+ this._listeners.set(eventKey, [callback]);
285
+ }
286
+ }
287
+ off(eventKey, callback) {
288
+ if (this._listeners.has(eventKey)) {
289
+ const callbacks = this._listeners.get(eventKey);
290
+ if (Array.isArray(callbacks)) {
291
+ for (let i = 0; i < (callbacks === null || callbacks === void 0 ? void 0 : callbacks.length); i++) {
292
+ if (callbacks[i] === callback) {
293
+ callbacks.splice(i, 1);
294
+ break;
295
+ }
296
+ }
297
+ }
298
+ this._listeners.set(eventKey, callbacks || []);
299
+ }
300
+ }
301
+ trigger(eventKey, e) {
302
+ const callbacks = this._listeners.get(eventKey);
303
+ if (Array.isArray(callbacks)) {
304
+ callbacks.forEach((cb) => {
305
+ cb(e);
306
+ });
307
+ return true;
308
+ } else {
309
+ return false;
310
+ }
311
+ }
312
+ has(name) {
313
+ if (this._listeners.has(name)) {
314
+ const list = this._listeners.get(name);
315
+ if (Array.isArray(list) && list.length > 0) {
316
+ return true;
317
+ }
318
+ }
319
+ return false;
320
+ }
321
+ }
459
322
  function parseAngleToRadian(angle2) {
460
323
  return angle2 / 180 * Math.PI;
461
324
  }
462
- function calcElementCenter(elem) {
463
- const p = {
464
- x: elem.x + elem.w / 2,
465
- y: elem.y + elem.h / 2
466
- };
467
- return p;
468
- }
469
- function rotateElement(ctx, elem, callback) {
470
- const center = calcElementCenter(elem);
471
- const radian = parseAngleToRadian(elem.angle || 0);
472
- return rotateContext(ctx, center, radian || 0, callback);
473
- }
474
- function rotateContext(ctx, center, radian, callback) {
325
+ function rotateElement(ctx, elemSize, callback) {
326
+ const center = calcElementCenter(elemSize);
327
+ const radian = parseAngleToRadian(elemSize.angle || 0);
475
328
  if (center && (radian > 0 || radian < 0)) {
476
329
  ctx.translate(center.x, center.y);
477
330
  ctx.rotate(radian);
@@ -484,28 +337,104 @@ var iDrawRenderer = function() {
484
337
  ctx.translate(-center.x, -center.y);
485
338
  }
486
339
  }
487
- function clearContext(ctx) {
488
- ctx.setFillStyle("#000000");
489
- ctx.setStrokeStyle("#000000");
490
- ctx.setLineDash([]);
491
- ctx.setGlobalAlpha(1);
492
- ctx.setShadowColor("#00000000");
493
- ctx.setShadowOffsetX(0);
494
- ctx.setShadowOffsetY(0);
495
- ctx.setShadowBlur(0);
340
+ function calcElementCenter(elem) {
341
+ const p = {
342
+ x: elem.x + elem.w / 2,
343
+ y: elem.y + elem.h / 2
344
+ };
345
+ return p;
496
346
  }
497
- function drawBgColor(ctx, color2) {
498
- const size = ctx.getSize();
499
- ctx.setFillStyle(color2);
500
- ctx.fillRect(0, 0, size.contextWidth, size.contextHeight);
347
+ function generateSVGPath(commands) {
348
+ let path = "";
349
+ commands.forEach((item) => {
350
+ path += item.type + item.params.join(" ");
351
+ });
352
+ return path;
501
353
  }
502
- function drawBox(ctx, elem, pattern) {
503
- clearContext(ctx);
504
- drawBoxBorder(ctx, elem);
505
- clearContext(ctx);
506
- rotateElement(ctx, elem, () => {
507
- const { x: x2, y: y2, w: w2, h: h2 } = elem;
508
- let r = elem.desc.borderRadius || 0;
354
+ function drawCircle(ctx, elem, opts) {
355
+ const { detail, angle: angle2 } = elem;
356
+ const { background = "#000000", borderColor = "#000000", borderWidth: borderWidth2 = 0 } = detail;
357
+ const { calculator, viewScaleInfo, viewSizeInfo } = opts;
358
+ const { x: x2, y: y2, w: w2, h: h2 } = calculator.elementSize({ x: elem.x, y: elem.y, w: elem.w, h: elem.h }, viewScaleInfo, viewSizeInfo);
359
+ rotateElement(ctx, { x: x2, y: y2, w: w2, h: h2, angle: angle2 }, () => {
360
+ const a = w2 / 2;
361
+ const b = h2 / 2;
362
+ const centerX = x2 + a;
363
+ const centerY = y2 + b;
364
+ if (borderWidth2 && borderWidth2 > 0) {
365
+ const ba = borderWidth2 / 2 + a;
366
+ const bb = borderWidth2 / 2 + b;
367
+ ctx.beginPath();
368
+ ctx.strokeStyle = borderColor;
369
+ ctx.lineWidth = borderWidth2;
370
+ ctx.circle(centerX, centerY, ba, bb, 0, 0, 2 * Math.PI);
371
+ ctx.closePath();
372
+ ctx.stroke();
373
+ }
374
+ ctx.beginPath();
375
+ ctx.fillStyle = background;
376
+ ctx.circle(centerX, centerY, a, b, 0, 0, 2 * Math.PI);
377
+ ctx.closePath();
378
+ ctx.fill();
379
+ });
380
+ }
381
+ function drawBox(ctx, viewElem, opts) {
382
+ var _a, _b;
383
+ if (((_a = viewElem == null ? void 0 : viewElem.detail) == null ? void 0 : _a.opacity) !== void 0 && ((_b = viewElem == null ? void 0 : viewElem.detail) == null ? void 0 : _b.opacity) > 0) {
384
+ ctx.globalAlpha = viewElem.detail.opacity;
385
+ } else {
386
+ ctx.globalAlpha = 1;
387
+ }
388
+ const { pattern, renderContent, originElem, calcElemSize, viewScaleInfo, viewSizeInfo } = opts || {};
389
+ drawClipPath(ctx, viewElem, {
390
+ originElem,
391
+ calcElemSize,
392
+ viewScaleInfo,
393
+ viewSizeInfo,
394
+ renderContent: () => {
395
+ drawBoxBorder(ctx, viewElem, { viewScaleInfo, viewSizeInfo });
396
+ drawBoxBackground(ctx, viewElem, { pattern, viewScaleInfo, viewSizeInfo });
397
+ renderContent == null ? void 0 : renderContent();
398
+ }
399
+ });
400
+ ctx.globalAlpha = 1;
401
+ }
402
+ function drawClipPath(ctx, viewElem, opts) {
403
+ const { renderContent, originElem, calcElemSize, viewScaleInfo, viewSizeInfo } = opts;
404
+ const totalScale = viewScaleInfo.scale * viewSizeInfo.devicePixelRatio;
405
+ const { clipPath } = (originElem == null ? void 0 : originElem.detail) || {};
406
+ if (clipPath && calcElemSize && clipPath.commands) {
407
+ const { x: x2, y: y2, w: w2, h: h2 } = calcElemSize;
408
+ const { originW, originH, originX, originY } = clipPath;
409
+ const scaleW = w2 / originW;
410
+ const scaleH = h2 / originH;
411
+ const viewOriginX = originX * scaleW;
412
+ const viewOriginY = originY * scaleH;
413
+ let internalX = x2 - viewOriginX;
414
+ let internalY = y2 - viewOriginY;
415
+ ctx.save();
416
+ ctx.translate(internalX, internalY);
417
+ ctx.scale(totalScale * scaleW, totalScale * scaleH);
418
+ const pathStr = generateSVGPath(clipPath.commands || []);
419
+ const path2d = new Path2D(pathStr);
420
+ ctx.clip(path2d);
421
+ ctx.translate(0 - internalX, 0 - internalY);
422
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
423
+ rotateElement(ctx, { ...viewElem }, () => {
424
+ renderContent == null ? void 0 : renderContent();
425
+ });
426
+ ctx.restore();
427
+ } else {
428
+ renderContent == null ? void 0 : renderContent();
429
+ }
430
+ }
431
+ function drawBoxBackground(ctx, viewElem, opts) {
432
+ var _a, _b;
433
+ const { pattern, viewScaleInfo } = opts;
434
+ let transform = [];
435
+ if (viewElem.detail.background || pattern) {
436
+ const { x: x2, y: y2, w: w2, h: h2 } = viewElem;
437
+ let r = (viewElem.detail.borderRadius || 0) * viewScaleInfo.scale;
509
438
  r = Math.min(r, w2 / 2, h2 / 2);
510
439
  if (w2 < r * 2 || h2 < r * 2) {
511
440
  r = 0;
@@ -518,49 +447,145 @@ var iDrawRenderer = function() {
518
447
  ctx.arcTo(x2, y2, x2 + w2, y2, r);
519
448
  ctx.closePath();
520
449
  if (typeof pattern === "string") {
521
- ctx.setFillStyle(pattern);
450
+ ctx.fillStyle = pattern;
522
451
  } else if (["CanvasPattern"].includes(istype.type(pattern))) {
523
- ctx.setFillStyle(pattern);
452
+ ctx.fillStyle = pattern;
453
+ } else if (typeof viewElem.detail.background === "string") {
454
+ ctx.fillStyle = viewElem.detail.background;
455
+ } else if (((_a = viewElem.detail.background) == null ? void 0 : _a.type) === "linearGradient") {
456
+ const { start, end, stops } = viewElem.detail.background;
457
+ const viewStart = {
458
+ x: start.x + x2,
459
+ y: start.y + y2
460
+ };
461
+ const viewEnd = {
462
+ x: end.x + x2,
463
+ y: end.y + y2
464
+ };
465
+ const linearGradient = ctx.createLinearGradient(viewStart.x, viewStart.y, viewEnd.x, viewEnd.y);
466
+ stops.forEach((stop) => {
467
+ linearGradient.addColorStop(stop.offset, stop.color);
468
+ });
469
+ ctx.fillStyle = linearGradient;
470
+ } else if (((_b = viewElem.detail.background) == null ? void 0 : _b.type) === "radialGradient") {
471
+ const { inner, outer, stops } = viewElem.detail.background;
472
+ transform = viewElem.detail.background.transform || [];
473
+ const viewInner = {
474
+ x: inner.x,
475
+ y: inner.y,
476
+ radius: inner.radius * viewScaleInfo.scale
477
+ };
478
+ const viewOuter = {
479
+ x: outer.x,
480
+ y: outer.y,
481
+ radius: outer.radius * viewScaleInfo.scale
482
+ };
483
+ const radialGradient = ctx.createRadialGradient(viewInner.x, viewInner.y, viewInner.radius, viewOuter.x, viewOuter.y, viewOuter.radius);
484
+ stops.forEach((stop) => {
485
+ radialGradient.addColorStop(stop.offset, stop.color);
486
+ });
487
+ ctx.fillStyle = radialGradient;
488
+ if (transform && transform.length > 0) {
489
+ for (let i = 0; i < (transform == null ? void 0 : transform.length); i++) {
490
+ const action = transform[i];
491
+ if (action.method === "translate") {
492
+ ctx.translate(action.args[0] + x2, action.args[1] + y2);
493
+ } else if (action.method === "rotate") {
494
+ ctx.rotate(...action.args);
495
+ } else if (action.method === "scale") {
496
+ ctx.scale(...action.args);
497
+ }
498
+ }
499
+ }
524
500
  }
525
501
  ctx.fill();
526
- });
527
- }
528
- function drawBoxBorder(ctx, elem) {
529
- clearContext(ctx);
530
- rotateElement(ctx, elem, () => {
531
- if (!(elem.desc.borderWidth && elem.desc.borderWidth > 0)) {
532
- return;
502
+ if (transform && transform.length > 0) {
503
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
533
504
  }
534
- const bw = elem.desc.borderWidth;
535
- let borderColor = "#000000";
536
- if (isColorStr(elem.desc.borderColor) === true) {
537
- borderColor = elem.desc.borderColor;
505
+ }
506
+ }
507
+ function drawBoxBorder(ctx, viewElem, opts) {
508
+ if (!isColorStr(viewElem.detail.borderColor)) {
509
+ return;
510
+ }
511
+ const { viewScaleInfo } = opts;
512
+ let borderColor = "#000000";
513
+ if (isColorStr(viewElem.detail.borderColor) === true) {
514
+ borderColor = viewElem.detail.borderColor;
515
+ }
516
+ const { borderWidth: borderWidth2, borderRadius: borderRadius2, borderDash } = viewElem.detail;
517
+ let bw = 0;
518
+ if (typeof borderWidth2 === "number") {
519
+ bw = borderWidth2 || 1;
520
+ }
521
+ bw = bw * viewScaleInfo.scale;
522
+ let r = borderRadius2 || 0;
523
+ ctx.strokeStyle = borderColor;
524
+ ctx.setLineDash(borderDash || []);
525
+ let borderTop = 0;
526
+ let borderRight = 0;
527
+ let borderBottom = 0;
528
+ let borderLeft = 0;
529
+ if (Array.isArray(borderWidth2)) {
530
+ borderTop = borderWidth2[0] || 0;
531
+ borderRight = borderWidth2[1] || 0;
532
+ borderBottom = borderWidth2[2] || 0;
533
+ borderLeft = borderWidth2[3] || 0;
534
+ }
535
+ if (borderLeft || borderRight || borderTop || borderBottom) {
536
+ const { x: x2, y: y2, w: w2, h: h2 } = viewElem;
537
+ if (borderLeft) {
538
+ ctx.beginPath();
539
+ ctx.lineWidth = borderLeft * viewScaleInfo.scale;
540
+ ctx.moveTo(x2, y2);
541
+ ctx.lineTo(x2, y2 + h2);
542
+ ctx.closePath();
543
+ ctx.stroke();
538
544
  }
539
- const x2 = elem.x - bw / 2;
540
- const y2 = elem.y - bw / 2;
541
- const w2 = elem.w + bw;
542
- const h2 = elem.h + bw;
543
- let r = elem.desc.borderRadius || 0;
544
- r = Math.min(r, w2 / 2, h2 / 2);
545
- if (r < w2 / 2 && r < h2 / 2) {
546
- r = r + bw / 2;
545
+ if (borderRight) {
546
+ ctx.beginPath();
547
+ ctx.lineWidth = borderRight * viewScaleInfo.scale;
548
+ ctx.moveTo(x2 + w2, y2);
549
+ ctx.lineTo(x2 + w2, y2 + h2);
550
+ ctx.closePath();
551
+ ctx.stroke();
547
552
  }
548
- const { desc } = elem;
549
- if (desc.shadowColor !== void 0 && isColorStr(desc.shadowColor)) {
550
- ctx.setShadowColor(desc.shadowColor);
553
+ if (borderTop) {
554
+ ctx.beginPath();
555
+ ctx.lineWidth = borderTop * viewScaleInfo.scale;
556
+ ctx.moveTo(x2, y2);
557
+ ctx.lineTo(x2 + w2, y2);
558
+ ctx.closePath();
559
+ ctx.stroke();
551
560
  }
552
- if (desc.shadowOffsetX !== void 0 && is.number(desc.shadowOffsetX)) {
553
- ctx.setShadowOffsetX(desc.shadowOffsetX);
561
+ if (borderBottom) {
562
+ ctx.beginPath();
563
+ ctx.lineWidth = borderBottom * viewScaleInfo.scale;
564
+ ctx.moveTo(x2, y2 + h2);
565
+ ctx.lineTo(x2 + w2, y2 + h2);
566
+ ctx.closePath();
567
+ ctx.stroke();
554
568
  }
555
- if (desc.shadowOffsetY !== void 0 && is.number(desc.shadowOffsetY)) {
556
- ctx.setShadowOffsetY(desc.shadowOffsetY);
569
+ } else {
570
+ let { x: x2, y: y2, w: w2, h: h2 } = viewElem;
571
+ const { boxSizing } = viewElem.detail;
572
+ if (boxSizing === "border-box") {
573
+ x2 = viewElem.x;
574
+ y2 = viewElem.y;
575
+ w2 = viewElem.w;
576
+ h2 = viewElem.h;
577
+ } else {
578
+ x2 = viewElem.x - bw;
579
+ y2 = viewElem.y - bw;
580
+ w2 = viewElem.w + bw * 2;
581
+ h2 = viewElem.h + bw * 2;
557
582
  }
558
- if (desc.shadowBlur !== void 0 && is.number(desc.shadowBlur)) {
559
- ctx.setShadowBlur(desc.shadowBlur);
583
+ r = Math.min(r, w2 / 2, h2 / 2);
584
+ if (r < w2 / 2 && r < h2 / 2) {
585
+ r = r + bw / 2;
560
586
  }
561
587
  ctx.beginPath();
562
- ctx.setLineWidth(bw);
563
- ctx.setStrokeStyle(borderColor);
588
+ ctx.lineWidth = bw;
564
589
  ctx.moveTo(x2 + r, y2);
565
590
  ctx.arcTo(x2 + w2, y2, x2 + w2, y2 + h2, r);
566
591
  ctx.arcTo(x2 + w2, y2 + h2, x2, y2 + h2, r);
@@ -568,683 +593,569 @@ var iDrawRenderer = function() {
568
593
  ctx.arcTo(x2, y2, x2 + w2, y2, r);
569
594
  ctx.closePath();
570
595
  ctx.stroke();
571
- });
596
+ }
572
597
  }
573
- function drawRect(ctx, elem) {
574
- drawBox(ctx, elem, elem.desc.bgColor);
598
+ function drawBoxShadow(ctx, viewElem, opts) {
599
+ const { detail } = viewElem;
600
+ const { viewScaleInfo, renderContent } = opts;
601
+ const { shadowColor, shadowOffsetX, shadowOffsetY, shadowBlur } = detail;
602
+ if (is.number(shadowBlur)) {
603
+ ctx.save();
604
+ ctx.shadowColor = shadowColor || "#000000";
605
+ ctx.shadowOffsetX = (shadowOffsetX || 0) * viewScaleInfo.scale;
606
+ ctx.shadowOffsetY = (shadowOffsetY || 0) * viewScaleInfo.scale;
607
+ ctx.shadowBlur = (shadowBlur || 0) * viewScaleInfo.scale;
608
+ renderContent();
609
+ ctx.restore();
610
+ } else {
611
+ renderContent();
612
+ }
575
613
  }
576
- function drawImage(ctx, elem, loader) {
577
- const content = loader.getContent(elem.uuid);
578
- rotateElement(ctx, elem, () => {
579
- if (content) {
580
- ctx.drawImage(content, elem.x, elem.y, elem.w, elem.h);
614
+ function drawRect(ctx, elem, opts) {
615
+ const { calculator, viewScaleInfo, viewSizeInfo } = opts;
616
+ let { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = calculator.elementSize(elem, viewScaleInfo, viewSizeInfo);
617
+ const viewElem = { ...elem, ...{ x: x2, y: y2, w: w2, h: h2, angle: angle2 } };
618
+ rotateElement(ctx, { x: x2, y: y2, w: w2, h: h2, angle: angle2 }, () => {
619
+ drawBoxShadow(ctx, viewElem, {
620
+ viewScaleInfo,
621
+ viewSizeInfo,
622
+ renderContent: () => {
623
+ drawBox(ctx, viewElem, {
624
+ originElem: elem,
625
+ calcElemSize: { x: x2, y: y2, w: w2, h: h2, angle: angle2 },
626
+ viewScaleInfo,
627
+ viewSizeInfo,
628
+ renderContent: () => {
629
+ }
630
+ });
631
+ }
632
+ });
633
+ });
634
+ }
635
+ function drawImage(ctx, elem, opts) {
636
+ const content = opts.loader.getContent(elem.uuid);
637
+ const { calculator, viewScaleInfo, viewSizeInfo } = opts;
638
+ const { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = calculator.elementSize(elem, viewScaleInfo, viewSizeInfo);
639
+ rotateElement(ctx, { x: x2, y: y2, w: w2, h: h2, angle: angle2 }, () => {
640
+ if (!content) {
641
+ opts.loader.load(elem, opts.elementAssets || {});
642
+ }
643
+ if (elem.type === "image" && content) {
644
+ ctx.drawImage(content, x2, y2, w2, h2);
581
645
  }
582
646
  });
583
647
  }
584
- function drawSVG(ctx, elem, loader) {
585
- const content = loader.getContent(elem.uuid);
586
- rotateElement(ctx, elem, () => {
587
- if (content) {
588
- ctx.drawImage(content, elem.x, elem.y, elem.w, elem.h);
648
+ function drawSVG(ctx, elem, opts) {
649
+ const content = opts.loader.getContent(elem.uuid);
650
+ const { calculator, viewScaleInfo, viewSizeInfo } = opts;
651
+ const { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = calculator.elementSize(elem, viewScaleInfo, viewSizeInfo);
652
+ rotateElement(ctx, { x: x2, y: y2, w: w2, h: h2, angle: angle2 }, () => {
653
+ if (!content) {
654
+ opts.loader.load(elem, opts.elementAssets || {});
655
+ }
656
+ if (elem.type === "svg" && content) {
657
+ ctx.drawImage(content, x2, y2, w2, h2);
589
658
  }
590
659
  });
591
660
  }
592
- function drawHTML(ctx, elem, loader) {
593
- const content = loader.getContent(elem.uuid);
594
- rotateElement(ctx, elem, () => {
595
- if (content) {
596
- ctx.drawImage(content, elem.x, elem.y, elem.w, elem.h);
661
+ function drawHTML(ctx, elem, opts) {
662
+ const content = opts.loader.getContent(elem.uuid);
663
+ const { calculator, viewScaleInfo, viewSizeInfo } = opts;
664
+ const { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = calculator.elementSize(elem, viewScaleInfo, viewSizeInfo);
665
+ rotateElement(ctx, { x: x2, y: y2, w: w2, h: h2, angle: angle2 }, () => {
666
+ if (!content) {
667
+ opts.loader.load(elem);
668
+ }
669
+ if (elem.type === "html" && content) {
670
+ ctx.drawImage(content, x2, y2, w2, h2);
597
671
  }
598
672
  });
599
673
  }
600
- function drawText(ctx, elem, loader) {
601
- clearContext(ctx);
602
- drawBox(ctx, elem, elem.desc.bgColor || "transparent");
603
- rotateElement(ctx, elem, () => {
604
- const desc = {
605
- ...{
606
- fontSize: 12,
607
- fontFamily: "sans-serif",
608
- textAlign: "center"
609
- },
610
- ...elem.desc
611
- };
612
- ctx.setFillStyle(elem.desc.color);
613
- ctx.setTextBaseline("top");
614
- ctx.setFont({
615
- fontWeight: desc.fontWeight,
616
- fontSize: desc.fontSize,
617
- fontFamily: desc.fontFamily
618
- });
619
- const descText = desc.text.replace(/\r\n/gi, "\n");
620
- const fontHeight = desc.lineHeight || desc.fontSize;
621
- const descTextList = descText.split("\n");
622
- const lines = [];
623
- let lineNum = 0;
624
- descTextList.forEach((tempText, idx) => {
625
- let lineText = "";
626
- if (tempText.length > 0) {
627
- for (let i = 0; i < tempText.length; i++) {
628
- if (ctx.measureText(lineText + (tempText[i] || "")).width < ctx.calcDeviceNum(elem.w)) {
629
- lineText += tempText[i] || "";
674
+ function drawText(ctx, elem, opts) {
675
+ const { calculator, viewScaleInfo, viewSizeInfo } = opts;
676
+ const { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = calculator.elementSize(elem, viewScaleInfo, viewSizeInfo);
677
+ const viewElem = { ...elem, ...{ x: x2, y: y2, w: w2, h: h2, angle: angle2 } };
678
+ rotateElement(ctx, { x: x2, y: y2, w: w2, h: h2, angle: angle2 }, () => {
679
+ drawBox(ctx, viewElem, {
680
+ originElem: elem,
681
+ calcElemSize: { x: x2, y: y2, w: w2, h: h2, angle: angle2 },
682
+ viewScaleInfo,
683
+ viewSizeInfo,
684
+ renderContent: () => {
685
+ const detail = {
686
+ ...{
687
+ fontSize: 12,
688
+ fontFamily: "sans-serif",
689
+ textAlign: "center"
690
+ },
691
+ ...elem.detail
692
+ };
693
+ const fontSize2 = detail.fontSize * viewScaleInfo.scale;
694
+ const lineHeight2 = detail.lineHeight ? detail.lineHeight * viewScaleInfo.scale : fontSize2;
695
+ ctx.fillStyle = elem.detail.color;
696
+ ctx.textBaseline = "top";
697
+ ctx.$setFont({
698
+ fontWeight: detail.fontWeight,
699
+ fontSize: fontSize2,
700
+ fontFamily: detail.fontFamily
701
+ });
702
+ const detailText = detail.text.replace(/\r\n/gi, "\n");
703
+ const fontHeight = lineHeight2;
704
+ const detailTextList = detailText.split("\n");
705
+ const lines = [];
706
+ let lineNum = 0;
707
+ detailTextList.forEach((tempText, idx) => {
708
+ let lineText = "";
709
+ if (tempText.length > 0) {
710
+ for (let i = 0; i < tempText.length; i++) {
711
+ if (ctx.measureText(lineText + (tempText[i] || "")).width < ctx.$doPixelRatio(w2)) {
712
+ lineText += tempText[i] || "";
713
+ } else {
714
+ lines.push({
715
+ text: lineText,
716
+ width: ctx.$undoPixelRatio(ctx.measureText(lineText).width)
717
+ });
718
+ lineText = tempText[i] || "";
719
+ lineNum++;
720
+ }
721
+ if ((lineNum + 1) * fontHeight > h2) {
722
+ break;
723
+ }
724
+ if (tempText.length - 1 === i) {
725
+ if ((lineNum + 1) * fontHeight < h2) {
726
+ lines.push({
727
+ text: lineText,
728
+ width: ctx.$undoPixelRatio(ctx.measureText(lineText).width)
729
+ });
730
+ if (idx < detailTextList.length - 1) {
731
+ lineNum++;
732
+ }
733
+ break;
734
+ }
735
+ }
736
+ }
630
737
  } else {
631
738
  lines.push({
632
- text: lineText,
633
- width: ctx.calcScreenNum(ctx.measureText(lineText).width)
739
+ text: "",
740
+ width: 0
634
741
  });
635
- lineText = tempText[i] || "";
636
- lineNum++;
637
742
  }
638
- if ((lineNum + 1) * fontHeight > elem.h) {
639
- break;
743
+ });
744
+ let startY = 0;
745
+ if (lines.length * fontHeight < h2) {
746
+ if (elem.detail.verticalAlign === "top") {
747
+ startY = 0;
748
+ } else if (elem.detail.verticalAlign === "bottom") {
749
+ startY += h2 - lines.length * fontHeight;
750
+ } else {
751
+ startY += (h2 - lines.length * fontHeight) / 2;
640
752
  }
641
- if (tempText.length - 1 === i) {
642
- if ((lineNum + 1) * fontHeight < elem.h) {
643
- lines.push({
644
- text: lineText,
645
- width: ctx.calcScreenNum(ctx.measureText(lineText).width)
646
- });
647
- if (idx < descTextList.length - 1) {
648
- lineNum++;
649
- }
650
- break;
651
- }
753
+ }
754
+ {
755
+ const _y = y2 + startY;
756
+ if (detail.textShadowColor !== void 0 && isColorStr(detail.textShadowColor)) {
757
+ ctx.shadowColor = detail.textShadowColor;
758
+ }
759
+ if (detail.textShadowOffsetX !== void 0 && is.number(detail.textShadowOffsetX)) {
760
+ ctx.shadowOffsetX = detail.textShadowOffsetX;
761
+ }
762
+ if (detail.textShadowOffsetY !== void 0 && is.number(detail.textShadowOffsetY)) {
763
+ ctx.shadowOffsetY = detail.textShadowOffsetY;
764
+ }
765
+ if (detail.textShadowBlur !== void 0 && is.number(detail.textShadowBlur)) {
766
+ ctx.shadowBlur = detail.textShadowBlur;
652
767
  }
768
+ lines.forEach((line, i) => {
769
+ let _x = x2;
770
+ if (detail.textAlign === "center") {
771
+ _x = x2 + (w2 - line.width) / 2;
772
+ } else if (detail.textAlign === "right") {
773
+ _x = x2 + (w2 - line.width);
774
+ }
775
+ ctx.fillText(line.text, _x, _y + fontHeight * i);
776
+ });
653
777
  }
654
- } else {
655
- lines.push({
656
- text: "",
657
- width: 0
658
- });
659
778
  }
660
779
  });
661
- let startY = 0;
662
- if (lines.length * fontHeight < elem.h) {
663
- if (elem.desc.verticalAlign === "top") {
664
- startY = 0;
665
- } else if (elem.desc.verticalAlign === "bottom") {
666
- startY += elem.h - lines.length * fontHeight;
667
- } else {
668
- startY += (elem.h - lines.length * fontHeight) / 2;
669
- }
670
- }
671
- {
672
- const _y = elem.y + startY;
673
- if (desc.textShadowColor !== void 0 && isColorStr(desc.textShadowColor)) {
674
- ctx.setShadowColor(desc.textShadowColor);
675
- }
676
- if (desc.textShadowOffsetX !== void 0 && is.number(desc.textShadowOffsetX)) {
677
- ctx.setShadowOffsetX(desc.textShadowOffsetX);
678
- }
679
- if (desc.textShadowOffsetY !== void 0 && is.number(desc.textShadowOffsetY)) {
680
- ctx.setShadowOffsetY(desc.textShadowOffsetY);
681
- }
682
- if (desc.textShadowBlur !== void 0 && is.number(desc.textShadowBlur)) {
683
- ctx.setShadowBlur(desc.textShadowBlur);
684
- }
685
- lines.forEach((line, i) => {
686
- let _x = elem.x;
687
- if (desc.textAlign === "center") {
688
- _x = elem.x + (elem.w - line.width) / 2;
689
- } else if (desc.textAlign === "right") {
690
- _x = elem.x + (elem.w - line.width);
691
- }
692
- ctx.fillText(line.text, _x, _y + fontHeight * i);
693
- });
694
- clearContext(ctx);
695
- }
696
- if (isColorStr(desc.strokeColor) && desc.strokeWidth !== void 0 && desc.strokeWidth > 0) {
697
- const _y = elem.y + startY;
698
- lines.forEach((line, i) => {
699
- let _x = elem.x;
700
- if (desc.textAlign === "center") {
701
- _x = elem.x + (elem.w - line.width) / 2;
702
- } else if (desc.textAlign === "right") {
703
- _x = elem.x + (elem.w - line.width);
704
- }
705
- if (desc.strokeColor !== void 0) {
706
- ctx.setStrokeStyle(desc.strokeColor);
707
- }
708
- if (desc.strokeWidth !== void 0 && desc.strokeWidth > 0) {
709
- ctx.setLineWidth(desc.strokeWidth);
710
- }
711
- ctx.strokeText(line.text, _x, _y + fontHeight * i);
712
- });
713
- }
714
780
  });
715
781
  }
716
- function drawCircle(ctx, elem) {
717
- clearContext(ctx);
718
- rotateElement(ctx, elem, (ctx2) => {
719
- const { x: x2, y: y2, w: w2, h: h2, desc } = elem;
720
- const {
721
- bgColor = "#000000",
722
- borderColor = "#000000",
723
- borderWidth: borderWidth2 = 0
724
- } = desc;
725
- const a = w2 / 2;
726
- const b = h2 / 2;
727
- const centerX = x2 + a;
728
- const centerY = y2 + b;
729
- if (borderWidth2 && borderWidth2 > 0) {
730
- const ba = borderWidth2 / 2 + a;
731
- const bb = borderWidth2 / 2 + b;
732
- ctx2.beginPath();
733
- ctx2.setStrokeStyle(borderColor);
734
- ctx2.setLineWidth(borderWidth2);
735
- ctx2.ellipse(centerX, centerY, ba, bb, 0, 0, 2 * Math.PI);
736
- ctx2.closePath();
737
- ctx2.stroke();
738
- }
739
- ctx2.beginPath();
740
- ctx2.setFillStyle(bgColor);
741
- ctx2.ellipse(centerX, centerY, a, b, 0, 0, 2 * Math.PI);
742
- ctx2.closePath();
743
- ctx2.fill();
782
+ function drawPath(ctx, elem, opts) {
783
+ const { detail } = elem;
784
+ const { originX, originY, originW, originH } = detail;
785
+ const { calculator, viewScaleInfo, viewSizeInfo } = opts;
786
+ const { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = calculator.elementSize(elem, viewScaleInfo, viewSizeInfo);
787
+ const scaleW = w2 / originW;
788
+ const scaleH = h2 / originH;
789
+ const viewOriginX = originX * scaleW;
790
+ const viewOriginY = originY * scaleH;
791
+ const internalX = x2 - viewOriginX;
792
+ const internalY = y2 - viewOriginY;
793
+ const scaleNum = viewScaleInfo.scale * viewSizeInfo.devicePixelRatio;
794
+ const viewElem = { ...elem, ...{ x: x2, y: y2, w: w2, h: h2, angle: angle2 } };
795
+ rotateElement(ctx, { x: x2, y: y2, w: w2, h: h2, angle: angle2 }, () => {
796
+ drawBox(ctx, viewElem, {
797
+ originElem: elem,
798
+ calcElemSize: { x: x2, y: y2, w: w2, h: h2, angle: angle2 },
799
+ viewScaleInfo,
800
+ viewSizeInfo,
801
+ renderContent: () => {
802
+ drawBoxShadow(ctx, viewElem, {
803
+ viewScaleInfo,
804
+ viewSizeInfo,
805
+ renderContent: () => {
806
+ ctx.save();
807
+ ctx.translate(internalX, internalY);
808
+ ctx.scale(scaleNum * scaleW / viewScaleInfo.scale, scaleNum * scaleH / viewScaleInfo.scale);
809
+ const pathStr = generateSVGPath(detail.commands || []);
810
+ const path2d = new Path2D(pathStr);
811
+ if (detail.fill) {
812
+ ctx.fillStyle = detail.fill;
813
+ ctx.fill(path2d);
814
+ }
815
+ if (detail.stroke && detail.strokeWidth !== 0) {
816
+ ctx.strokeStyle = detail.stroke;
817
+ ctx.lineWidth = (detail.strokeWidth || 1) / viewSizeInfo.devicePixelRatio;
818
+ ctx.lineCap = detail.strokeLineCap || "square";
819
+ ctx.stroke(path2d);
820
+ }
821
+ ctx.translate(-internalX, -internalY);
822
+ ctx.restore();
823
+ }
824
+ });
825
+ }
826
+ });
744
827
  });
745
828
  }
746
- function drawContext(ctx, data, loader) {
829
+ function drawElement(ctx, elem, opts) {
747
830
  var _a;
748
- clearContext(ctx);
749
- const size = ctx.getSize();
750
- ctx.clearRect(0, 0, size.contextWidth, size.contextHeight);
751
- if (typeof data.bgColor === "string" && isColorStr(data.bgColor)) {
752
- drawBgColor(ctx, data.bgColor);
753
- }
754
- if (!(data.elements.length > 0)) {
831
+ if (((_a = elem == null ? void 0 : elem.operations) == null ? void 0 : _a.invisible) === true) {
755
832
  return;
756
833
  }
757
- for (let i = 0; i < data.elements.length; i++) {
758
- const elem = data.elements[i];
759
- if (((_a = elem == null ? void 0 : elem.operation) == null ? void 0 : _a.invisible) === true) {
760
- continue;
761
- }
834
+ try {
762
835
  switch (elem.type) {
763
836
  case "rect": {
764
- drawRect(ctx, elem);
837
+ drawRect(ctx, elem, opts);
838
+ break;
839
+ }
840
+ case "circle": {
841
+ drawCircle(ctx, elem, opts);
765
842
  break;
766
843
  }
767
844
  case "text": {
768
- drawText(ctx, elem);
845
+ drawText(ctx, elem, opts);
769
846
  break;
770
847
  }
771
848
  case "image": {
772
- drawImage(ctx, elem, loader);
849
+ drawImage(ctx, elem, opts);
773
850
  break;
774
851
  }
775
852
  case "svg": {
776
- drawSVG(ctx, elem, loader);
853
+ drawSVG(ctx, elem, opts);
777
854
  break;
778
855
  }
779
856
  case "html": {
780
- drawHTML(ctx, elem, loader);
857
+ drawHTML(ctx, elem, opts);
781
858
  break;
782
859
  }
783
- case "circle": {
784
- drawCircle(ctx, elem);
860
+ case "path": {
861
+ drawPath(ctx, elem, opts);
862
+ break;
863
+ }
864
+ case "group": {
865
+ drawGroup(ctx, elem, opts);
866
+ break;
867
+ }
868
+ default: {
785
869
  break;
786
870
  }
787
871
  }
872
+ } catch (err) {
873
+ console.error(err);
788
874
  }
789
875
  }
790
- class LoaderEvent {
791
- constructor() {
792
- this._listeners = /* @__PURE__ */ new Map();
793
- }
794
- on(eventKey, callback) {
795
- if (this._listeners.has(eventKey)) {
796
- const callbacks = this._listeners.get(eventKey);
797
- callbacks == null ? void 0 : callbacks.push(callback);
798
- this._listeners.set(eventKey, callbacks || []);
799
- } else {
800
- this._listeners.set(eventKey, [callback]);
801
- }
802
- }
803
- off(eventKey, callback) {
804
- if (this._listeners.has(eventKey)) {
805
- const callbacks = this._listeners.get(eventKey);
806
- if (Array.isArray(callbacks)) {
807
- for (let i = 0; i < (callbacks == null ? void 0 : callbacks.length); i++) {
808
- if (callbacks[i] === callback) {
809
- callbacks.splice(i, 1);
810
- break;
876
+ function drawGroup(ctx, elem, opts) {
877
+ const { calculator, viewScaleInfo, viewSizeInfo } = opts;
878
+ const { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = calculator.elementSize({ x: elem.x, y: elem.y, w: elem.w, h: elem.h, angle: elem.angle }, viewScaleInfo, viewSizeInfo);
879
+ const viewElem = { ...elem, ...{ x: x2, y: y2, w: w2, h: h2, angle: angle2 } };
880
+ rotateElement(ctx, { x: x2, y: y2, w: w2, h: h2, angle: angle2 }, () => {
881
+ drawBox(ctx, viewElem, {
882
+ originElem: elem,
883
+ calcElemSize: { x: x2, y: y2, w: w2, h: h2, angle: angle2 },
884
+ viewScaleInfo,
885
+ viewSizeInfo,
886
+ renderContent: () => {
887
+ if (Array.isArray(elem.detail.children)) {
888
+ const { parentElementSize: parentSize } = opts;
889
+ const newParentSize = {
890
+ x: parentSize.x + elem.x,
891
+ y: parentSize.y + elem.y,
892
+ w: elem.w || parentSize.w,
893
+ h: elem.h || parentSize.h,
894
+ angle: elem.angle
895
+ };
896
+ const { calculator: calculator2 } = opts;
897
+ if (elem.detail.overflow === "hidden") {
898
+ ctx.save();
899
+ ctx.beginPath();
900
+ ctx.moveTo(x2, y2);
901
+ ctx.lineTo(x2 + w2, y2);
902
+ ctx.lineTo(x2 + w2, y2 + h2);
903
+ ctx.lineTo(x2, y2 + h2);
904
+ ctx.closePath();
905
+ ctx.clip();
811
906
  }
907
+ for (let i = 0; i < elem.detail.children.length; i++) {
908
+ let child = elem.detail.children[i];
909
+ child = {
910
+ ...child,
911
+ ...{
912
+ x: newParentSize.x + child.x,
913
+ y: newParentSize.y + child.y
914
+ }
915
+ };
916
+ if (!calculator2.isElementInView(child, opts.viewScaleInfo, opts.viewSizeInfo)) {
917
+ continue;
918
+ }
919
+ try {
920
+ drawElement(ctx, child, { ...opts });
921
+ } catch (err) {
922
+ console.error(err);
923
+ }
924
+ }
925
+ ctx.restore();
812
926
  }
813
927
  }
814
- this._listeners.set(eventKey, callbacks || []);
815
- }
816
- }
817
- trigger(eventKey, arg) {
818
- const callbacks = this._listeners.get(eventKey);
819
- if (Array.isArray(callbacks)) {
820
- callbacks.forEach((cb) => {
821
- cb(arg);
822
- });
823
- return true;
824
- } else {
825
- return false;
928
+ });
929
+ });
930
+ }
931
+ function drawElementList(ctx, data, opts) {
932
+ const { elements = [] } = data;
933
+ for (let i = 0; i < elements.length; i++) {
934
+ const elem = elements[i];
935
+ if (!opts.calculator.isElementInView(elem, opts.viewScaleInfo, opts.viewSizeInfo)) {
936
+ continue;
826
937
  }
827
- }
828
- has(name) {
829
- if (this._listeners.has(name)) {
830
- const list = this._listeners.get(name);
831
- if (Array.isArray(list) && list.length > 0) {
832
- return true;
833
- }
938
+ try {
939
+ drawElement(ctx, elem, opts);
940
+ } catch (err) {
941
+ console.error(err);
834
942
  }
835
- return false;
836
943
  }
837
944
  }
838
- function filterScript(html2) {
839
- return html2.replace(/<script[\s\S]*?<\/script>/ig, "");
840
- }
841
- class Loader {
842
- constructor(opts) {
843
- this._currentLoadData = {};
844
- this._currentUUIDQueue = [];
845
- this._storageLoadData = {};
846
- this._status = "free";
847
- this._waitingLoadQueue = [];
848
- this._opts = opts;
849
- this._event = new LoaderEvent();
850
- this._waitingLoadQueue = [];
851
- }
852
- load(data, changeResourceUUIDs) {
853
- const [uuidQueue, loadData] = this._resetLoadData(
854
- data,
855
- changeResourceUUIDs
856
- );
857
- if (this._status === "free" || this._status === "complete") {
858
- this._currentUUIDQueue = uuidQueue;
859
- this._currentLoadData = loadData;
860
- this._loadTask();
861
- } else if (this._status === "loading" && uuidQueue.length > 0) {
862
- this._waitingLoadQueue.push({
863
- uuidQueue,
864
- loadData
945
+ const supportElementTypes = ["image", "svg", "html"];
946
+ class Loader extends EventEmitter {
947
+ constructor() {
948
+ super();
949
+ this._loadFuncMap = {};
950
+ this._currentLoadItemMap = {};
951
+ this._storageLoadItemMap = {};
952
+ this._registerLoadFunc("image", async (elem, assets) => {
953
+ var _a;
954
+ const src = ((_a = assets[elem.detail.src]) == null ? void 0 : _a.value) || elem.detail.src;
955
+ const content = await loadImage(src);
956
+ return {
957
+ uuid: elem.uuid,
958
+ lastModified: Date.now(),
959
+ content
960
+ };
961
+ });
962
+ this._registerLoadFunc("html", async (elem, assets) => {
963
+ var _a;
964
+ const html2 = ((_a = assets[elem.detail.html]) == null ? void 0 : _a.value) || elem.detail.html;
965
+ const content = await loadHTML(html2, {
966
+ width: elem.detail.width || elem.w,
967
+ height: elem.detail.height || elem.h
865
968
  });
866
- }
867
- }
868
- on(name, callback) {
869
- this._event.on(name, callback);
870
- }
871
- off(name, callback) {
872
- this._event.off(name, callback);
873
- }
874
- isComplete() {
875
- return this._status === "complete";
969
+ return {
970
+ uuid: elem.uuid,
971
+ lastModified: Date.now(),
972
+ content
973
+ };
974
+ });
975
+ this._registerLoadFunc("svg", async (elem, assets) => {
976
+ var _a;
977
+ const svg2 = ((_a = assets[elem.detail.svg]) == null ? void 0 : _a.value) || elem.detail.svg;
978
+ const content = await loadSVG(svg2);
979
+ return {
980
+ uuid: elem.uuid,
981
+ lastModified: Date.now(),
982
+ content
983
+ };
984
+ });
876
985
  }
877
- getContent(uuid) {
878
- var _a;
879
- if (((_a = this._storageLoadData[uuid]) == null ? void 0 : _a.status) === "loaded") {
880
- return this._storageLoadData[uuid].content;
986
+ _registerLoadFunc(type, func) {
987
+ this._loadFuncMap[type] = func;
988
+ }
989
+ _getLoadElementSource(element) {
990
+ var _a, _b, _c;
991
+ let source = null;
992
+ if (element.type === "image") {
993
+ source = ((_a = element == null ? void 0 : element.detail) == null ? void 0 : _a.src) || null;
994
+ } else if (element.type === "svg") {
995
+ source = ((_b = element == null ? void 0 : element.detail) == null ? void 0 : _b.svg) || null;
996
+ } else if (element.type === "html") {
997
+ source = ((_c = element == null ? void 0 : element.detail) == null ? void 0 : _c.html) || null;
881
998
  }
882
- return null;
999
+ return source;
883
1000
  }
884
- // getPattern(
885
- // elem: DataElement<keyof DataElemDesc>,
886
- // opts?: {
887
- // forceUpdate: boolean
888
- // }
889
- // ): null | CanvasPattern {
890
- // if (this._patternMap[elem.uuid] ) {
891
- // if (!(opts && opts.forceUpdate === true)) {
892
- // return this._patternMap[elem.uuid];
893
- // }
894
- // }
895
- // const item = this._currentLoadData[elem.uuid];
896
- // if (item?.status === 'loaded') {
897
- // const board = this._opts.board;
898
- // const tempCanvas = board.createCanvas();
899
- // const tempCtx = board.createContext(tempCanvas);
900
- // const image = this.getContent(elem.uuid);
901
- // tempCtx.drawImage(image, elem.x, elem.y, elem.w, elem.h);
902
- // const canvas = board.createCanvas();
903
- // const ctx = board.createContext(canvas);
904
- // const pattern = ctx.createPattern(tempCanvas, 'no-repeat');
905
- // if (pattern) this._patternMap[elem.uuid] = pattern;
906
- // return pattern;
907
- // }
908
- // return null;
909
- // }
910
- _resetLoadData(data, changeResourceUUIDs) {
911
- const loadData = {};
912
- const uuidQueue = [];
913
- const storageLoadData = this._storageLoadData;
914
- for (let i = data.elements.length - 1; i >= 0; i--) {
915
- const elem = data.elements[i];
916
- if (["image", "svg", "html"].includes(elem.type)) {
917
- if (!storageLoadData[elem.uuid]) {
918
- loadData[elem.uuid] = this._createEmptyLoadItem(elem);
919
- uuidQueue.push(elem.uuid);
920
- } else {
921
- if (changeResourceUUIDs.includes(elem.uuid)) {
922
- loadData[elem.uuid] = this._createEmptyLoadItem(elem);
923
- uuidQueue.push(elem.uuid);
924
- }
925
- }
926
- }
927
- }
928
- return [uuidQueue, loadData];
929
- }
930
- _createEmptyLoadItem(elem) {
931
- let source = "";
932
- const type = elem.type;
933
- let elemW = elem.w;
934
- let elemH = elem.h;
935
- if (elem.type === "image") {
936
- const _elem = elem;
937
- source = _elem.desc.src || "";
938
- } else if (elem.type === "svg") {
939
- const _elem = elem;
940
- source = _elem.desc.svg || "";
941
- } else if (elem.type === "html") {
942
- const _elem = elem;
943
- source = filterScript(_elem.desc.html || "");
944
- elemW = _elem.desc.width || elem.w;
945
- elemH = _elem.desc.height || elem.h;
946
- }
1001
+ _createLoadItem(element) {
947
1002
  return {
948
- uuid: elem.uuid,
949
- type,
1003
+ element,
950
1004
  status: "null",
951
1005
  content: null,
952
- source,
953
- elemW,
954
- elemH,
955
- element: deepClone(elem)
1006
+ error: null,
1007
+ startTime: -1,
1008
+ endTime: -1,
1009
+ source: this._getLoadElementSource(element)
956
1010
  };
957
1011
  }
958
- _loadTask() {
959
- if (this._status === "loading") {
960
- return;
961
- }
962
- this._status = "loading";
963
- if (this._currentUUIDQueue.length === 0) {
964
- if (this._waitingLoadQueue.length === 0) {
965
- this._status = "complete";
966
- this._event.trigger("complete", void 0);
967
- return;
968
- } else {
969
- const waitingItem = this._waitingLoadQueue.shift();
970
- if (waitingItem) {
971
- const { uuidQueue, loadData } = waitingItem;
972
- this._currentLoadData = loadData;
973
- this._currentUUIDQueue = uuidQueue;
974
- }
1012
+ _emitLoad(item) {
1013
+ const uuid = item.element.uuid;
1014
+ const storageItem = this._storageLoadItemMap[uuid];
1015
+ if (storageItem) {
1016
+ if (storageItem.startTime < item.startTime) {
1017
+ this._storageLoadItemMap[uuid] = item;
1018
+ this.trigger("load", { ...item, countTime: item.endTime - item.startTime });
975
1019
  }
976
- }
977
- const { maxParallelNum } = this._opts;
978
- const uuids = this._currentUUIDQueue.splice(0, maxParallelNum);
979
- uuids.forEach((url, i) => {
980
- });
981
- const loadUUIDList = [];
982
- const _loadAction = () => {
983
- if (loadUUIDList.length >= maxParallelNum) {
984
- return false;
985
- }
986
- if (uuids.length === 0) {
987
- return true;
988
- }
989
- for (let i = loadUUIDList.length; i < maxParallelNum; i++) {
990
- const uuid = uuids.shift();
991
- if (uuid === void 0) {
992
- break;
993
- }
994
- loadUUIDList.push(uuid);
995
- this._loadElementSource(this._currentLoadData[uuid]).then((image) => {
996
- var _a, _b;
997
- loadUUIDList.splice(loadUUIDList.indexOf(uuid), 1);
998
- const status = _loadAction();
999
- this._storageLoadData[uuid] = {
1000
- uuid,
1001
- type: this._currentLoadData[uuid].type,
1002
- status: "loaded",
1003
- content: image,
1004
- source: this._currentLoadData[uuid].source,
1005
- elemW: this._currentLoadData[uuid].elemW,
1006
- elemH: this._currentLoadData[uuid].elemH,
1007
- element: this._currentLoadData[uuid].element
1008
- };
1009
- if (loadUUIDList.length === 0 && uuids.length === 0 && status === true) {
1010
- this._status = "free";
1011
- this._loadTask();
1012
- }
1013
- this._event.trigger("load", {
1014
- uuid: (_a = this._storageLoadData[uuid]) == null ? void 0 : _a.uuid,
1015
- type: this._storageLoadData[uuid].type,
1016
- status: this._storageLoadData[uuid].status,
1017
- content: this._storageLoadData[uuid].content,
1018
- source: this._storageLoadData[uuid].source,
1019
- elemW: this._storageLoadData[uuid].elemW,
1020
- elemH: this._storageLoadData[uuid].elemH,
1021
- element: (_b = this._storageLoadData[uuid]) == null ? void 0 : _b.element
1022
- });
1023
- }).catch((err) => {
1024
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
1025
- console.warn(err);
1026
- loadUUIDList.splice(loadUUIDList.indexOf(uuid), 1);
1027
- const status = _loadAction();
1028
- if (this._currentLoadData[uuid]) {
1029
- this._storageLoadData[uuid] = {
1030
- uuid,
1031
- type: (_a = this._currentLoadData[uuid]) == null ? void 0 : _a.type,
1032
- status: "fail",
1033
- content: null,
1034
- error: err,
1035
- source: (_b = this._currentLoadData[uuid]) == null ? void 0 : _b.source,
1036
- elemW: (_c = this._currentLoadData[uuid]) == null ? void 0 : _c.elemW,
1037
- elemH: (_d = this._currentLoadData[uuid]) == null ? void 0 : _d.elemH,
1038
- element: (_e = this._currentLoadData[uuid]) == null ? void 0 : _e.element
1039
- };
1040
- }
1041
- if (loadUUIDList.length === 0 && uuids.length === 0 && status === true) {
1042
- this._status = "free";
1043
- this._loadTask();
1044
- }
1045
- if (this._currentLoadData[uuid]) {
1046
- this._event.trigger("error", {
1047
- uuid,
1048
- type: (_f = this._storageLoadData[uuid]) == null ? void 0 : _f.type,
1049
- status: (_g = this._storageLoadData[uuid]) == null ? void 0 : _g.status,
1050
- content: (_h = this._storageLoadData[uuid]) == null ? void 0 : _h.content,
1051
- source: (_i = this._storageLoadData[uuid]) == null ? void 0 : _i.source,
1052
- elemW: (_j = this._storageLoadData[uuid]) == null ? void 0 : _j.elemW,
1053
- elemH: (_k = this._storageLoadData[uuid]) == null ? void 0 : _k.elemH,
1054
- element: (_l = this._storageLoadData[uuid]) == null ? void 0 : _l.element
1055
- });
1056
- }
1057
- });
1058
- }
1059
- return false;
1060
- };
1061
- _loadAction();
1062
- }
1063
- async _loadElementSource(params) {
1064
- if (params && params.type === "image") {
1065
- const image = await loadImage(params.source);
1066
- return image;
1067
- } else if (params && params.type === "svg") {
1068
- const image = await loadSVG(params.source);
1069
- return image;
1070
- } else if (params && params.type === "html") {
1071
- const image = await loadHTML(params.source, {
1072
- width: params.elemW,
1073
- height: params.elemH
1074
- });
1075
- return image;
1076
- }
1077
- throw Error("Element's source is not support!");
1078
- }
1079
- }
1080
- class RendererEvent {
1081
- constructor() {
1082
- this._listeners = /* @__PURE__ */ new Map();
1083
- }
1084
- on(eventKey, callback) {
1085
- if (this._listeners.has(eventKey)) {
1086
- const callbacks = this._listeners.get(eventKey);
1087
- callbacks == null ? void 0 : callbacks.push(callback);
1088
- this._listeners.set(eventKey, callbacks || []);
1089
1020
  } else {
1090
- this._listeners.set(eventKey, [callback]);
1021
+ this._storageLoadItemMap[uuid] = item;
1022
+ this.trigger("load", { ...item, countTime: item.endTime - item.startTime });
1091
1023
  }
1092
1024
  }
1093
- off(eventKey, callback) {
1094
- if (this._listeners.has(eventKey)) {
1095
- const callbacks = this._listeners.get(eventKey);
1096
- if (Array.isArray(callbacks)) {
1097
- for (let i = 0; i < (callbacks == null ? void 0 : callbacks.length); i++) {
1098
- if (callbacks[i] === callback) {
1099
- callbacks.splice(i, 1);
1100
- break;
1101
- }
1102
- }
1025
+ _emitError(item) {
1026
+ const uuid = item.element.uuid;
1027
+ const storageItem = this._storageLoadItemMap[uuid];
1028
+ if (storageItem) {
1029
+ if (storageItem.startTime < item.startTime) {
1030
+ this._storageLoadItemMap[uuid] = item;
1031
+ this.trigger("error", { ...item, countTime: item.endTime - item.startTime });
1103
1032
  }
1104
- this._listeners.set(eventKey, callbacks || []);
1033
+ } else {
1034
+ this._storageLoadItemMap[uuid] = item;
1035
+ this.trigger("error", { ...item, countTime: item.endTime - item.startTime });
1105
1036
  }
1106
1037
  }
1107
- trigger(eventKey, arg) {
1108
- const callbacks = this._listeners.get(eventKey);
1109
- if (Array.isArray(callbacks)) {
1110
- callbacks.forEach((cb) => {
1111
- cb(arg);
1038
+ _loadResource(element, assets) {
1039
+ const item = this._createLoadItem(element);
1040
+ this._currentLoadItemMap[element.uuid] = item;
1041
+ const loadFunc = this._loadFuncMap[element.type];
1042
+ if (typeof loadFunc === "function") {
1043
+ item.startTime = Date.now();
1044
+ loadFunc(element, assets).then((result) => {
1045
+ item.content = result.content;
1046
+ item.endTime = Date.now();
1047
+ item.status = "load";
1048
+ this._emitLoad(item);
1049
+ }).catch((err) => {
1050
+ console.warn(`Load element source "${item.source}" fail`, err, element);
1051
+ item.endTime = Date.now();
1052
+ item.status = "error";
1053
+ item.error = err;
1054
+ this._emitError(item);
1112
1055
  });
1113
- return true;
1114
- } else {
1115
- return false;
1116
1056
  }
1117
1057
  }
1118
- has(name) {
1119
- if (this._listeners.has(name)) {
1120
- const list = this._listeners.get(name);
1121
- if (Array.isArray(list) && list.length > 0) {
1122
- return true;
1123
- }
1058
+ _isExistingErrorStorage(element) {
1059
+ var _a;
1060
+ const existItem = (_a = this._currentLoadItemMap) == null ? void 0 : _a[element == null ? void 0 : element.uuid];
1061
+ if (existItem && existItem.status === "error" && existItem.source && existItem.source === this._getLoadElementSource(element)) {
1062
+ return true;
1124
1063
  }
1125
1064
  return false;
1126
1065
  }
1066
+ load(element, assets) {
1067
+ if (this._isExistingErrorStorage(element)) {
1068
+ return;
1069
+ }
1070
+ if (supportElementTypes.includes(element.type)) {
1071
+ const elem = deepClone(element);
1072
+ this._loadResource(elem, assets);
1073
+ }
1074
+ }
1075
+ getContent(uuid) {
1076
+ var _a, _b;
1077
+ return ((_b = (_a = this._storageLoadItemMap) == null ? void 0 : _a[uuid]) == null ? void 0 : _b.content) || null;
1078
+ }
1127
1079
  }
1128
- const { requestAnimationFrame } = window;
1129
- class Renderer extends RendererEvent {
1080
+ class Renderer extends EventEmitter {
1081
+ // private _draftContextTop: CanvasRenderingContext2D;
1082
+ // private _draftContextMiddle: CanvasRenderingContext2D;
1083
+ // private _draftContextBottom: CanvasRenderingContext2D;
1130
1084
  constructor(opts) {
1131
1085
  super();
1132
- this._queue = [];
1133
- this._ctx = null;
1134
- this._status = "null";
1086
+ this._loader = new Loader();
1135
1087
  this._opts = opts;
1136
- this._loader = new Loader({
1137
- maxParallelNum: 6
1138
- });
1139
- this._loader.on("load", (res) => {
1140
- this._drawFrame();
1141
- this.trigger("load", { element: res.element });
1088
+ this._init();
1089
+ }
1090
+ _init() {
1091
+ const { _loader: loader } = this;
1092
+ loader.on("load", (e) => {
1093
+ this.trigger("load", e);
1142
1094
  });
1143
- this._loader.on("error", (res) => {
1144
- this.trigger("error", { element: res.element, error: res.error });
1095
+ loader.on("error", () => {
1145
1096
  });
1146
- this._loader.on("complete", () => {
1147
- this.trigger("loadComplete", { t: Date.now() });
1097
+ }
1098
+ updateOptions(opts) {
1099
+ this._opts = opts;
1100
+ }
1101
+ drawData(data, opts) {
1102
+ const { _loader: loader } = this;
1103
+ const { calculator } = this._opts;
1104
+ const { viewContext } = this._opts.viewContent;
1105
+ viewContext.clearRect(0, 0, viewContext.canvas.width, viewContext.canvas.height);
1106
+ const parentElementSize = {
1107
+ x: 0,
1108
+ y: 0,
1109
+ w: opts.viewSizeInfo.width,
1110
+ h: opts.viewSizeInfo.height
1111
+ };
1112
+ drawElementList(viewContext, data, {
1113
+ loader,
1114
+ calculator,
1115
+ parentElementSize,
1116
+ elementAssets: data.assets,
1117
+ ...opts
1148
1118
  });
1149
1119
  }
1150
- render(target, originData, opts) {
1151
- const { changeResourceUUIDs = [] } = opts || {};
1152
- this._status = "free";
1153
- const data = deepClone(originData);
1154
- if (Array.isArray(data.elements)) {
1155
- data.elements.forEach((elem) => {
1156
- if (!(typeof elem.uuid === "string" && elem.uuid)) {
1157
- elem.uuid = createUUID();
1158
- }
1159
- });
1160
- }
1161
- if (!this._ctx) {
1162
- if (this._opts && Object.prototype.toString.call(target) === "[object HTMLCanvasElement]") {
1163
- const { width, height, contextWidth, contextHeight, devicePixelRatio } = this._opts;
1164
- const canvas = target;
1165
- canvas.width = width * devicePixelRatio;
1166
- canvas.height = height * devicePixelRatio;
1167
- const ctx2d = canvas.getContext("2d");
1168
- this._ctx = new Context(ctx2d, {
1120
+ scale(num) {
1121
+ const { sharer } = this._opts;
1122
+ const { data, offsetTop, offsetBottom, offsetLeft, offsetRight, width, height, contextHeight, contextWidth, devicePixelRatio } = sharer.getActiveStoreSnapshot();
1123
+ if (data) {
1124
+ this.drawData(data, {
1125
+ viewScaleInfo: {
1126
+ scale: num,
1127
+ offsetTop,
1128
+ offsetBottom,
1129
+ offsetLeft,
1130
+ offsetRight
1131
+ },
1132
+ viewSizeInfo: {
1169
1133
  width,
1170
1134
  height,
1171
- contextWidth: contextWidth || width,
1172
- contextHeight: contextHeight || height,
1135
+ contextHeight,
1136
+ contextWidth,
1173
1137
  devicePixelRatio
1174
- });
1175
- } else if (target) {
1176
- this._ctx = target;
1177
- }
1178
- }
1179
- if ([
1180
- "freeze"
1181
- /* FREEZE */
1182
- ].includes(this._status)) {
1183
- return;
1184
- }
1185
- const _data = deepClone({ data });
1186
- this._queue.push(_data);
1187
- this._drawFrame();
1188
- this._loader.load(data, changeResourceUUIDs || []);
1189
- }
1190
- getContext() {
1191
- return this._ctx;
1192
- }
1193
- thaw() {
1194
- this._status = "free";
1195
- }
1196
- _freeze() {
1197
- this._status = "freeze";
1198
- }
1199
- _drawFrame() {
1200
- if (this._status === "freeze") {
1201
- return;
1202
- }
1203
- requestAnimationFrame(() => {
1204
- if (this._status === "freeze") {
1205
- return;
1206
- }
1207
- const ctx = this._ctx;
1208
- let item = this._queue[0];
1209
- let isLastFrame = false;
1210
- if (this._queue.length > 1) {
1211
- item = this._queue.shift();
1212
- } else {
1213
- isLastFrame = true;
1214
- }
1215
- if (this._loader.isComplete() !== true) {
1216
- this._drawFrame();
1217
- if (item && ctx) {
1218
- drawContext(ctx, item.data, this._loader);
1219
- }
1220
- } else if (item && ctx) {
1221
- drawContext(ctx, item.data, this._loader);
1222
- this._retainQueueOneItem();
1223
- if (!isLastFrame) {
1224
- this._drawFrame();
1225
- } else {
1226
- this._status = "free";
1227
1138
  }
1228
- } else {
1229
- this._status = "free";
1230
- }
1231
- this.trigger("drawFrame", { t: Date.now() });
1232
- if (this._loader.isComplete() === true && this._queue.length === 1 && this._status === "free") {
1233
- if (ctx && this._queue[0] && this._queue[0].data) {
1234
- drawContext(ctx, this._queue[0].data, this._loader);
1235
- }
1236
- this.trigger("drawFrameComplete", { t: Date.now() });
1237
- this._freeze();
1238
- }
1239
- });
1240
- }
1241
- _retainQueueOneItem() {
1242
- if (this._queue.length <= 1) {
1243
- return;
1139
+ });
1244
1140
  }
1245
- const lastOne = deepClone(this._queue[this._queue.length - 1]);
1246
- this._queue = [lastOne];
1247
1141
  }
1142
+ // scroll(opts: { offsetTop?: number; offsetLeft?: number }) {
1143
+ // const { sharer } = this._opts;
1144
+ // const { data, scale, offsetTop, offsetBottom, offsetLeft, offsetRight } = sharer.getActiveStoreSnapshot();
1145
+ // // TODO calc offset data
1146
+ // if (data) {
1147
+ // this.drawData(data, {
1148
+ // scale,
1149
+ // offsetTop,
1150
+ // offsetBottom,
1151
+ // offsetLeft,
1152
+ // offsetRight
1153
+ // });
1154
+ // }
1155
+ // // sharer.setActiveStorage('scale', num);
1156
+ // }
1248
1157
  }
1249
- return Renderer;
1250
- }();
1158
+ exports.Renderer = Renderer;
1159
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
1160
+ return exports;
1161
+ }({});