@idraw/renderer 0.4.0-alpha.3 → 0.4.0-alpha.5

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.
@@ -3,34 +3,57 @@ var iDrawRenderer = function(exports) {
3
3
  function isColorStr(color2) {
4
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));
5
5
  }
6
- function deepClone(target) {
7
- function _clone(t) {
8
- const type = is$1(t);
9
- if (["Null", "Number", "String", "Boolean", "Undefined"].indexOf(type) >= 0) {
10
- return t;
11
- } else if (type === "Array") {
12
- const arr = [];
13
- t.forEach((item) => {
14
- arr.push(_clone(item));
15
- });
16
- return arr;
17
- } else if (type === "Object") {
18
- const obj = {};
19
- const keys = Object.keys(t);
20
- keys.forEach((key) => {
21
- obj[key] = _clone(t[key]);
22
- });
23
- const symbolKeys = Object.getOwnPropertySymbols(t);
24
- symbolKeys.forEach((key) => {
25
- obj[key] = _clone(t[key]);
26
- });
27
- return obj;
28
- }
6
+ function mergeHexColorAlpha(hex, alpha) {
7
+ if (alpha === 1) {
8
+ return hex;
9
+ }
10
+ let hexAlpha = 1;
11
+ const regHex1 = /^\#[0-9a-f]{6,6}$/i;
12
+ const regHex2 = /^\#[0-9a-f]{8,8}$/i;
13
+ let result = hex;
14
+ if (regHex1.test(hex)) {
15
+ hexAlpha = parseInt(hex.substring(5, 7).replace(/^\#/, "0x"));
16
+ } else if (regHex2.test(hex)) {
17
+ hexAlpha = parseInt(hex.substring(7, 9).replace(/^\#/, "0x"));
18
+ result = hex.substring(0, 7);
19
+ }
20
+ hexAlpha = hexAlpha * alpha;
21
+ if (regHex1.test(result) && hexAlpha > 0 && hexAlpha < 1) {
22
+ const aHexNum = Math.max(0, Math.min(255, Math.ceil(hexAlpha * 256)));
23
+ result = `${result.toUpperCase()}${aHexNum.toString(16).toUpperCase()}`;
24
+ }
25
+ return result;
26
+ }
27
+ function createUUID() {
28
+ function _createStr() {
29
+ return ((1 + Math.random()) * 65536 | 0).toString(16).substring(1);
29
30
  }
30
- return _clone(target);
31
+ return `${_createStr()}${_createStr()}-${_createStr()}-${_createStr()}-${_createStr()}-${_createStr()}${_createStr()}${_createStr()}`;
31
32
  }
32
- function is$1(target) {
33
- return Object.prototype.toString.call(target).replace(/[\]|\[]{1,1}/gi, "").split(" ")[1];
33
+ function limitHexStr(str) {
34
+ let count = 0;
35
+ for (let i = 0; i < str.length; i++) {
36
+ count += str.charCodeAt(i) * str.charCodeAt(i) * i * i;
37
+ }
38
+ return count.toString(16).substring(0, 4);
39
+ }
40
+ function createAssetId(assetStr) {
41
+ const len = assetStr.length;
42
+ const mid = Math.floor(len / 2);
43
+ const start4 = assetStr.substring(0, 4).padEnd(4, "0");
44
+ const end4 = assetStr.substring(0, 4).padEnd(4, "0");
45
+ const str1 = limitHexStr(len.toString(16).padEnd(4, start4));
46
+ const str2 = limitHexStr(assetStr.substring(mid - 4, mid).padEnd(4, start4)).padEnd(4, "f");
47
+ const str3 = limitHexStr(assetStr.substring(mid - 8, mid - 4).padEnd(4, start4)).padEnd(4, "f");
48
+ const str4 = limitHexStr(assetStr.substring(mid - 12, mid - 8).padEnd(4, start4)).padEnd(4, "f");
49
+ const str5 = limitHexStr(assetStr.substring(mid - 16, mid - 12).padEnd(4, end4)).padEnd(4, "f");
50
+ const str6 = limitHexStr(assetStr.substring(mid, mid + 4).padEnd(4, end4)).padEnd(4, "f");
51
+ const str7 = limitHexStr(assetStr.substring(mid + 4, mid + 8).padEnd(4, end4)).padEnd(4, "f");
52
+ const str8 = limitHexStr(end4.padEnd(4, start4).padEnd(4, end4));
53
+ return `@assets/${str1}${str2}-${str3}-${str4}-${str5}-${str6}${str7}${str8}`;
54
+ }
55
+ function isAssetId(id) {
56
+ return /^@assets\/[0-9a-z]{8,8}\-[0-9a-z]{4,4}\-[0-9a-z]{4,4}\-[0-9a-z]{4,4}\-[0-9a-z]{12,12}$/.test(`${id}`);
34
57
  }
35
58
  function parsePrototype(data) {
36
59
  const typeStr = Object.prototype.toString.call(data) || "";
@@ -114,7 +137,7 @@ var iDrawRenderer = function(exports) {
114
137
  };
115
138
  });
116
139
  }
117
- var __awaiter = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
140
+ var __awaiter = function(thisArg, _arguments, P, generator) {
118
141
  function adopt(value) {
119
142
  return value instanceof P ? value : new P(function(resolve) {
120
143
  resolve(value);
@@ -322,9 +345,8 @@ var iDrawRenderer = function(exports) {
322
345
  function parseAngleToRadian(angle2) {
323
346
  return angle2 / 180 * Math.PI;
324
347
  }
325
- function rotateElement(ctx, elemSize, callback) {
326
- const center = calcElementCenter(elemSize);
327
- const radian = parseAngleToRadian(elemSize.angle || 0);
348
+ function rotateByCenter(ctx, angle2, center, callback) {
349
+ const radian = parseAngleToRadian(angle2 || 0);
328
350
  if (center && (radian > 0 || radian < 0)) {
329
351
  ctx.translate(center.x, center.y);
330
352
  ctx.rotate(radian);
@@ -337,6 +359,12 @@ var iDrawRenderer = function(exports) {
337
359
  ctx.translate(-center.x, -center.y);
338
360
  }
339
361
  }
362
+ function rotateElement(ctx, elemSize, callback) {
363
+ const center = calcElementCenter(elemSize);
364
+ rotateByCenter(ctx, elemSize.angle || 0, center, () => {
365
+ callback(ctx);
366
+ });
367
+ }
340
368
  function calcElementCenter(elem) {
341
369
  const p = {
342
370
  x: elem.x + elem.w / 2,
@@ -351,40 +379,117 @@ var iDrawRenderer = function(exports) {
351
379
  });
352
380
  return path;
353
381
  }
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
- });
382
+ function getDefaultElementDetailConfig() {
383
+ const config = {
384
+ boxSizing: "center-line",
385
+ borderWidth: 0,
386
+ borderColor: "#000000",
387
+ shadowColor: "#000000",
388
+ borderRadius: 0,
389
+ borderDash: [],
390
+ shadowOffsetX: 0,
391
+ shadowOffsetY: 0,
392
+ shadowBlur: 0,
393
+ opacity: 1,
394
+ color: "#000000",
395
+ textAlign: "left",
396
+ verticalAlign: "top",
397
+ fontSize: 16,
398
+ lineHeight: 20,
399
+ fontFamily: "sans-serif",
400
+ fontWeight: 400
401
+ };
402
+ return config;
380
403
  }
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;
404
+ const defaultElemConfig$1 = getDefaultElementDetailConfig();
405
+ function calcViewBoxSize(viewElem, opts) {
406
+ const { viewScaleInfo } = opts;
407
+ const { scale } = viewScaleInfo;
408
+ let { borderRadius: borderRadius2, boxSizing = defaultElemConfig$1.boxSizing, borderWidth: borderWidth2 } = viewElem.detail;
409
+ if (typeof borderWidth2 !== "number") {
410
+ borderRadius2 = 0;
411
+ }
412
+ let { x: x2, y: y2, w: w2, h: h2 } = viewElem;
413
+ let radiusList = [0, 0, 0, 0];
414
+ if (typeof borderRadius2 === "number") {
415
+ const br = borderRadius2 * scale;
416
+ radiusList = [br, br, br, br];
417
+ } else if (Array.isArray(borderRadius2) && (borderRadius2 === null || borderRadius2 === void 0 ? void 0 : borderRadius2.length) === 4) {
418
+ radiusList = [borderRadius2[0] * scale, borderRadius2[1] * scale, borderRadius2[2] * scale, borderRadius2[3] * scale];
419
+ }
420
+ let bw = 0;
421
+ if (typeof borderWidth2 === "number") {
422
+ bw = (borderWidth2 || 1) * scale;
423
+ }
424
+ if (boxSizing === "border-box") {
425
+ x2 = viewElem.x + bw / 2;
426
+ y2 = viewElem.y + bw / 2;
427
+ w2 = viewElem.w - bw;
428
+ h2 = viewElem.h - bw;
429
+ } else if (boxSizing === "content-box") {
430
+ x2 = viewElem.x - bw / 2;
431
+ y2 = viewElem.y - bw / 2;
432
+ w2 = viewElem.w + bw;
433
+ h2 = viewElem.h + bw;
385
434
  } else {
386
- ctx.globalAlpha = 1;
435
+ x2 = viewElem.x;
436
+ y2 = viewElem.y;
437
+ w2 = viewElem.w;
438
+ h2 = viewElem.h;
439
+ }
440
+ return {
441
+ x: x2,
442
+ y: y2,
443
+ w: w2,
444
+ h: h2,
445
+ radiusList
446
+ };
447
+ }
448
+ function createColorStyle(ctx, color2, opts) {
449
+ if (typeof color2 === "string") {
450
+ return color2;
451
+ }
452
+ const { viewElementSize, viewScaleInfo, opacity = 1 } = opts;
453
+ const { x: x2, y: y2 } = viewElementSize;
454
+ const { scale } = viewScaleInfo;
455
+ if ((color2 == null ? void 0 : color2.type) === "linear-gradient") {
456
+ const { start, end, stops } = color2;
457
+ const viewStart = {
458
+ x: x2 + start.x * scale,
459
+ y: y2 + start.y * scale
460
+ };
461
+ const viewEnd = {
462
+ x: x2 + end.x * scale,
463
+ y: y2 + end.y * scale
464
+ };
465
+ const linearGradient = ctx.createLinearGradient(viewStart.x, viewStart.y, viewEnd.x, viewEnd.y);
466
+ stops.forEach((stop) => {
467
+ linearGradient.addColorStop(stop.offset, mergeHexColorAlpha(stop.color, opacity));
468
+ });
469
+ return linearGradient;
470
+ }
471
+ if ((color2 == null ? void 0 : color2.type) === "radial-gradient") {
472
+ const { inner, outer, stops } = color2;
473
+ const viewInner = {
474
+ x: x2 + inner.x * scale,
475
+ y: y2 + inner.y * scale,
476
+ radius: inner.radius * scale
477
+ };
478
+ const viewOuter = {
479
+ x: x2 + outer.x * scale,
480
+ y: y2 + outer.y * scale,
481
+ radius: outer.radius * 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, mergeHexColorAlpha(stop.color, opacity));
486
+ });
487
+ return radialGradient;
387
488
  }
489
+ return "#000000";
490
+ }
491
+ const defaultElemConfig = getDefaultElementDetailConfig();
492
+ function drawBox(ctx, viewElem, opts) {
388
493
  const { pattern, renderContent, originElem, calcElemSize, viewScaleInfo, viewSizeInfo } = opts || {};
389
494
  drawClipPath(ctx, viewElem, {
390
495
  originElem,
@@ -392,12 +497,18 @@ var iDrawRenderer = function(exports) {
392
497
  viewScaleInfo,
393
498
  viewSizeInfo,
394
499
  renderContent: () => {
395
- drawBoxBorder(ctx, viewElem, { viewScaleInfo, viewSizeInfo });
500
+ var _a, _b;
501
+ 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) {
502
+ ctx.globalAlpha = viewElem.detail.opacity;
503
+ } else {
504
+ ctx.globalAlpha = 1;
505
+ }
396
506
  drawBoxBackground(ctx, viewElem, { pattern, viewScaleInfo, viewSizeInfo });
397
507
  renderContent == null ? void 0 : renderContent();
508
+ drawBoxBorder(ctx, viewElem, { viewScaleInfo, viewSizeInfo });
509
+ ctx.globalAlpha = 1;
398
510
  }
399
511
  });
400
- ctx.globalAlpha = 1;
401
512
  }
402
513
  function drawClipPath(ctx, viewElem, opts) {
403
514
  const { renderContent, originElem, calcElemSize, viewScaleInfo, viewSizeInfo } = opts;
@@ -430,21 +541,20 @@ var iDrawRenderer = function(exports) {
430
541
  }
431
542
  function drawBoxBackground(ctx, viewElem, opts) {
432
543
  var _a, _b;
433
- const { pattern, viewScaleInfo } = opts;
544
+ const { pattern, viewScaleInfo, viewSizeInfo } = opts;
434
545
  let transform = [];
546
+ viewElem.detail;
435
547
  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;
438
- r = Math.min(r, w2 / 2, h2 / 2);
439
- if (w2 < r * 2 || h2 < r * 2) {
440
- r = 0;
441
- }
548
+ const { x: x2, y: y2, w: w2, h: h2, radiusList } = calcViewBoxSize(viewElem, {
549
+ viewScaleInfo,
550
+ viewSizeInfo
551
+ });
442
552
  ctx.beginPath();
443
- ctx.moveTo(x2 + r, y2);
444
- ctx.arcTo(x2 + w2, y2, x2 + w2, y2 + h2, r);
445
- ctx.arcTo(x2 + w2, y2 + h2, x2, y2 + h2, r);
446
- ctx.arcTo(x2, y2 + h2, x2, y2, r);
447
- ctx.arcTo(x2, y2, x2 + w2, y2, r);
553
+ ctx.moveTo(x2 + radiusList[0], y2);
554
+ ctx.arcTo(x2 + w2, y2, x2 + w2, y2 + h2, radiusList[1]);
555
+ ctx.arcTo(x2 + w2, y2 + h2, x2, y2 + h2, radiusList[2]);
556
+ ctx.arcTo(x2, y2 + h2, x2, y2, radiusList[3]);
557
+ ctx.arcTo(x2, y2, x2 + w2, y2, radiusList[0]);
448
558
  ctx.closePath();
449
559
  if (typeof pattern === "string") {
450
560
  ctx.fillStyle = pattern;
@@ -452,39 +562,20 @@ var iDrawRenderer = function(exports) {
452
562
  ctx.fillStyle = pattern;
453
563
  } else if (typeof viewElem.detail.background === "string") {
454
564
  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);
565
+ } else if (((_a = viewElem.detail.background) == null ? void 0 : _a.type) === "linear-gradient") {
566
+ const colorStyle = createColorStyle(ctx, viewElem.detail.background, {
567
+ viewElementSize: { x: x2, y: y2, w: w2, h: h2 },
568
+ viewScaleInfo,
569
+ opacity: ctx.globalAlpha
468
570
  });
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);
571
+ ctx.fillStyle = colorStyle;
572
+ } else if (((_b = viewElem.detail.background) == null ? void 0 : _b.type) === "radial-gradient") {
573
+ const colorStyle = createColorStyle(ctx, viewElem.detail.background, {
574
+ viewElementSize: { x: x2, y: y2, w: w2, h: h2 },
575
+ viewScaleInfo,
576
+ opacity: ctx.globalAlpha
486
577
  });
487
- ctx.fillStyle = radialGradient;
578
+ ctx.fillStyle = colorStyle;
488
579
  if (transform && transform.length > 0) {
489
580
  for (let i = 0; i < (transform == null ? void 0 : transform.length); i++) {
490
581
  const action = transform[i];
@@ -505,95 +596,139 @@ var iDrawRenderer = function(exports) {
505
596
  }
506
597
  }
507
598
  function drawBoxBorder(ctx, viewElem, opts) {
599
+ var _a, _b;
600
+ if (viewElem.detail.borderWidth === 0) {
601
+ return;
602
+ }
508
603
  if (!isColorStr(viewElem.detail.borderColor)) {
509
604
  return;
510
605
  }
606
+ 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) {
607
+ ctx.globalAlpha = viewElem.detail.opacity;
608
+ } else {
609
+ ctx.globalAlpha = 1;
610
+ }
511
611
  const { viewScaleInfo } = opts;
512
- let borderColor = "#000000";
612
+ const { scale } = viewScaleInfo;
613
+ let borderColor = defaultElemConfig.borderColor;
513
614
  if (isColorStr(viewElem.detail.borderColor) === true) {
514
615
  borderColor = viewElem.detail.borderColor;
515
616
  }
516
- const { borderWidth: borderWidth2, borderRadius: borderRadius2, borderDash } = viewElem.detail;
617
+ const { borderWidth: borderWidth2, borderRadius: borderRadius2, borderDash, boxSizing = defaultElemConfig.boxSizing } = viewElem.detail;
517
618
  let bw = 0;
518
619
  if (typeof borderWidth2 === "number") {
519
620
  bw = borderWidth2 || 1;
520
621
  }
521
- bw = bw * viewScaleInfo.scale;
522
- let r = borderRadius2 || 0;
622
+ bw = bw * scale;
623
+ let radiusList = [0, 0, 0, 0];
624
+ if (typeof borderRadius2 === "number") {
625
+ const br = borderRadius2 * scale;
626
+ radiusList = [br, br, br, br];
627
+ } else if (Array.isArray(borderRadius2) && (borderRadius2 == null ? void 0 : borderRadius2.length) === 4) {
628
+ radiusList = [borderRadius2[0] * scale, borderRadius2[1] * scale, borderRadius2[2] * scale, borderRadius2[3] * scale];
629
+ }
523
630
  ctx.strokeStyle = borderColor;
524
- ctx.setLineDash(borderDash || []);
631
+ let viewBorderDash = [];
632
+ if (Array.isArray(borderDash) && borderDash.length > 0) {
633
+ viewBorderDash = borderDash.map((num) => Math.ceil(num * scale));
634
+ }
525
635
  let borderTop = 0;
526
636
  let borderRight = 0;
527
637
  let borderBottom = 0;
528
638
  let borderLeft = 0;
529
639
  if (Array.isArray(borderWidth2)) {
530
- borderTop = borderWidth2[0] || 0;
531
- borderRight = borderWidth2[1] || 0;
532
- borderBottom = borderWidth2[2] || 0;
533
- borderLeft = borderWidth2[3] || 0;
640
+ borderTop = (borderWidth2[0] || 0) * scale;
641
+ borderRight = (borderWidth2[1] || 0) * scale;
642
+ borderBottom = (borderWidth2[2] || 0) * scale;
643
+ borderLeft = (borderWidth2[3] || 0) * scale;
534
644
  }
535
645
  if (borderLeft || borderRight || borderTop || borderBottom) {
536
- const { x: x2, y: y2, w: w2, h: h2 } = viewElem;
537
- if (borderLeft) {
646
+ ctx.lineCap = "butt";
647
+ let { x: x2, y: y2, w: w2, h: h2 } = viewElem;
648
+ if (boxSizing === "border-box") {
649
+ x2 = x2 + borderLeft / 2;
650
+ y2 = y2 + borderTop / 2;
651
+ w2 = w2 - borderLeft / 2 - borderRight / 2;
652
+ h2 = h2 - borderTop / 2 - borderBottom / 2;
653
+ } else if (boxSizing === "content-box") {
654
+ x2 = x2 - borderLeft / 2;
655
+ y2 = y2 - borderTop / 2;
656
+ w2 = w2 + borderLeft / 2 + borderRight / 2;
657
+ h2 = h2 + borderTop / 2 + borderBottom / 2;
658
+ } else {
659
+ x2 = viewElem.x;
660
+ y2 = viewElem.y;
661
+ w2 = viewElem.w;
662
+ h2 = viewElem.h;
663
+ }
664
+ if (borderTop) {
538
665
  ctx.beginPath();
539
- ctx.lineWidth = borderLeft * viewScaleInfo.scale;
540
- ctx.moveTo(x2, y2);
541
- ctx.lineTo(x2, y2 + h2);
666
+ ctx.lineWidth = borderTop;
667
+ ctx.moveTo(x2 - borderLeft / 2, y2);
668
+ ctx.lineTo(x2 + w2 + borderRight / 2, y2);
542
669
  ctx.closePath();
543
670
  ctx.stroke();
544
671
  }
545
672
  if (borderRight) {
546
673
  ctx.beginPath();
547
- ctx.lineWidth = borderRight * viewScaleInfo.scale;
548
- ctx.moveTo(x2 + w2, y2);
549
- ctx.lineTo(x2 + w2, y2 + h2);
674
+ ctx.lineWidth = borderRight;
675
+ ctx.moveTo(x2 + w2, y2 - borderTop / 2);
676
+ ctx.lineTo(x2 + w2, y2 + h2 + borderBottom / 2);
550
677
  ctx.closePath();
551
678
  ctx.stroke();
552
679
  }
553
- if (borderTop) {
680
+ if (borderBottom) {
554
681
  ctx.beginPath();
555
- ctx.lineWidth = borderTop * viewScaleInfo.scale;
556
- ctx.moveTo(x2, y2);
557
- ctx.lineTo(x2 + w2, y2);
682
+ ctx.lineWidth = borderBottom;
683
+ ctx.moveTo(x2 - borderLeft / 2, y2 + h2);
684
+ ctx.lineTo(x2 + w2 + borderRight / 2, y2 + h2);
558
685
  ctx.closePath();
559
686
  ctx.stroke();
560
687
  }
561
- if (borderBottom) {
688
+ if (borderLeft) {
562
689
  ctx.beginPath();
563
- ctx.lineWidth = borderBottom * viewScaleInfo.scale;
564
- ctx.moveTo(x2, y2 + h2);
565
- ctx.lineTo(x2 + w2, y2 + h2);
690
+ ctx.lineWidth = borderLeft;
691
+ ctx.moveTo(x2, y2 - borderTop / 2);
692
+ ctx.lineTo(x2, y2 + h2 + borderBottom / 2);
566
693
  ctx.closePath();
567
694
  ctx.stroke();
568
695
  }
569
696
  } else {
570
697
  let { x: x2, y: y2, w: w2, h: h2 } = viewElem;
571
- const { boxSizing } = viewElem.detail;
572
698
  if (boxSizing === "border-box") {
699
+ x2 = viewElem.x + bw / 2;
700
+ y2 = viewElem.y + bw / 2;
701
+ w2 = viewElem.w - bw;
702
+ h2 = viewElem.h - bw;
703
+ } else if (boxSizing === "content-box") {
704
+ x2 = viewElem.x - bw / 2;
705
+ y2 = viewElem.y - bw / 2;
706
+ w2 = viewElem.w + bw;
707
+ h2 = viewElem.h + bw;
708
+ } else {
573
709
  x2 = viewElem.x;
574
710
  y2 = viewElem.y;
575
711
  w2 = viewElem.w;
576
712
  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;
582
713
  }
583
- r = Math.min(r, w2 / 2, h2 / 2);
584
- if (r < w2 / 2 && r < h2 / 2) {
585
- r = r + bw / 2;
714
+ if (viewBorderDash.length > 0) {
715
+ ctx.lineCap = "butt";
716
+ } else {
717
+ ctx.lineCap = "square";
586
718
  }
587
- ctx.beginPath();
719
+ ctx.setLineDash(viewBorderDash);
588
720
  ctx.lineWidth = bw;
589
- ctx.moveTo(x2 + r, y2);
590
- ctx.arcTo(x2 + w2, y2, x2 + w2, y2 + h2, r);
591
- ctx.arcTo(x2 + w2, y2 + h2, x2, y2 + h2, r);
592
- ctx.arcTo(x2, y2 + h2, x2, y2, r);
593
- ctx.arcTo(x2, y2, x2 + w2, y2, r);
721
+ ctx.beginPath();
722
+ ctx.moveTo(x2 + radiusList[0], y2);
723
+ ctx.arcTo(x2 + w2, y2, x2 + w2, y2 + h2, radiusList[1]);
724
+ ctx.arcTo(x2 + w2, y2 + h2, x2, y2 + h2, radiusList[2]);
725
+ ctx.arcTo(x2, y2 + h2, x2, y2, radiusList[3]);
726
+ ctx.arcTo(x2, y2, x2 + w2, y2, radiusList[0]);
594
727
  ctx.closePath();
595
728
  ctx.stroke();
729
+ ctx.globalAlpha = 1;
596
730
  }
731
+ ctx.setLineDash([]);
597
732
  }
598
733
  function drawBoxShadow(ctx, viewElem, opts) {
599
734
  const { detail } = viewElem;
@@ -601,7 +736,7 @@ var iDrawRenderer = function(exports) {
601
736
  const { shadowColor, shadowOffsetX, shadowOffsetY, shadowBlur } = detail;
602
737
  if (is.number(shadowBlur)) {
603
738
  ctx.save();
604
- ctx.shadowColor = shadowColor || "#000000";
739
+ ctx.shadowColor = shadowColor || defaultElemConfig.shadowColor;
605
740
  ctx.shadowOffsetX = (shadowOffsetX || 0) * viewScaleInfo.scale;
606
741
  ctx.shadowOffsetY = (shadowOffsetY || 0) * viewScaleInfo.scale;
607
742
  ctx.shadowBlur = (shadowBlur || 0) * viewScaleInfo.scale;
@@ -611,6 +746,52 @@ var iDrawRenderer = function(exports) {
611
746
  renderContent();
612
747
  }
613
748
  }
749
+ function drawCircle(ctx, elem, opts) {
750
+ const { detail, angle: angle2 } = elem;
751
+ const { background = "#000000", borderColor = "#000000", borderWidth: borderWidth2 = 0 } = detail;
752
+ const { calculator, viewScaleInfo, viewSizeInfo } = opts;
753
+ 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);
754
+ const viewElem = { ...elem, ...{ x: x2, y: y2, w: w2, h: h2, angle: angle2 } };
755
+ rotateElement(ctx, { x: x2, y: y2, w: w2, h: h2, angle: angle2 }, () => {
756
+ drawBoxShadow(ctx, viewElem, {
757
+ viewScaleInfo,
758
+ viewSizeInfo,
759
+ renderContent: () => {
760
+ var _a, _b;
761
+ const a = w2 / 2;
762
+ const b = h2 / 2;
763
+ const centerX = x2 + a;
764
+ const centerY = y2 + b;
765
+ if (((_a = elem == null ? void 0 : elem.detail) == null ? void 0 : _a.opacity) !== void 0 && ((_b = elem == null ? void 0 : elem.detail) == null ? void 0 : _b.opacity) >= 0) {
766
+ ctx.globalAlpha = elem.detail.opacity;
767
+ } else {
768
+ ctx.globalAlpha = 1;
769
+ }
770
+ if (typeof borderWidth2 === "number" && borderWidth2 > 0) {
771
+ const ba = borderWidth2 / 2 + a;
772
+ const bb = borderWidth2 / 2 + b;
773
+ ctx.beginPath();
774
+ ctx.strokeStyle = borderColor;
775
+ ctx.lineWidth = borderWidth2;
776
+ ctx.circle(centerX, centerY, ba, bb, 0, 0, 2 * Math.PI);
777
+ ctx.closePath();
778
+ ctx.stroke();
779
+ }
780
+ ctx.beginPath();
781
+ const fillStyle = createColorStyle(ctx, background, {
782
+ viewElementSize: { x: x2, y: y2, w: w2, h: h2 },
783
+ viewScaleInfo,
784
+ opacity: ctx.globalAlpha
785
+ });
786
+ ctx.fillStyle = fillStyle;
787
+ ctx.circle(centerX, centerY, a, b, 0, 0, 2 * Math.PI);
788
+ ctx.closePath();
789
+ ctx.fill();
790
+ ctx.globalAlpha = 1;
791
+ }
792
+ });
793
+ });
794
+ }
614
795
  function drawRect(ctx, elem, opts) {
615
796
  const { calculator, viewScaleInfo, viewSizeInfo } = opts;
616
797
  let { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = calculator.elementSize(elem, viewScaleInfo, viewSizeInfo);
@@ -633,20 +814,53 @@ var iDrawRenderer = function(exports) {
633
814
  });
634
815
  }
635
816
  function drawImage(ctx, elem, opts) {
636
- const content = opts.loader.getContent(elem.uuid);
817
+ const content = opts.loader.getContent(elem);
637
818
  const { calculator, viewScaleInfo, viewSizeInfo } = opts;
638
819
  const { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = calculator.elementSize(elem, viewScaleInfo, viewSizeInfo);
820
+ const viewElem = { ...elem, ...{ x: x2, y: y2, w: w2, h: h2, angle: angle2 } };
639
821
  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);
645
- }
822
+ drawBoxShadow(ctx, viewElem, {
823
+ viewScaleInfo,
824
+ viewSizeInfo,
825
+ renderContent: () => {
826
+ drawBox(ctx, viewElem, {
827
+ originElem: elem,
828
+ calcElemSize: { x: x2, y: y2, w: w2, h: h2, angle: angle2 },
829
+ viewScaleInfo,
830
+ viewSizeInfo,
831
+ renderContent: () => {
832
+ if (!content) {
833
+ opts.loader.load(elem, opts.elementAssets || {});
834
+ }
835
+ if (elem.type === "image" && content) {
836
+ const { opacity } = elem.detail;
837
+ ctx.globalAlpha = opacity ? opacity : 1;
838
+ const { x: x22, y: y22, w: w22, h: h22, radiusList } = calcViewBoxSize(viewElem, {
839
+ viewScaleInfo,
840
+ viewSizeInfo
841
+ });
842
+ ctx.save();
843
+ ctx.beginPath();
844
+ ctx.moveTo(x22 + radiusList[0], y22);
845
+ ctx.arcTo(x22 + w22, y22, x22 + w22, y22 + h22, radiusList[1]);
846
+ ctx.arcTo(x22 + w22, y22 + h22, x22, y22 + h22, radiusList[2]);
847
+ ctx.arcTo(x22, y22 + h22, x22, y22, radiusList[3]);
848
+ ctx.arcTo(x22, y22, x22 + w22, y22, radiusList[0]);
849
+ ctx.closePath();
850
+ ctx.fill();
851
+ ctx.clip();
852
+ ctx.drawImage(content, x22, y22, w22, h22);
853
+ ctx.globalAlpha = 1;
854
+ ctx.restore();
855
+ }
856
+ }
857
+ });
858
+ }
859
+ });
646
860
  });
647
861
  }
648
862
  function drawSVG(ctx, elem, opts) {
649
- const content = opts.loader.getContent(elem.uuid);
863
+ const content = opts.loader.getContent(elem);
650
864
  const { calculator, viewScaleInfo, viewSizeInfo } = opts;
651
865
  const { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = calculator.elementSize(elem, viewScaleInfo, viewSizeInfo);
652
866
  rotateElement(ctx, { x: x2, y: y2, w: w2, h: h2, angle: angle2 }, () => {
@@ -654,23 +868,30 @@ var iDrawRenderer = function(exports) {
654
868
  opts.loader.load(elem, opts.elementAssets || {});
655
869
  }
656
870
  if (elem.type === "svg" && content) {
871
+ const { opacity } = elem.detail;
872
+ ctx.globalAlpha = opacity ? opacity : 1;
657
873
  ctx.drawImage(content, x2, y2, w2, h2);
874
+ ctx.globalAlpha = 1;
658
875
  }
659
876
  });
660
877
  }
661
878
  function drawHTML(ctx, elem, opts) {
662
- const content = opts.loader.getContent(elem.uuid);
879
+ const content = opts.loader.getContent(elem);
663
880
  const { calculator, viewScaleInfo, viewSizeInfo } = opts;
664
881
  const { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = calculator.elementSize(elem, viewScaleInfo, viewSizeInfo);
665
882
  rotateElement(ctx, { x: x2, y: y2, w: w2, h: h2, angle: angle2 }, () => {
666
883
  if (!content) {
667
- opts.loader.load(elem);
884
+ opts.loader.load(elem, opts.elementAssets || {});
668
885
  }
669
886
  if (elem.type === "html" && content) {
887
+ const { opacity } = elem.detail;
888
+ ctx.globalAlpha = opacity ? opacity : 1;
670
889
  ctx.drawImage(content, x2, y2, w2, h2);
890
+ ctx.globalAlpha = 1;
671
891
  }
672
892
  });
673
893
  }
894
+ const detailConfig = getDefaultElementDetailConfig();
674
895
  function drawText(ctx, elem, opts) {
675
896
  const { calculator, viewScaleInfo, viewSizeInfo } = opts;
676
897
  const { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = calculator.elementSize(elem, viewScaleInfo, viewSizeInfo);
@@ -683,16 +904,12 @@ var iDrawRenderer = function(exports) {
683
904
  viewSizeInfo,
684
905
  renderContent: () => {
685
906
  const detail = {
686
- ...{
687
- fontSize: 12,
688
- fontFamily: "sans-serif",
689
- textAlign: "center"
690
- },
907
+ ...detailConfig,
691
908
  ...elem.detail
692
909
  };
693
- const fontSize2 = detail.fontSize * viewScaleInfo.scale;
910
+ const fontSize2 = (detail.fontSize || detailConfig.fontSize) * viewScaleInfo.scale;
694
911
  const lineHeight2 = detail.lineHeight ? detail.lineHeight * viewScaleInfo.scale : fontSize2;
695
- ctx.fillStyle = elem.detail.color;
912
+ ctx.fillStyle = elem.detail.color || detailConfig.color;
696
913
  ctx.textBaseline = "top";
697
914
  ctx.$setFont({
698
915
  fontWeight: detail.fontWeight,
@@ -928,10 +1145,20 @@ var iDrawRenderer = function(exports) {
928
1145
  });
929
1146
  });
930
1147
  }
1148
+ const defaultDetail = getDefaultElementDetailConfig();
931
1149
  function drawElementList(ctx, data, opts) {
932
1150
  const { elements = [] } = data;
933
1151
  for (let i = 0; i < elements.length; i++) {
934
- const elem = elements[i];
1152
+ const element = elements[i];
1153
+ const elem = {
1154
+ ...element,
1155
+ ...{
1156
+ detail: {
1157
+ ...defaultDetail,
1158
+ ...element == null ? void 0 : element.detail
1159
+ }
1160
+ }
1161
+ };
935
1162
  if (!opts.calculator.isElementInView(elem, opts.viewScaleInfo, opts.viewSizeInfo)) {
936
1163
  continue;
937
1164
  }
@@ -943,6 +1170,24 @@ var iDrawRenderer = function(exports) {
943
1170
  }
944
1171
  }
945
1172
  const supportElementTypes = ["image", "svg", "html"];
1173
+ const getAssetIdFromElement = (element) => {
1174
+ var _a, _b, _c;
1175
+ let source = null;
1176
+ if (element.type === "image") {
1177
+ source = ((_a = element == null ? void 0 : element.detail) == null ? void 0 : _a.src) || null;
1178
+ } else if (element.type === "svg") {
1179
+ source = ((_b = element == null ? void 0 : element.detail) == null ? void 0 : _b.svg) || null;
1180
+ } else if (element.type === "html") {
1181
+ source = ((_c = element == null ? void 0 : element.detail) == null ? void 0 : _c.html) || null;
1182
+ }
1183
+ if (typeof source === "string" && source) {
1184
+ if (isAssetId(source)) {
1185
+ return source;
1186
+ }
1187
+ return createAssetId(source);
1188
+ }
1189
+ return createAssetId(`${createUUID()}-${element.uuid}-${createUUID()}-${createUUID()}`);
1190
+ };
946
1191
  class Loader extends EventEmitter {
947
1192
  constructor() {
948
1193
  super();
@@ -1010,34 +1255,35 @@ var iDrawRenderer = function(exports) {
1010
1255
  };
1011
1256
  }
1012
1257
  _emitLoad(item) {
1013
- const uuid = item.element.uuid;
1014
- const storageItem = this._storageLoadItemMap[uuid];
1258
+ const assetId = getAssetIdFromElement(item.element);
1259
+ const storageItem = this._storageLoadItemMap[assetId];
1015
1260
  if (storageItem) {
1016
1261
  if (storageItem.startTime < item.startTime) {
1017
- this._storageLoadItemMap[uuid] = item;
1262
+ this._storageLoadItemMap[assetId] = item;
1018
1263
  this.trigger("load", { ...item, countTime: item.endTime - item.startTime });
1019
1264
  }
1020
1265
  } else {
1021
- this._storageLoadItemMap[uuid] = item;
1266
+ this._storageLoadItemMap[assetId] = item;
1022
1267
  this.trigger("load", { ...item, countTime: item.endTime - item.startTime });
1023
1268
  }
1024
1269
  }
1025
1270
  _emitError(item) {
1026
- const uuid = item.element.uuid;
1027
- const storageItem = this._storageLoadItemMap[uuid];
1271
+ const assetId = getAssetIdFromElement(item.element);
1272
+ const storageItem = this._storageLoadItemMap[assetId];
1028
1273
  if (storageItem) {
1029
1274
  if (storageItem.startTime < item.startTime) {
1030
- this._storageLoadItemMap[uuid] = item;
1275
+ this._storageLoadItemMap[assetId] = item;
1031
1276
  this.trigger("error", { ...item, countTime: item.endTime - item.startTime });
1032
1277
  }
1033
1278
  } else {
1034
- this._storageLoadItemMap[uuid] = item;
1279
+ this._storageLoadItemMap[assetId] = item;
1035
1280
  this.trigger("error", { ...item, countTime: item.endTime - item.startTime });
1036
1281
  }
1037
1282
  }
1038
1283
  _loadResource(element, assets) {
1039
1284
  const item = this._createLoadItem(element);
1040
- this._currentLoadItemMap[element.uuid] = item;
1285
+ const assetId = getAssetIdFromElement(element);
1286
+ this._currentLoadItemMap[assetId] = item;
1041
1287
  const loadFunc = this._loadFuncMap[element.type];
1042
1288
  if (typeof loadFunc === "function") {
1043
1289
  item.startTime = Date.now();
@@ -1057,7 +1303,8 @@ var iDrawRenderer = function(exports) {
1057
1303
  }
1058
1304
  _isExistingErrorStorage(element) {
1059
1305
  var _a;
1060
- const existItem = (_a = this._currentLoadItemMap) == null ? void 0 : _a[element == null ? void 0 : element.uuid];
1306
+ const assetId = getAssetIdFromElement(element);
1307
+ const existItem = (_a = this._currentLoadItemMap) == null ? void 0 : _a[assetId];
1061
1308
  if (existItem && existItem.status === "error" && existItem.source && existItem.source === this._getLoadElementSource(element)) {
1062
1309
  return true;
1063
1310
  }
@@ -1068,13 +1315,13 @@ var iDrawRenderer = function(exports) {
1068
1315
  return;
1069
1316
  }
1070
1317
  if (supportElementTypes.includes(element.type)) {
1071
- const elem = deepClone(element);
1072
- this._loadResource(elem, assets);
1318
+ this._loadResource(element, assets);
1073
1319
  }
1074
1320
  }
1075
- getContent(uuid) {
1321
+ getContent(element) {
1076
1322
  var _a, _b;
1077
- return ((_b = (_a = this._storageLoadItemMap) == null ? void 0 : _a[uuid]) == null ? void 0 : _b.content) || null;
1323
+ const assetId = getAssetIdFromElement(element);
1324
+ return ((_b = (_a = this._storageLoadItemMap) == null ? void 0 : _a[assetId]) == null ? void 0 : _b.content) || null;
1078
1325
  }
1079
1326
  }
1080
1327
  class Renderer extends EventEmitter {
@@ -1139,21 +1386,6 @@ var iDrawRenderer = function(exports) {
1139
1386
  });
1140
1387
  }
1141
1388
  }
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
- // }
1157
1389
  }
1158
1390
  exports.Renderer = Renderer;
1159
1391
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });