@meta2d/core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (202) hide show
  1. package/index.d.ts +9 -0
  2. package/index.js +10 -0
  3. package/index.js.map +1 -0
  4. package/package.json +39 -0
  5. package/src/canvas/canvas.d.ts +399 -0
  6. package/src/canvas/canvas.js +5920 -0
  7. package/src/canvas/canvas.js.map +1 -0
  8. package/src/canvas/canvasImage.d.ts +25 -0
  9. package/src/canvas/canvasImage.js +343 -0
  10. package/src/canvas/canvasImage.js.map +1 -0
  11. package/src/canvas/index.d.ts +2 -0
  12. package/src/canvas/index.js +3 -0
  13. package/src/canvas/index.js.map +1 -0
  14. package/src/canvas/magnifierCanvas.d.ts +20 -0
  15. package/src/canvas/magnifierCanvas.js +99 -0
  16. package/src/canvas/magnifierCanvas.js.map +1 -0
  17. package/src/canvas/offscreen.d.ts +2 -0
  18. package/src/canvas/offscreen.js +14 -0
  19. package/src/canvas/offscreen.js.map +1 -0
  20. package/src/core.d.ts +348 -0
  21. package/src/core.js +2465 -0
  22. package/src/core.js.map +1 -0
  23. package/src/data.d.ts +34 -0
  24. package/src/data.js +83 -0
  25. package/src/data.js.map +1 -0
  26. package/src/diagrams/arrow.d.ts +4 -0
  27. package/src/diagrams/arrow.js +47 -0
  28. package/src/diagrams/arrow.js.map +1 -0
  29. package/src/diagrams/circle.d.ts +2 -0
  30. package/src/diagrams/circle.js +9 -0
  31. package/src/diagrams/circle.js.map +1 -0
  32. package/src/diagrams/cloud.d.ts +2 -0
  33. package/src/diagrams/cloud.js +12 -0
  34. package/src/diagrams/cloud.js.map +1 -0
  35. package/src/diagrams/cube.d.ts +2 -0
  36. package/src/diagrams/cube.js +70 -0
  37. package/src/diagrams/cube.js.map +1 -0
  38. package/src/diagrams/diamond.d.ts +2 -0
  39. package/src/diagrams/diamond.js +13 -0
  40. package/src/diagrams/diamond.js.map +1 -0
  41. package/src/diagrams/file.d.ts +2 -0
  42. package/src/diagrams/file.js +18 -0
  43. package/src/diagrams/file.js.map +1 -0
  44. package/src/diagrams/gif.d.ts +5 -0
  45. package/src/diagrams/gif.js +80 -0
  46. package/src/diagrams/gif.js.map +1 -0
  47. package/src/diagrams/hexagon.d.ts +2 -0
  48. package/src/diagrams/hexagon.js +55 -0
  49. package/src/diagrams/hexagon.js.map +1 -0
  50. package/src/diagrams/iframe.d.ts +5 -0
  51. package/src/diagrams/iframe.js +52 -0
  52. package/src/diagrams/iframe.js.map +1 -0
  53. package/src/diagrams/index.d.ts +67 -0
  54. package/src/diagrams/index.js +72 -0
  55. package/src/diagrams/index.js.map +1 -0
  56. package/src/diagrams/line/curve.d.ts +16 -0
  57. package/src/diagrams/line/curve.js +233 -0
  58. package/src/diagrams/line/curve.js.map +1 -0
  59. package/src/diagrams/line/index.d.ts +4 -0
  60. package/src/diagrams/line/index.js +5 -0
  61. package/src/diagrams/line/index.js.map +1 -0
  62. package/src/diagrams/line/line.d.ts +42 -0
  63. package/src/diagrams/line/line.js +419 -0
  64. package/src/diagrams/line/line.js.map +1 -0
  65. package/src/diagrams/line/polyline.d.ts +10 -0
  66. package/src/diagrams/line/polyline.js +622 -0
  67. package/src/diagrams/line/polyline.js.map +1 -0
  68. package/src/diagrams/line/smooth.d.ts +3 -0
  69. package/src/diagrams/line/smooth.js +174 -0
  70. package/src/diagrams/line/smooth.js.map +1 -0
  71. package/src/diagrams/message.d.ts +2 -0
  72. package/src/diagrams/message.js +15 -0
  73. package/src/diagrams/message.js.map +1 -0
  74. package/src/diagrams/mindLine.d.ts +3 -0
  75. package/src/diagrams/mindLine.js +31 -0
  76. package/src/diagrams/mindLine.js.map +1 -0
  77. package/src/diagrams/mindNode.d.ts +3 -0
  78. package/src/diagrams/mindNode.js +189 -0
  79. package/src/diagrams/mindNode.js.map +1 -0
  80. package/src/diagrams/pentagon.d.ts +3 -0
  81. package/src/diagrams/pentagon.js +46 -0
  82. package/src/diagrams/pentagon.js.map +1 -0
  83. package/src/diagrams/pentagram.d.ts +3 -0
  84. package/src/diagrams/pentagram.js +77 -0
  85. package/src/diagrams/pentagram.js.map +1 -0
  86. package/src/diagrams/people.d.ts +2 -0
  87. package/src/diagrams/people.js +19 -0
  88. package/src/diagrams/people.js.map +1 -0
  89. package/src/diagrams/rectangle.d.ts +3 -0
  90. package/src/diagrams/rectangle.js +26 -0
  91. package/src/diagrams/rectangle.js.map +1 -0
  92. package/src/diagrams/svg/parse.d.ts +15 -0
  93. package/src/diagrams/svg/parse.js +326 -0
  94. package/src/diagrams/svg/parse.js.map +1 -0
  95. package/src/diagrams/svgPath.d.ts +2 -0
  96. package/src/diagrams/svgPath.js +30 -0
  97. package/src/diagrams/svgPath.js.map +1 -0
  98. package/src/diagrams/triangle.d.ts +3 -0
  99. package/src/diagrams/triangle.js +41 -0
  100. package/src/diagrams/triangle.js.map +1 -0
  101. package/src/diagrams/video.d.ts +5 -0
  102. package/src/diagrams/video.js +143 -0
  103. package/src/diagrams/video.js.map +1 -0
  104. package/src/event/event.d.ts +45 -0
  105. package/src/event/event.js +17 -0
  106. package/src/event/event.js.map +1 -0
  107. package/src/event/index.d.ts +1 -0
  108. package/src/event/index.js +2 -0
  109. package/src/event/index.js.map +1 -0
  110. package/src/map/index.d.ts +1 -0
  111. package/src/map/index.js +2 -0
  112. package/src/map/index.js.map +1 -0
  113. package/src/map/map.d.ts +20 -0
  114. package/src/map/map.js +143 -0
  115. package/src/map/map.js.map +1 -0
  116. package/src/options.d.ts +70 -0
  117. package/src/options.js +61 -0
  118. package/src/options.js.map +1 -0
  119. package/src/pen/arrow.d.ts +4 -0
  120. package/src/pen/arrow.js +186 -0
  121. package/src/pen/arrow.js.map +1 -0
  122. package/src/pen/index.d.ts +5 -0
  123. package/src/pen/index.js +6 -0
  124. package/src/pen/index.js.map +1 -0
  125. package/src/pen/math.d.ts +28 -0
  126. package/src/pen/math.js +304 -0
  127. package/src/pen/math.js.map +1 -0
  128. package/src/pen/model.d.ts +354 -0
  129. package/src/pen/model.js +172 -0
  130. package/src/pen/model.js.map +1 -0
  131. package/src/pen/render.d.ts +132 -0
  132. package/src/pen/render.js +1908 -0
  133. package/src/pen/render.js.map +1 -0
  134. package/src/pen/text.d.ts +7 -0
  135. package/src/pen/text.js +311 -0
  136. package/src/pen/text.js.map +1 -0
  137. package/src/pen/utils.d.ts +2 -0
  138. package/src/pen/utils.js +41 -0
  139. package/src/pen/utils.js.map +1 -0
  140. package/src/point/index.d.ts +1 -0
  141. package/src/point/index.js +2 -0
  142. package/src/point/index.js.map +1 -0
  143. package/src/point/point.d.ts +53 -0
  144. package/src/point/point.js +119 -0
  145. package/src/point/point.js.map +1 -0
  146. package/src/rect/index.d.ts +1 -0
  147. package/src/rect/index.js +2 -0
  148. package/src/rect/index.js.map +1 -0
  149. package/src/rect/rect.d.ts +49 -0
  150. package/src/rect/rect.js +446 -0
  151. package/src/rect/rect.js.map +1 -0
  152. package/src/rect/triangle.d.ts +2 -0
  153. package/src/rect/triangle.js +10 -0
  154. package/src/rect/triangle.js.map +1 -0
  155. package/src/scroll/index.d.ts +1 -0
  156. package/src/scroll/index.js +2 -0
  157. package/src/scroll/index.js.map +1 -0
  158. package/src/scroll/scroll.d.ts +32 -0
  159. package/src/scroll/scroll.js +193 -0
  160. package/src/scroll/scroll.js.map +1 -0
  161. package/src/store/global.d.ts +25 -0
  162. package/src/store/global.js +18 -0
  163. package/src/store/global.js.map +1 -0
  164. package/src/store/index.d.ts +2 -0
  165. package/src/store/index.js +3 -0
  166. package/src/store/index.js.map +1 -0
  167. package/src/store/store.d.ts +104 -0
  168. package/src/store/store.js +73 -0
  169. package/src/store/store.js.map +1 -0
  170. package/src/tooltip/index.d.ts +1 -0
  171. package/src/tooltip/index.js +2 -0
  172. package/src/tooltip/index.js.map +1 -0
  173. package/src/tooltip/tooltip.d.ts +40 -0
  174. package/src/tooltip/tooltip.js +162 -0
  175. package/src/tooltip/tooltip.js.map +1 -0
  176. package/src/utils/browser.d.ts +1 -0
  177. package/src/utils/browser.js +4 -0
  178. package/src/utils/browser.js.map +1 -0
  179. package/src/utils/clone.d.ts +8 -0
  180. package/src/utils/clone.js +73 -0
  181. package/src/utils/clone.js.map +1 -0
  182. package/src/utils/color.d.ts +3 -0
  183. package/src/utils/color.js +126 -0
  184. package/src/utils/color.js.map +1 -0
  185. package/src/utils/error.d.ts +2 -0
  186. package/src/utils/error.js +6 -0
  187. package/src/utils/error.js.map +1 -0
  188. package/src/utils/file.d.ts +2 -0
  189. package/src/utils/file.js +84 -0
  190. package/src/utils/file.js.map +1 -0
  191. package/src/utils/index.d.ts +7 -0
  192. package/src/utils/index.js +8 -0
  193. package/src/utils/index.js.map +1 -0
  194. package/src/utils/math.d.ts +18 -0
  195. package/src/utils/math.js +141 -0
  196. package/src/utils/math.js.map +1 -0
  197. package/src/utils/padding.d.ts +7 -0
  198. package/src/utils/padding.js +47 -0
  199. package/src/utils/padding.js.map +1 -0
  200. package/src/utils/uuid.d.ts +4 -0
  201. package/src/utils/uuid.js +13 -0
  202. package/src/utils/uuid.js.map +1 -0
@@ -0,0 +1,1908 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __read = (this && this.__read) || function (o, n) {
13
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
14
+ if (!m) return o;
15
+ var i = m.call(o), r, ar = [], e;
16
+ try {
17
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
18
+ }
19
+ catch (error) { e = { error: error }; }
20
+ finally {
21
+ try {
22
+ if (r && !r.done && (m = i["return"])) m.call(i);
23
+ }
24
+ finally { if (e) throw e.error; }
25
+ }
26
+ return ar;
27
+ };
28
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
29
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
30
+ if (ar || !(i in from)) {
31
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
32
+ ar[i] = from[i];
33
+ }
34
+ }
35
+ return to.concat(ar || Array.prototype.slice.call(from));
36
+ };
37
+ var __values = (this && this.__values) || function(o) {
38
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
39
+ if (m) return m.call(o);
40
+ if (o && typeof o.length === "number") return {
41
+ next: function () {
42
+ if (o && i >= o.length) o = void 0;
43
+ return { value: o && o[i++], done: !o };
44
+ }
45
+ };
46
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
47
+ };
48
+ import { LineAnimateType, LockState } from './model';
49
+ import { getLineRect, getSplitAnchor } from '../diagrams';
50
+ import { Direction, inheritanceProps } from '../data';
51
+ import { calcRotate, distance, facePoint, rotatePoint, scalePoint, translatePoint, TwoWay, } from '../point';
52
+ import { calcCenter, calcRightBottom, calcRelativePoint, calcRelativeRect, rectInRect, scaleRect, translateRect, } from '../rect';
53
+ import { globalStore } from '../store';
54
+ import { calcTextLines, calcTextDrawRect, calcTextRect } from './text';
55
+ import { deepClone } from '../utils/clone';
56
+ import { renderFromArrow, renderToArrow } from './arrow';
57
+ import { Gradient, PenType } from '@meta2d/core';
58
+ import { rgba } from '../utils';
59
+ export function getParent(pen, root) {
60
+ if (!pen || !pen.parentId || !pen.calculative) {
61
+ return undefined;
62
+ }
63
+ var store = pen.calculative.canvas.store;
64
+ var parent = store.pens[pen.parentId];
65
+ if (!root) {
66
+ return parent;
67
+ }
68
+ return getParent(parent, root) || parent;
69
+ }
70
+ export function getAllChildren(pen, store) {
71
+ if (!pen || !pen.children) {
72
+ return [];
73
+ }
74
+ var children = [];
75
+ pen.children.forEach(function (id) {
76
+ var child = store.pens[id];
77
+ if (child) {
78
+ children.push(child);
79
+ children.push.apply(children, __spreadArray([], __read(getAllChildren(child, store)), false));
80
+ }
81
+ });
82
+ return children;
83
+ }
84
+ function drawBkLinearGradient(ctx, pen) {
85
+ var _a = pen.calculative, worldRect = _a.worldRect, gradientFromColor = _a.gradientFromColor, gradientToColor = _a.gradientToColor, gradientAngle = _a.gradientAngle;
86
+ return linearGradient(ctx, worldRect, gradientFromColor, gradientToColor, gradientAngle);
87
+ }
88
+ /**
89
+ * 避免副作用,把创建好后的径向渐变对象返回出来
90
+ * @param ctx 画布绘制对象
91
+ * @param pen 当前画笔
92
+ * @returns 径向渐变
93
+ */
94
+ function drawBkRadialGradient(ctx, pen) {
95
+ var _a = pen.calculative, worldRect = _a.worldRect, gradientFromColor = _a.gradientFromColor, gradientToColor = _a.gradientToColor, gradientRadius = _a.gradientRadius;
96
+ if (!gradientFromColor || !gradientToColor) {
97
+ return;
98
+ }
99
+ var width = worldRect.width, height = worldRect.height, center = worldRect.center;
100
+ var centerX = center.x, centerY = center.y;
101
+ var r = width;
102
+ if (r < height) {
103
+ r = height;
104
+ }
105
+ r *= 0.5;
106
+ var grd = ctx.createRadialGradient(centerX, centerY, r * (gradientRadius || 0), centerX, centerY, r);
107
+ grd.addColorStop(0, gradientFromColor);
108
+ grd.addColorStop(1, gradientToColor);
109
+ return grd;
110
+ }
111
+ function strokeLinearGradient(ctx, pen) {
112
+ var _a = pen.calculative, worldRect = _a.worldRect, lineGradientFromColor = _a.lineGradientFromColor, lineGradientToColor = _a.lineGradientToColor, lineGradientAngle = _a.lineGradientAngle;
113
+ return linearGradient(ctx, worldRect, lineGradientFromColor, lineGradientToColor, lineGradientAngle);
114
+ }
115
+ /**
116
+ * 避免副作用,把创建好后的线性渐变对象返回出来
117
+ * @param ctx 画布绘制对象
118
+ * @param worldRect 世界坐标
119
+ * @returns 线性渐变
120
+ */
121
+ function linearGradient(ctx, worldRect, fromColor, toColor, angle) {
122
+ if (!fromColor || !toColor) {
123
+ return;
124
+ }
125
+ var x = worldRect.x, y = worldRect.y, center = worldRect.center, ex = worldRect.ex, ey = worldRect.ey;
126
+ var from = {
127
+ x: x,
128
+ y: center.y,
129
+ };
130
+ var to = {
131
+ x: ex,
132
+ y: center.y,
133
+ };
134
+ if (angle % 90 === 0 && angle % 180) {
135
+ from.x = center.x;
136
+ to.x = center.x;
137
+ if (angle % 270) {
138
+ from.y = y;
139
+ to.y = ey;
140
+ }
141
+ else {
142
+ from.y = ey;
143
+ to.y = y;
144
+ }
145
+ }
146
+ else if (angle) {
147
+ rotatePoint(from, angle, worldRect.center);
148
+ rotatePoint(to, angle, worldRect.center);
149
+ }
150
+ // contributor: https://github.com/sunnyguohua/meta2d
151
+ var grd = ctx.createLinearGradient(from.x, from.y, to.x, to.y);
152
+ grd.addColorStop(0, fromColor);
153
+ grd.addColorStop(1, toColor);
154
+ return grd;
155
+ }
156
+ /**
157
+ * 根据图片的宽高, imageRatio iconAlign 来获取图片的实际位置
158
+ * @param pen 画笔
159
+ */
160
+ function getImagePosition(pen) {
161
+ var _a = pen.calculative, rect = _a.worldIconRect, iconWidth = _a.iconWidth, iconHeight = _a.iconHeight, imgNaturalWidth = _a.imgNaturalWidth, imgNaturalHeight = _a.imgNaturalHeight;
162
+ var x = rect.x, y = rect.y, w = rect.width, h = rect.height;
163
+ if (iconWidth) {
164
+ w = iconWidth;
165
+ }
166
+ if (iconHeight) {
167
+ h = iconHeight;
168
+ }
169
+ if (imgNaturalWidth && imgNaturalHeight && pen.imageRatio) {
170
+ var scaleW = rect.width / imgNaturalWidth;
171
+ var scaleH = rect.height / imgNaturalHeight;
172
+ var scaleMin = Math.min(scaleW, scaleH);
173
+ var wDivideH = imgNaturalWidth / imgNaturalHeight;
174
+ if (iconWidth) {
175
+ h = iconWidth / wDivideH;
176
+ }
177
+ else if (iconHeight) {
178
+ w = iconHeight * wDivideH;
179
+ }
180
+ else {
181
+ w = scaleMin * imgNaturalWidth;
182
+ h = scaleMin * imgNaturalHeight;
183
+ }
184
+ }
185
+ x += (rect.width - w) / 2;
186
+ y += (rect.height - h) / 2;
187
+ switch (pen.iconAlign) {
188
+ case 'top':
189
+ y = rect.y;
190
+ break;
191
+ case 'bottom':
192
+ y = rect.ey - h;
193
+ break;
194
+ case 'left':
195
+ x = rect.x;
196
+ break;
197
+ case 'right':
198
+ x = rect.ex - w;
199
+ break;
200
+ case 'left-top':
201
+ x = rect.x;
202
+ y = rect.y;
203
+ break;
204
+ case 'right-top':
205
+ x = rect.ex - w;
206
+ y = rect.y;
207
+ break;
208
+ case 'left-bottom':
209
+ x = rect.x;
210
+ y = rect.ey - h;
211
+ break;
212
+ case 'right-bottom':
213
+ x = rect.ex - w;
214
+ y = rect.ey - h;
215
+ break;
216
+ }
217
+ return {
218
+ x: x,
219
+ y: y,
220
+ width: w,
221
+ height: h,
222
+ };
223
+ }
224
+ export function drawImage(ctx, pen) {
225
+ var _a = getImagePosition(pen), x = _a.x, y = _a.y, width = _a.width, height = _a.height;
226
+ var _b = pen.calculative, worldIconRect = _b.worldIconRect, iconRotate = _b.iconRotate, img = _b.img;
227
+ if (iconRotate) {
228
+ var _c = worldIconRect.center, centerX = _c.x, centerY = _c.y;
229
+ ctx.translate(centerX, centerY);
230
+ ctx.rotate((iconRotate * Math.PI) / 180);
231
+ ctx.translate(-centerX, -centerY);
232
+ }
233
+ ctx.drawImage(img, x, y, width, height);
234
+ }
235
+ /**
236
+ * 获取文字颜色, textColor 优先其次 color
237
+ */
238
+ export function getTextColor(pen, store) {
239
+ var _a = pen.calculative, textColor = _a.textColor, color = _a.color;
240
+ var data = store.data, options = store.options;
241
+ return textColor || color || data.color || options.textColor || options.color;
242
+ }
243
+ function drawText(ctx, pen) {
244
+ var _a = pen.calculative, fontStyle = _a.fontStyle, fontWeight = _a.fontWeight, fontSize = _a.fontSize, fontFamily = _a.fontFamily, lineHeight = _a.lineHeight, text = _a.text, hiddenText = _a.hiddenText, canvas = _a.canvas, textHasShadow = _a.textHasShadow, textBackground = _a.textBackground;
245
+ if (text == undefined || hiddenText) {
246
+ return;
247
+ }
248
+ var store = canvas.store;
249
+ ctx.save();
250
+ if (!textHasShadow) {
251
+ ctx.shadowBlur = 0;
252
+ ctx.shadowOffsetX = 0;
253
+ ctx.shadowOffsetY = 0;
254
+ }
255
+ var fill = undefined;
256
+ if (pen.calculative.hover) {
257
+ fill = pen.hoverTextColor || pen.hoverColor || store.options.hoverColor;
258
+ }
259
+ else if (pen.calculative.active) {
260
+ fill = pen.activeTextColor || pen.activeColor || store.options.activeColor;
261
+ }
262
+ ctx.fillStyle = fill || getTextColor(pen, store);
263
+ ctx.font = getFont({
264
+ fontStyle: fontStyle,
265
+ fontWeight: fontWeight,
266
+ fontFamily: fontFamily || store.options.fontFamily,
267
+ fontSize: fontSize,
268
+ lineHeight: lineHeight,
269
+ });
270
+ !pen.calculative.textDrawRect && calcTextDrawRect(ctx, pen);
271
+ var _b = pen.calculative.textDrawRect, drawRectX = _b.x, drawRectY = _b.y, width = _b.width, height = _b.height;
272
+ if (textBackground) {
273
+ ctx.save();
274
+ ctx.fillStyle = textBackground;
275
+ ctx.fillRect(drawRectX, drawRectY, width, height);
276
+ ctx.restore();
277
+ }
278
+ var y = 0.55;
279
+ var textAlign = pen.textAlign || store.options.textAlign;
280
+ var oneRowHeight = fontSize * lineHeight;
281
+ pen.calculative.textLines.forEach(function (text, i) {
282
+ var textLineWidth = pen.calculative.textLineWidths[i];
283
+ var x = 0;
284
+ if (textAlign === 'center') {
285
+ x = (width - textLineWidth) / 2;
286
+ }
287
+ else if (textAlign === 'right') {
288
+ x = width - textLineWidth;
289
+ }
290
+ ctx.fillText(text, drawRectX + x, drawRectY + (i + y) * oneRowHeight);
291
+ });
292
+ ctx.restore();
293
+ }
294
+ function drawFillText(ctx, pen, text) {
295
+ var e_1, _a;
296
+ if (text == undefined) {
297
+ return;
298
+ }
299
+ var _b = pen.calculative, fontStyle = _b.fontStyle, fontWeight = _b.fontWeight, fontSize = _b.fontSize, fontFamily = _b.fontFamily, lineHeight = _b.lineHeight, canvas = _b.canvas;
300
+ var store = canvas.store;
301
+ ctx.save();
302
+ var fill = undefined;
303
+ if (pen.calculative.hover) {
304
+ fill = pen.hoverTextColor || pen.hoverColor || store.options.hoverColor;
305
+ }
306
+ else if (pen.calculative.active) {
307
+ fill = pen.activeTextColor || pen.activeColor || store.options.activeColor;
308
+ }
309
+ ctx.fillStyle = fill || getTextColor(pen, store);
310
+ ctx.font = getFont({
311
+ fontStyle: fontStyle,
312
+ fontWeight: fontWeight,
313
+ fontFamily: fontFamily || store.options.fontFamily,
314
+ fontSize: fontSize,
315
+ lineHeight: lineHeight,
316
+ });
317
+ var w = ctx.measureText(text).width;
318
+ var t;
319
+ var prev;
320
+ try {
321
+ for (var _c = __values(pen.calculative.worldAnchors), _d = _c.next(); !_d.done; _d = _c.next()) {
322
+ var anchor = _d.value;
323
+ if (!prev) {
324
+ prev = anchor;
325
+ continue;
326
+ }
327
+ var dis = distance(prev, anchor);
328
+ var n = Math.floor(dis / w);
329
+ t = '';
330
+ for (var i = 0; i < n; i++) {
331
+ t += text;
332
+ }
333
+ var angle = calcRotate(prev, anchor) - 270;
334
+ ctx.save();
335
+ if (angle % 360 !== 0) {
336
+ var x = prev.x, y = prev.y;
337
+ ctx.translate(x, y);
338
+ var rotate = (angle * Math.PI) / 180;
339
+ ctx.rotate(rotate);
340
+ ctx.translate(-x, -y);
341
+ }
342
+ ctx.fillText(t, prev.x, prev.y + lineHeight / 2);
343
+ ctx.restore();
344
+ prev = anchor;
345
+ }
346
+ }
347
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
348
+ finally {
349
+ try {
350
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
351
+ }
352
+ finally { if (e_1) throw e_1.error; }
353
+ }
354
+ ctx.restore();
355
+ }
356
+ export function drawIcon(ctx, pen) {
357
+ var store = pen.calculative.canvas.store;
358
+ ctx.save();
359
+ ctx.shadowColor = '';
360
+ ctx.shadowBlur = 0;
361
+ ctx.shadowOffsetX = 0;
362
+ ctx.shadowOffsetY = 0;
363
+ ctx.textAlign = 'center';
364
+ ctx.textBaseline = 'middle';
365
+ var iconRect = pen.calculative.worldIconRect;
366
+ var x = iconRect.x + iconRect.width / 2;
367
+ var y = iconRect.y + iconRect.height / 2;
368
+ switch (pen.iconAlign) {
369
+ case 'top':
370
+ y = iconRect.y;
371
+ ctx.textBaseline = 'top';
372
+ break;
373
+ case 'bottom':
374
+ y = iconRect.ey;
375
+ ctx.textBaseline = 'bottom';
376
+ break;
377
+ case 'left':
378
+ x = iconRect.x;
379
+ ctx.textAlign = 'left';
380
+ break;
381
+ case 'right':
382
+ x = iconRect.ex;
383
+ ctx.textAlign = 'right';
384
+ break;
385
+ case 'left-top':
386
+ x = iconRect.x;
387
+ y = iconRect.y;
388
+ ctx.textAlign = 'left';
389
+ ctx.textBaseline = 'top';
390
+ break;
391
+ case 'right-top':
392
+ x = iconRect.ex;
393
+ y = iconRect.y;
394
+ ctx.textAlign = 'right';
395
+ ctx.textBaseline = 'top';
396
+ break;
397
+ case 'left-bottom':
398
+ x = iconRect.x;
399
+ y = iconRect.ey;
400
+ ctx.textAlign = 'left';
401
+ ctx.textBaseline = 'bottom';
402
+ break;
403
+ case 'right-bottom':
404
+ x = iconRect.ex;
405
+ y = iconRect.ey;
406
+ ctx.textAlign = 'right';
407
+ ctx.textBaseline = 'bottom';
408
+ break;
409
+ }
410
+ var fontWeight = pen.calculative.iconWeight;
411
+ var fontSize = undefined;
412
+ var fontFamily = pen.calculative.iconFamily;
413
+ if (pen.calculative.iconSize > 0) {
414
+ fontSize = pen.calculative.iconSize;
415
+ }
416
+ else if (iconRect.width > iconRect.height) {
417
+ fontSize = iconRect.height;
418
+ }
419
+ else {
420
+ fontSize = iconRect.width;
421
+ }
422
+ ctx.font = getFont({
423
+ fontSize: fontSize,
424
+ fontWeight: fontWeight,
425
+ fontFamily: fontFamily,
426
+ });
427
+ ctx.fillStyle = pen.calculative.iconColor || getTextColor(pen, store);
428
+ if (pen.calculative.iconRotate) {
429
+ ctx.translate(iconRect.center.x, iconRect.center.y);
430
+ ctx.rotate((pen.calculative.iconRotate * Math.PI) / 180);
431
+ ctx.translate(-iconRect.center.x, -iconRect.center.y);
432
+ }
433
+ ctx.beginPath();
434
+ ctx.fillText(pen.calculative.icon, x, y);
435
+ ctx.restore();
436
+ }
437
+ /**
438
+ * canvas2svg 中对 font 的解析规则比 canvas 中简单,能识别的类型很少
439
+ * @returns ctx.font
440
+ */
441
+ export function getFont(_a) {
442
+ var _b = _a === void 0 ? {} : _a, _c = _b.fontStyle, fontStyle = _c === void 0 ? 'normal' : _c, _d = _b.textDecoration, textDecoration = _d === void 0 ? 'normal' : _d, _e = _b.fontWeight, fontWeight = _e === void 0 ? 'normal' : _e, _f = _b.fontSize, fontSize = _f === void 0 ? 12 : _f, _g = _b.fontFamily, fontFamily = _g === void 0 ? 'Arial' : _g, _h = _b.lineHeight, lineHeight = _h === void 0 ? 1 : _h;
443
+ return fontStyle + " " + textDecoration + " " + fontWeight + " " + fontSize + "px/" + lineHeight + " " + fontFamily;
444
+ }
445
+ // TODO: 0.5 偏移量在 图片中可能存在问题
446
+ export function ctxFlip(ctx, pen) {
447
+ // worldRect 可能为 undefined
448
+ var _a = pen.calculative.worldRect || {}, x = _a.x, ex = _a.ex, y = _a.y, ey = _a.ey;
449
+ if (pen.calculative.flipX) {
450
+ ctx.translate(x + ex + 0.5, 0.5);
451
+ ctx.scale(-1, 1);
452
+ }
453
+ if (pen.calculative.flipY) {
454
+ ctx.translate(0.5, y + ey + 0.5);
455
+ ctx.scale(1, -1);
456
+ }
457
+ }
458
+ export function ctxRotate(ctx, pen) {
459
+ var _a = pen.calculative.worldRect.center, x = _a.x, y = _a.y;
460
+ ctx.translate(x, y);
461
+ var rotate = (pen.calculative.rotate * Math.PI) / 180;
462
+ // 目前只有水平和垂直翻转,都需要 * -1
463
+ if (pen.calculative.flipX) {
464
+ rotate *= -1;
465
+ }
466
+ else if (pen.calculative.flipY) {
467
+ rotate *= -1;
468
+ }
469
+ ctx.rotate(rotate);
470
+ ctx.translate(-x, -y);
471
+ }
472
+ export function renderPen(ctx, pen) {
473
+ var e_2, _a;
474
+ ctx.save();
475
+ ctx.translate(0.5, 0.5);
476
+ ctx.beginPath();
477
+ ctxFlip(ctx, pen);
478
+ if (pen.calculative.rotate && pen.name !== 'line') {
479
+ ctxRotate(ctx, pen);
480
+ }
481
+ if (pen.calculative.lineWidth > 1) {
482
+ ctx.lineWidth = pen.calculative.lineWidth;
483
+ }
484
+ var store = pen.calculative.canvas.store;
485
+ inspectRect(ctx, store, pen); // 审查 rect
486
+ var fill;
487
+ // 该变量控制在 hover active 状态下的节点是否设置填充颜色
488
+ var setBack = true;
489
+ if (pen.calculative.hover) {
490
+ ctx.strokeStyle = pen.hoverColor || store.options.hoverColor;
491
+ fill = pen.hoverBackground || store.options.hoverBackground;
492
+ ctx.fillStyle = fill;
493
+ fill && (setBack = false);
494
+ }
495
+ else if (pen.calculative.active) {
496
+ ctx.strokeStyle = pen.activeColor || store.options.activeColor;
497
+ fill = pen.activeBackground || store.options.activeBackground;
498
+ ctx.fillStyle = fill;
499
+ fill && (setBack = false);
500
+ }
501
+ else if (pen.calculative.isDock) {
502
+ if (pen.type === PenType.Line) {
503
+ ctx.strokeStyle = store.options.dockPenColor;
504
+ }
505
+ else {
506
+ fill = rgba(store.options.dockPenColor, 0.2);
507
+ ctx.fillStyle = fill;
508
+ fill && (setBack = false);
509
+ }
510
+ }
511
+ else {
512
+ var strokeImg = pen.calculative.strokeImg;
513
+ if (pen.calculative.strokeImage && strokeImg) {
514
+ ctx.strokeStyle = ctx.createPattern(strokeImg, 'repeat');
515
+ fill = true;
516
+ }
517
+ else {
518
+ var stroke = void 0;
519
+ // TODO: 线只有线性渐变
520
+ if (pen.calculative.strokeType === Gradient.Linear) {
521
+ stroke = strokeLinearGradient(ctx, pen);
522
+ }
523
+ else {
524
+ stroke = pen.calculative.color;
525
+ }
526
+ ctx.strokeStyle = stroke;
527
+ }
528
+ }
529
+ if (setBack) {
530
+ var backgroundImg = pen.calculative.backgroundImg;
531
+ if (pen.calculative.backgroundImage && backgroundImg) {
532
+ ctx.fillStyle = ctx.createPattern(backgroundImg, 'repeat');
533
+ fill = true;
534
+ }
535
+ else {
536
+ var back = void 0;
537
+ if (pen.calculative.bkType === Gradient.Linear) {
538
+ back = drawBkLinearGradient(ctx, pen);
539
+ }
540
+ else if (pen.calculative.bkType === Gradient.Radial) {
541
+ back = drawBkRadialGradient(ctx, pen);
542
+ }
543
+ else {
544
+ back = pen.calculative.background || store.data.penBackground;
545
+ }
546
+ ctx.fillStyle = back;
547
+ fill = !!back;
548
+ }
549
+ }
550
+ setLineCap(ctx, pen);
551
+ setLineJoin(ctx, pen);
552
+ setGlobalAlpha(ctx, pen);
553
+ if (pen.calculative.lineDash) {
554
+ ctx.setLineDash(pen.calculative.lineDash);
555
+ }
556
+ if (pen.calculative.lineDashOffset) {
557
+ ctx.lineDashOffset = pen.calculative.lineDashOffset;
558
+ }
559
+ if (pen.calculative.shadowColor) {
560
+ ctx.shadowColor = pen.calculative.shadowColor;
561
+ ctx.shadowOffsetX = pen.calculative.shadowOffsetX;
562
+ ctx.shadowOffsetY = pen.calculative.shadowOffsetY;
563
+ ctx.shadowBlur = pen.calculative.shadowBlur;
564
+ }
565
+ ctxDrawPath(true, ctx, pen, store, fill);
566
+ ctxDrawCanvas(ctx, pen);
567
+ if (!(pen.image && pen.calculative.img) && pen.calculative.icon) {
568
+ drawIcon(ctx, pen);
569
+ }
570
+ drawText(ctx, pen);
571
+ if (pen.type === PenType.Line && pen.fillTexts) {
572
+ try {
573
+ for (var _b = __values(pen.fillTexts), _c = _b.next(); !_c.done; _c = _b.next()) {
574
+ var text = _c.value;
575
+ drawFillText(ctx, pen, text);
576
+ }
577
+ }
578
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
579
+ finally {
580
+ try {
581
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
582
+ }
583
+ finally { if (e_2) throw e_2.error; }
584
+ }
585
+ }
586
+ ctx.restore();
587
+ }
588
+ /**
589
+ * 更改 ctx 的 lineCap 属性
590
+ */
591
+ export function setLineCap(ctx, pen) {
592
+ var lineCap = pen.lineCap || (pen.type ? 'round' : 'square');
593
+ if (lineCap) {
594
+ ctx.lineCap = lineCap;
595
+ }
596
+ else if (pen.type) {
597
+ ctx.lineCap = 'round';
598
+ }
599
+ }
600
+ /**
601
+ * 更改 ctx 的 lineJoin 属性
602
+ */
603
+ export function setLineJoin(ctx, pen) {
604
+ var lineJoin = pen.lineJoin;
605
+ if (lineJoin) {
606
+ ctx.lineJoin = lineJoin;
607
+ }
608
+ else if (pen.type) {
609
+ ctx.lineJoin = 'round';
610
+ }
611
+ }
612
+ /**
613
+ * 通常用在下载 svg
614
+ * canvas2svg 与 canvas ctx 设置 strokeStyle 表现不同
615
+ * 若设置值为 undefined ,canvas2svg 为空, canvas ctx 为上一个值
616
+ */
617
+ export function renderPenRaw(ctx, pen, rect) {
618
+ var e_3, _a;
619
+ var _b, _c;
620
+ ctx.save();
621
+ if (rect) {
622
+ ctx.translate(-rect.x, -rect.y);
623
+ }
624
+ // for canvas2svg
625
+ (_c = (_b = ctx).setAttrs) === null || _c === void 0 ? void 0 : _c.call(_b, pen);
626
+ // end
627
+ ctx.beginPath();
628
+ if (pen.calculative.flipX) {
629
+ if (rect) {
630
+ ctx.translate(pen.calculative.worldRect.x + pen.calculative.worldRect.ex - rect.x, -rect.y);
631
+ }
632
+ else {
633
+ ctx.translate(pen.calculative.worldRect.x + pen.calculative.worldRect.ex, 0);
634
+ }
635
+ ctx.scale(-1, 1);
636
+ }
637
+ if (pen.calculative.flipY) {
638
+ if (rect) {
639
+ ctx.translate(-rect.x, pen.calculative.worldRect.y + pen.calculative.worldRect.ey - rect.x);
640
+ }
641
+ else {
642
+ ctx.translate(0, pen.calculative.worldRect.y + pen.calculative.worldRect.ey);
643
+ }
644
+ ctx.scale(1, -1);
645
+ }
646
+ if (pen.calculative.rotate && pen.name !== 'line') {
647
+ ctxRotate(ctx, pen);
648
+ }
649
+ if (pen.calculative.lineWidth > 1) {
650
+ ctx.lineWidth = pen.calculative.lineWidth;
651
+ }
652
+ var store = pen.calculative.canvas.store;
653
+ var fill;
654
+ if (pen.calculative.hover) {
655
+ ctx.strokeStyle = pen.hoverColor || store.options.hoverColor;
656
+ ctx.fillStyle = pen.hoverBackground || store.options.hoverBackground;
657
+ fill = pen.hoverBackground || store.options.hoverBackground;
658
+ }
659
+ else if (pen.calculative.active) {
660
+ ctx.strokeStyle = pen.activeColor || store.options.activeColor;
661
+ ctx.fillStyle = pen.activeBackground || store.options.activeBackground;
662
+ fill = pen.activeBackground || store.options.activeBackground;
663
+ }
664
+ else {
665
+ if (pen.strokeImage) {
666
+ if (pen.calculative.strokeImg) {
667
+ ctx.strokeStyle = ctx.createPattern(pen.calculative.strokeImg, 'repeat');
668
+ fill = true;
669
+ }
670
+ }
671
+ else {
672
+ ctx.strokeStyle = pen.calculative.color || getGlobalColor(store);
673
+ }
674
+ if (pen.backgroundImage) {
675
+ if (pen.calculative.backgroundImg) {
676
+ ctx.fillStyle = ctx.createPattern(pen.calculative.backgroundImg, 'repeat');
677
+ fill = true;
678
+ }
679
+ }
680
+ else {
681
+ ctx.fillStyle = pen.background;
682
+ fill = !!pen.background;
683
+ }
684
+ }
685
+ setLineCap(ctx, pen);
686
+ setLineJoin(ctx, pen);
687
+ setGlobalAlpha(ctx, pen);
688
+ if (pen.calculative.lineDash) {
689
+ ctx.setLineDash(pen.calculative.lineDash);
690
+ }
691
+ if (pen.calculative.lineDashOffset) {
692
+ ctx.lineDashOffset = pen.calculative.lineDashOffset;
693
+ }
694
+ if (pen.calculative.shadowColor) {
695
+ ctx.shadowColor = pen.calculative.shadowColor;
696
+ ctx.shadowOffsetX = pen.calculative.shadowOffsetX;
697
+ ctx.shadowOffsetY = pen.calculative.shadowOffsetY;
698
+ ctx.shadowBlur = pen.calculative.shadowBlur;
699
+ }
700
+ ctxDrawPath(false, ctx, pen, store, fill);
701
+ ctxDrawCanvas(ctx, pen);
702
+ // renderPenRaw 用在 downloadPng svg , echarts 等图形需要
703
+ if (pen.calculative.img) {
704
+ ctx.save();
705
+ ctx.shadowColor = '';
706
+ ctx.shadowBlur = 0;
707
+ ctx.shadowOffsetX = 0;
708
+ ctx.shadowOffsetY = 0;
709
+ drawImage(ctx, pen);
710
+ ctx.restore();
711
+ }
712
+ else if (pen.calculative.icon) {
713
+ drawIcon(ctx, pen);
714
+ }
715
+ drawText(ctx, pen);
716
+ if (pen.type === PenType.Line && pen.fillTexts) {
717
+ try {
718
+ for (var _d = __values(pen.fillTexts), _e = _d.next(); !_e.done; _e = _d.next()) {
719
+ var text = _e.value;
720
+ drawFillText(ctx, pen, text);
721
+ }
722
+ }
723
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
724
+ finally {
725
+ try {
726
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
727
+ }
728
+ finally { if (e_3) throw e_3.error; }
729
+ }
730
+ }
731
+ ctx.restore();
732
+ }
733
+ /**
734
+ * 根据 path2D 绘制 path
735
+ * @param canUsePath 是否可使用 Path2D, downloadSvg 不可使用 path2D
736
+ */
737
+ export function ctxDrawPath(canUsePath, ctx, pen, store, fill) {
738
+ if (canUsePath === void 0) { canUsePath = true; }
739
+ var path = canUsePath
740
+ ? store.path2dMap.get(pen)
741
+ : globalStore.path2dDraws[pen.name];
742
+ if (path) {
743
+ if (pen.type === PenType.Line && pen.borderWidth) {
744
+ ctx.save();
745
+ ctx.beginPath();
746
+ var lineWidth = pen.calculative.lineWidth + pen.calculative.borderWidth;
747
+ ctx.lineWidth = lineWidth;
748
+ ctx.strokeStyle = pen.borderColor;
749
+ if (path instanceof Path2D) {
750
+ fill && ctx.fill(path);
751
+ lineWidth && ctx.stroke(path);
752
+ }
753
+ else {
754
+ path(pen, ctx);
755
+ fill && ctx.fill();
756
+ lineWidth && ctx.stroke();
757
+ }
758
+ ctx.restore();
759
+ }
760
+ if (path instanceof Path2D) {
761
+ fill && ctx.fill(path);
762
+ }
763
+ else {
764
+ ctx.save();
765
+ path(pen, ctx);
766
+ fill && ctx.fill();
767
+ ctx.restore();
768
+ }
769
+ var progress = pen.calculative.progress;
770
+ if (progress != null) {
771
+ ctx.save();
772
+ var _a = pen.calculative.worldRect, x = _a.x, y = _a.y, width = _a.width, height = _a.height, ey = _a.ey;
773
+ var grd = !pen.verticalProgress
774
+ ? ctx.createLinearGradient(x, y, x + width * progress, y)
775
+ : ctx.createLinearGradient(x, ey, x, y + height * (1 - progress));
776
+ var color = pen.calculative.progressColor ||
777
+ pen.calculative.color ||
778
+ store.options.activeColor;
779
+ grd.addColorStop(0, color);
780
+ grd.addColorStop(1, color);
781
+ grd.addColorStop(1, 'transparent');
782
+ ctx.fillStyle = grd;
783
+ if (path instanceof Path2D) {
784
+ ctx.fill(path);
785
+ }
786
+ else {
787
+ path(pen, ctx);
788
+ ctx.fill();
789
+ }
790
+ ctx.restore();
791
+ }
792
+ if (pen.calculative.lineWidth) {
793
+ if (path instanceof Path2D) {
794
+ ctx.stroke(path);
795
+ }
796
+ else {
797
+ path(pen, ctx);
798
+ ctx.stroke();
799
+ }
800
+ }
801
+ if (pen.type) {
802
+ if (pen.calculative.animatePos) {
803
+ ctx.save();
804
+ setCtxLineAnimate(ctx, pen, store);
805
+ if (path instanceof Path2D) {
806
+ ctx.stroke(path);
807
+ }
808
+ else {
809
+ path(pen, ctx);
810
+ ctx.stroke();
811
+ }
812
+ ctx.restore();
813
+ }
814
+ pen.fromArrow && renderFromArrow(ctx, pen, store);
815
+ pen.toArrow && renderToArrow(ctx, pen, store);
816
+ if (pen.calculative.active && !pen.calculative.pencil) {
817
+ renderLineAnchors(ctx, pen);
818
+ }
819
+ }
820
+ }
821
+ }
822
+ /**
823
+ * 设置线条动画,ctx 的 strokeStyle lineDash 等属性更改
824
+ */
825
+ export function setCtxLineAnimate(ctx, pen, store) {
826
+ ctx.strokeStyle = pen.animateColor || store.options.animateColor;
827
+ var len = 0;
828
+ switch (pen.lineAnimateType) {
829
+ case LineAnimateType.Beads:
830
+ if (pen.animateReverse) {
831
+ ctx.lineDashOffset = pen.calculative.animatePos;
832
+ }
833
+ else {
834
+ ctx.lineDashOffset = pen.length - pen.calculative.animatePos;
835
+ }
836
+ len = pen.calculative.lineWidth || 5;
837
+ if (len < 5) {
838
+ len = 5;
839
+ }
840
+ var dash = pen.animateLineDash &&
841
+ pen.animateLineDash.map(function (item) { return (item * len) / 5; });
842
+ ctx.setLineDash(dash || [len, len * 2]);
843
+ break;
844
+ case LineAnimateType.Dot:
845
+ if (pen.animateReverse) {
846
+ ctx.lineDashOffset = pen.calculative.animatePos;
847
+ }
848
+ else {
849
+ ctx.lineDashOffset = pen.length - pen.calculative.animatePos;
850
+ }
851
+ len =
852
+ pen.calculative.animateDotSize || pen.calculative.lineWidth * 2 || 6;
853
+ if (len < 6) {
854
+ len = 6;
855
+ }
856
+ ctx.lineWidth = len;
857
+ ctx.setLineDash([0.1, pen.length]);
858
+ break;
859
+ default:
860
+ if (pen.animateReverse) {
861
+ ctx.setLineDash([
862
+ 0,
863
+ pen.length - pen.calculative.animatePos + 1,
864
+ pen.calculative.animatePos,
865
+ ]);
866
+ }
867
+ else {
868
+ ctx.setLineDash([
869
+ pen.calculative.animatePos,
870
+ pen.length - pen.calculative.animatePos,
871
+ ]);
872
+ }
873
+ break;
874
+ }
875
+ }
876
+ /**
877
+ * 全局 color
878
+ */
879
+ export function getGlobalColor(store) {
880
+ var data = store.data, options = store.options;
881
+ return data.color || options.color;
882
+ }
883
+ export function renderLineAnchors(ctx, pen) {
884
+ var store = pen.calculative.canvas.store;
885
+ ctx.save();
886
+ ctx.lineWidth = 1;
887
+ ctx.fillStyle = pen.activeColor || store.options.activeColor;
888
+ pen.calculative.worldAnchors.forEach(function (pt) {
889
+ !pt.hidden && !pt.isTemp && renderAnchor(ctx, pt, pen);
890
+ });
891
+ ctx.restore();
892
+ }
893
+ export function renderAnchor(ctx, pt, pen) {
894
+ if (!pt) {
895
+ return;
896
+ }
897
+ var active = pen.calculative.activeAnchor === pt;
898
+ var r = 3;
899
+ if (pen.calculative.lineWidth > 3) {
900
+ r = pen.calculative.lineWidth;
901
+ }
902
+ if (active) {
903
+ if (pt.prev) {
904
+ ctx.save();
905
+ ctx.strokeStyle = '#4dffff';
906
+ ctx.beginPath();
907
+ ctx.moveTo(pt.prev.x, pt.prev.y);
908
+ ctx.lineTo(pt.x, pt.y);
909
+ ctx.stroke();
910
+ ctx.restore();
911
+ ctx.save();
912
+ ctx.fillStyle = '#ffffff';
913
+ ctx.beginPath();
914
+ ctx.arc(pt.prev.x, pt.prev.y, r, 0, Math.PI * 2);
915
+ ctx.fill();
916
+ ctx.stroke();
917
+ ctx.restore();
918
+ }
919
+ if (pt.next) {
920
+ ctx.save();
921
+ ctx.strokeStyle = '#4dffff';
922
+ ctx.beginPath();
923
+ ctx.moveTo(pt.x, pt.y);
924
+ ctx.lineTo(pt.next.x, pt.next.y);
925
+ ctx.stroke();
926
+ ctx.restore();
927
+ ctx.save();
928
+ ctx.fillStyle = '#ffffff';
929
+ ctx.beginPath();
930
+ ctx.arc(pt.next.x, pt.next.y, r, 0, Math.PI * 2);
931
+ ctx.fill();
932
+ ctx.stroke();
933
+ ctx.restore();
934
+ ctx.beginPath();
935
+ ctx.arc(pt.x, pt.y, r, 0, Math.PI * 2);
936
+ ctx.fill();
937
+ ctx.stroke();
938
+ }
939
+ ctx.beginPath();
940
+ ctx.arc(pt.x, pt.y, r, 0, Math.PI * 2);
941
+ ctx.fill();
942
+ ctx.stroke();
943
+ }
944
+ else {
945
+ ctx.save();
946
+ ctx.fillStyle = '#ffffff';
947
+ ctx.beginPath();
948
+ ctx.arc(pt.x, pt.y, r, 0, Math.PI * 2);
949
+ ctx.fill();
950
+ ctx.stroke();
951
+ ctx.restore();
952
+ }
953
+ }
954
+ export function calcWorldRects(pen) {
955
+ var store = pen.calculative.canvas.store;
956
+ var rect = {
957
+ x: pen.x,
958
+ y: pen.y,
959
+ };
960
+ if (!pen.parentId) {
961
+ rect.width = pen.width;
962
+ rect.height = pen.height;
963
+ rect.rotate = pen.rotate;
964
+ calcRightBottom(rect);
965
+ calcCenter(rect);
966
+ }
967
+ else {
968
+ var parent_1 = store.pens[pen.parentId];
969
+ var parentRect = parent_1.calculative.worldRect;
970
+ if (!parentRect) {
971
+ parentRect = calcWorldRects(parent_1);
972
+ }
973
+ rect.x = parentRect.x + parentRect.width * pen.x;
974
+ rect.y = parentRect.y + parentRect.height * pen.y;
975
+ rect.width = parentRect.width * pen.width;
976
+ rect.height = parentRect.height * pen.height;
977
+ if (parent_1.flipX) {
978
+ rect.x =
979
+ parentRect.width - (rect.x - parentRect.x + rect.width) + parentRect.x;
980
+ }
981
+ if (parent_1.flipY) {
982
+ rect.y =
983
+ parentRect.height -
984
+ (rect.y - parentRect.y + rect.height) +
985
+ parentRect.y;
986
+ }
987
+ calcRightBottom(rect);
988
+ rect.rotate = parentRect.rotate + pen.rotate;
989
+ calcCenter(rect);
990
+ }
991
+ pen.calculative.worldRect = rect;
992
+ // 这里的 rect 均是绝对值
993
+ calcPadding(pen, rect);
994
+ return rect;
995
+ }
996
+ export function calcPadding(pen, rect) {
997
+ !pen.paddingTop && (pen.calculative.paddingTop = 0);
998
+ !pen.paddingBottom && (pen.calculative.paddingBottom = 0);
999
+ !pen.paddingLeft && (pen.calculative.paddingLeft = 0);
1000
+ !pen.paddingRight && (pen.calculative.paddingRight = 0);
1001
+ pen.calculative.paddingTop < 1 && (pen.calculative.paddingTop *= rect.height);
1002
+ pen.calculative.paddingBottom < 1 &&
1003
+ (pen.calculative.paddingBottom *= rect.height);
1004
+ pen.calculative.paddingLeft < 1 &&
1005
+ (pen.calculative.paddingLeft *= rect.width);
1006
+ pen.calculative.paddingRight < 1 &&
1007
+ (pen.calculative.paddingRight *= rect.width);
1008
+ }
1009
+ export function calcPenRect(pen) {
1010
+ var worldRect = pen.calculative.worldRect;
1011
+ if (!pen.parentId) {
1012
+ Object.assign(pen, worldRect);
1013
+ return;
1014
+ }
1015
+ var store = pen.calculative.canvas.store;
1016
+ var parentRect = store.pens[pen.parentId].calculative.worldRect;
1017
+ Object.assign(pen, calcRelativeRect(worldRect, parentRect));
1018
+ }
1019
+ export function calcWorldAnchors(pen) {
1020
+ var store = pen.calculative.canvas.store;
1021
+ var anchors = [];
1022
+ if (pen.anchors) {
1023
+ pen.anchors.forEach(function (anchor) {
1024
+ anchors.push(calcWorldPointOfPen(pen, anchor));
1025
+ });
1026
+ }
1027
+ // Default anchors of node
1028
+ if (!anchors.length &&
1029
+ !pen.type &&
1030
+ !pen.calculative.canvas.parent.isCombine(pen)) {
1031
+ var _a = pen.calculative.worldRect, x_1 = _a.x, y_1 = _a.y, width_1 = _a.width, height_1 = _a.height;
1032
+ anchors = store.options.defaultAnchors.map(function (anchor, index) {
1033
+ return {
1034
+ id: "" + index,
1035
+ penId: pen.id,
1036
+ x: x_1 + width_1 * anchor.x,
1037
+ y: y_1 + height_1 * anchor.y,
1038
+ };
1039
+ });
1040
+ }
1041
+ if (pen.calculative.rotate) {
1042
+ anchors.forEach(function (anchor) {
1043
+ rotatePoint(anchor, pen.calculative.rotate, pen.calculative.worldRect.center);
1044
+ });
1045
+ }
1046
+ if (!pen.type || pen.anchors) {
1047
+ pen.calculative.worldAnchors = anchors;
1048
+ }
1049
+ if (pen.calculative.activeAnchor && anchors.length) {
1050
+ pen.calculative.activeAnchor = anchors.find(function (a) {
1051
+ a.id === pen.calculative.activeAnchor.id;
1052
+ });
1053
+ }
1054
+ }
1055
+ export function calcWorldPointOfPen(pen, pt) {
1056
+ var p = __assign({}, pt);
1057
+ var _a = pen.calculative.worldRect, x = _a.x, y = _a.y, width = _a.width, height = _a.height;
1058
+ p.x = x + width * pt.x;
1059
+ p.y = y + height * pt.y;
1060
+ if (pt.prev) {
1061
+ p.prev = {
1062
+ penId: pen.id,
1063
+ connectTo: pt.prev.connectTo,
1064
+ x: x + width * pt.prev.x,
1065
+ y: y + height * pt.prev.y,
1066
+ };
1067
+ }
1068
+ if (pt.next) {
1069
+ p.next = {
1070
+ penId: pen.id,
1071
+ connectTo: pt.next.connectTo,
1072
+ x: x + width * pt.next.x,
1073
+ y: y + height * pt.next.y,
1074
+ };
1075
+ }
1076
+ return p;
1077
+ }
1078
+ export function calcIconRect(pens, pen) {
1079
+ var _a = pen.calculative, paddingTop = _a.paddingTop, paddingBottom = _a.paddingBottom, paddingLeft = _a.paddingLeft, paddingRight = _a.paddingRight;
1080
+ var x = paddingLeft;
1081
+ var y = paddingTop;
1082
+ var width = pen.calculative.worldRect.width - paddingLeft - paddingRight;
1083
+ var height = pen.calculative.worldRect.height - paddingTop - paddingBottom;
1084
+ var iconLeft = pen.calculative.iconLeft;
1085
+ var iconTop = pen.calculative.iconTop;
1086
+ if (iconLeft && Math.abs(iconLeft) < 1) {
1087
+ iconLeft = pen.calculative.worldRect.width * iconLeft;
1088
+ }
1089
+ if (iconTop && Math.abs(iconTop) < 1) {
1090
+ iconTop = pen.calculative.worldRect.height * iconTop;
1091
+ }
1092
+ x += iconLeft || 0;
1093
+ y += iconTop || 0;
1094
+ width -= iconLeft || 0;
1095
+ height -= iconTop || 0;
1096
+ var rotate = pen.calculative.iconRotate || 0;
1097
+ if (pen.parentId) {
1098
+ var parentPen = pens[pen.parentId].calculative;
1099
+ if (parentPen) {
1100
+ rotate += parentPen.rotate;
1101
+ rotate %= 360;
1102
+ }
1103
+ }
1104
+ x = pen.calculative.worldRect.x + x;
1105
+ y = pen.calculative.worldRect.y + y;
1106
+ pen.calculative.worldIconRect = {
1107
+ x: x,
1108
+ y: y,
1109
+ width: width,
1110
+ height: height,
1111
+ rotate: rotate,
1112
+ };
1113
+ calcRightBottom(pen.calculative.worldIconRect);
1114
+ calcCenter(pen.calculative.worldIconRect);
1115
+ }
1116
+ export function scalePen(pen, scale, center) {
1117
+ scaleRect(pen.calculative.worldRect, scale, center);
1118
+ if (pen.calculative.initRect) {
1119
+ scaleRect(pen.calculative.initRect, scale, center);
1120
+ }
1121
+ if (pen.calculative.x) {
1122
+ scalePoint(pen.calculative, scale, center);
1123
+ }
1124
+ if (pen.type) {
1125
+ calcWorldAnchors(pen);
1126
+ }
1127
+ }
1128
+ export function pushPenAnchor(pen, pt) {
1129
+ if (!pen.anchors) {
1130
+ pen.anchors = [];
1131
+ }
1132
+ if (!pen.calculative.worldAnchors) {
1133
+ pen.calculative.worldAnchors = [];
1134
+ }
1135
+ var worldAnchor = {
1136
+ id: pt.id,
1137
+ penId: pen.id,
1138
+ x: pt.x,
1139
+ y: pt.y,
1140
+ };
1141
+ pen.calculative.worldAnchors.push(worldAnchor);
1142
+ if (pen.calculative.worldRect) {
1143
+ if (pen.rotate % 360) {
1144
+ rotatePoint(pt, -pen.rotate, pen.calculative.worldRect.center);
1145
+ }
1146
+ var anchor = {
1147
+ id: pt.id,
1148
+ penId: pen.id,
1149
+ x: (pt.x - pen.calculative.worldRect.x) / pen.calculative.worldRect.width,
1150
+ y: (pt.y - pen.calculative.worldRect.y) / pen.calculative.worldRect.height,
1151
+ };
1152
+ pen.anchors.push(anchor);
1153
+ }
1154
+ return worldAnchor;
1155
+ }
1156
+ export function addLineAnchor(pen, pt, index) {
1157
+ if (!pen.anchors) {
1158
+ pen.anchors = [];
1159
+ }
1160
+ if (!pen.calculative.worldAnchors) {
1161
+ pen.calculative.worldAnchors = [];
1162
+ }
1163
+ var worldAnchor = getSplitAnchor(pen, pt, index);
1164
+ pen.calculative.worldAnchors.splice(index + 1, 0, worldAnchor);
1165
+ pen.anchors.splice(index + 1, 0, calcRelativePoint(worldAnchor, pen.calculative.worldRect));
1166
+ pen.calculative.activeAnchor = worldAnchor;
1167
+ return worldAnchor;
1168
+ }
1169
+ export function removePenAnchor(pen, anchor) {
1170
+ if (!pen || !pen.calculative.worldAnchors) {
1171
+ return;
1172
+ }
1173
+ var i = pen.calculative.worldAnchors.findIndex(function (a) { return a.id === anchor.id; });
1174
+ if (i > -1) {
1175
+ pen.calculative.worldAnchors.splice(i, 1);
1176
+ }
1177
+ i = pen.anchors.findIndex(function (a) { return a.id === anchor.id; });
1178
+ if (i > -1) {
1179
+ pen.anchors.splice(i, 1);
1180
+ }
1181
+ }
1182
+ export function facePen(pt, pen) {
1183
+ if (!pen || !pen.calculative || !pen.calculative.worldRect.center) {
1184
+ return Direction.None;
1185
+ }
1186
+ return facePoint(pt, pen.calculative.worldRect.center);
1187
+ }
1188
+ export function nearestAnchor(pen, pt) {
1189
+ var dis = Infinity;
1190
+ var anchor;
1191
+ pen.calculative.worldAnchors.forEach(function (a) {
1192
+ var d = distance(pt, a);
1193
+ if (dis > d) {
1194
+ dis = d;
1195
+ anchor = a;
1196
+ }
1197
+ });
1198
+ return anchor;
1199
+ }
1200
+ export function translateLine(pen, x, y) {
1201
+ pen.x += x;
1202
+ pen.y += y;
1203
+ if (pen.anchors) {
1204
+ pen.anchors.forEach(function (a) {
1205
+ translatePoint(a, x, y);
1206
+ });
1207
+ }
1208
+ if (pen.calculative.worldAnchors) {
1209
+ pen.calculative.worldAnchors.forEach(function (a) {
1210
+ translatePoint(a, x, y);
1211
+ });
1212
+ }
1213
+ }
1214
+ export function deleteTempAnchor(pen) {
1215
+ if (pen && pen.calculative && pen.calculative.worldAnchors.length) {
1216
+ var to = getToAnchor(pen);
1217
+ // 第一次画线
1218
+ if (!pen.anchors || !pen.anchors.length) {
1219
+ while (pen.calculative.worldAnchors.length &&
1220
+ to !== pen.calculative.activeAnchor) {
1221
+ pen.calculative.worldAnchors.pop();
1222
+ to = getToAnchor(pen);
1223
+ }
1224
+ }
1225
+ // 拖拽终点
1226
+ else if (to === pen.calculative.activeAnchor) {
1227
+ pen.calculative.worldAnchors = [pen.calculative.worldAnchors[0]];
1228
+ }
1229
+ // 拖拽起点
1230
+ else if (pen.calculative.worldAnchors[0] === pen.calculative.activeAnchor) {
1231
+ pen.calculative.worldAnchors = [
1232
+ pen.calculative.worldAnchors[pen.calculative.worldAnchors.length - 1],
1233
+ ];
1234
+ }
1235
+ }
1236
+ }
1237
+ /**
1238
+ * 添加line到pen的connectedLines中,并关联相关属性
1239
+ * 不添加连线到画布中,请确保画布中已经有该连线。
1240
+ * */
1241
+ export function connectLine(pen, anchor, line, lineAnchor) {
1242
+ if (!pen ||
1243
+ !anchor ||
1244
+ !line ||
1245
+ !lineAnchor ||
1246
+ anchor.twoWay === TwoWay.DisableConnected ||
1247
+ anchor.twoWay === TwoWay.Disable ||
1248
+ lineAnchor.twoWay === TwoWay.DisableConnectTo ||
1249
+ lineAnchor.twoWay === TwoWay.Disable) {
1250
+ return;
1251
+ }
1252
+ if (anchor.twoWay === TwoWay.In) {
1253
+ if (line.calculative.worldAnchors.length === 1) {
1254
+ return;
1255
+ }
1256
+ var to = getToAnchor(line);
1257
+ if (lineAnchor.id !== to.id) {
1258
+ return;
1259
+ }
1260
+ }
1261
+ if (anchor.twoWay === TwoWay.Out) {
1262
+ var from = getFromAnchor(line);
1263
+ if (lineAnchor.id !== from.id) {
1264
+ return;
1265
+ }
1266
+ }
1267
+ if (lineAnchor.connectTo === pen.id && lineAnchor.anchorId === anchor.id) {
1268
+ return;
1269
+ }
1270
+ if (lineAnchor.connectTo) {
1271
+ var p = pen.calculative.canvas.store.pens[lineAnchor.connectTo];
1272
+ disconnectLine(p, getAnchor(p, lineAnchor.anchorId), line, lineAnchor);
1273
+ }
1274
+ if (!pen.connectedLines) {
1275
+ pen.connectedLines = [];
1276
+ }
1277
+ var i = pen.connectedLines.findIndex(function (item) {
1278
+ return item.lineId === line.id &&
1279
+ item.lineAnchor === lineAnchor.id &&
1280
+ item.anchor === anchor.id;
1281
+ });
1282
+ if (i < 0) {
1283
+ pen.connectedLines.push({
1284
+ lineId: line.id,
1285
+ lineAnchor: lineAnchor.id,
1286
+ anchor: anchor.id,
1287
+ });
1288
+ }
1289
+ lineAnchor.connectTo = pen.id;
1290
+ lineAnchor.anchorId = anchor.id;
1291
+ // 如果两条连线,则相互关联
1292
+ if (pen.type) {
1293
+ connectLine(line, lineAnchor, pen, anchor);
1294
+ }
1295
+ pen.calculative.canvas.store.emitter.emit('connectLine', {
1296
+ line: line,
1297
+ lineAnchor: lineAnchor,
1298
+ pen: pen,
1299
+ anchor: anchor,
1300
+ });
1301
+ return true;
1302
+ }
1303
+ /**
1304
+ * 从 pen.connectedLines 中删除 lineId 和 lineAnchor
1305
+ */
1306
+ export function disconnectLine(pen, anchor, line, lineAnchor) {
1307
+ if (!pen || !anchor || !line || !lineAnchor) {
1308
+ return;
1309
+ }
1310
+ if (!pen.connectedLines || !pen.connectedLines.length) {
1311
+ return;
1312
+ }
1313
+ pen.connectedLines.forEach(function (item, index, arr) {
1314
+ if ((item.lineId === line.id || item.lineId === line.id) &&
1315
+ item.lineAnchor === lineAnchor.id &&
1316
+ item.anchor === anchor.id) {
1317
+ arr.splice(index, 1);
1318
+ }
1319
+ });
1320
+ lineAnchor.connectTo = undefined;
1321
+ lineAnchor.anchorId = undefined;
1322
+ // 如果两条连线相互关联,则都取消关联
1323
+ if (pen.type &&
1324
+ anchor.connectTo === line.id &&
1325
+ anchor.anchorId === lineAnchor.id) {
1326
+ disconnectLine(line, lineAnchor, pen, anchor);
1327
+ }
1328
+ pen.calculative.canvas.store.emitter.emit('disconnectLine', {
1329
+ line: line,
1330
+ lineAnchor: lineAnchor,
1331
+ pen: pen,
1332
+ anchor: anchor,
1333
+ });
1334
+ return true;
1335
+ }
1336
+ export function getAnchor(pen, anchorId) {
1337
+ var _a;
1338
+ if (!pen || !anchorId) {
1339
+ return;
1340
+ }
1341
+ return (_a = pen.calculative.worldAnchors) === null || _a === void 0 ? void 0 : _a.find(function (item) { return item.id === anchorId; });
1342
+ }
1343
+ export function getFromAnchor(pen) {
1344
+ if (!pen || !pen.calculative.worldAnchors) {
1345
+ return;
1346
+ }
1347
+ return pen.calculative.worldAnchors[0];
1348
+ }
1349
+ export function getToAnchor(pen) {
1350
+ if (!pen || !pen.calculative.worldAnchors) {
1351
+ return;
1352
+ }
1353
+ return pen.calculative.worldAnchors[pen.calculative.worldAnchors.length - 1];
1354
+ }
1355
+ export function setNodeAnimate(pen, now) {
1356
+ var e_4, _a, e_5, _b;
1357
+ if (pen.calculative.start === 0 || !pen.frames || !pen.frames.length) {
1358
+ pen.calculative.start = undefined;
1359
+ return 0;
1360
+ }
1361
+ if (!pen.calculative.duration) {
1362
+ pen.calculative.duration = 0;
1363
+ try {
1364
+ for (var _c = __values(pen.frames), _d = _c.next(); !_d.done; _d = _c.next()) {
1365
+ var f = _d.value;
1366
+ pen.calculative.duration += f.duration;
1367
+ for (var k in f) {
1368
+ if (k !== 'duration' && !pen[k]) {
1369
+ if (k === 'scale') {
1370
+ pen[k] = 1;
1371
+ }
1372
+ }
1373
+ }
1374
+ }
1375
+ }
1376
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
1377
+ finally {
1378
+ try {
1379
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
1380
+ }
1381
+ finally { if (e_4) throw e_4.error; }
1382
+ }
1383
+ }
1384
+ if (!pen.animateCycle) {
1385
+ pen.animateCycle = Infinity;
1386
+ }
1387
+ if (!pen.calculative.start) {
1388
+ pen.calculative.start = now;
1389
+ pen.calculative.frameIndex = 0;
1390
+ pen.calculative.frameStart = pen.calculative.start;
1391
+ pen.calculative.frameDuration = pen.frames[0].duration;
1392
+ pen.calculative.frameEnd =
1393
+ pen.calculative.frameStart + pen.calculative.frameDuration;
1394
+ pen.calculative.cycleIndex = 1;
1395
+ pen.calculative.x = pen.calculative.worldRect.x;
1396
+ pen.calculative.y = pen.calculative.worldRect.y;
1397
+ pen.calculative.initRect = deepClone(pen.calculative.worldRect);
1398
+ pen.calculative.initRect.rotate = pen.calculative.rotate || 0;
1399
+ initPrevFrame(pen);
1400
+ }
1401
+ else {
1402
+ var frameIndex = 0;
1403
+ var cycleIndex = Math.ceil((now - pen.calculative.start) / pen.calculative.duration);
1404
+ // 播放结束
1405
+ if (cycleIndex > pen.animateCycle) {
1406
+ pen.calculative.start = undefined;
1407
+ setNodeAnimateProcess(pen, 1);
1408
+ return 0;
1409
+ }
1410
+ var pos = (now - pen.calculative.start) % pen.calculative.duration;
1411
+ var d = 0;
1412
+ try {
1413
+ for (var _e = __values(pen.frames), _f = _e.next(); !_f.done; _f = _e.next()) {
1414
+ var frame = _f.value;
1415
+ d += frame.duration;
1416
+ if (pos > d) {
1417
+ ++frameIndex;
1418
+ }
1419
+ else {
1420
+ break;
1421
+ }
1422
+ }
1423
+ }
1424
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
1425
+ finally {
1426
+ try {
1427
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
1428
+ }
1429
+ finally { if (e_5) throw e_5.error; }
1430
+ }
1431
+ // 帧超出
1432
+ if (!pen.frames[frameIndex]) {
1433
+ return true;
1434
+ }
1435
+ pen.calculative.frameDuration = pen.frames[frameIndex].duration;
1436
+ pen.calculative.frameStart =
1437
+ pen.calculative.start + pen.calculative.duration * (cycleIndex - 1);
1438
+ pen.calculative.frameEnd =
1439
+ pen.calculative.frameStart + pen.calculative.frameDuration;
1440
+ // 换帧
1441
+ var frameChanged = frameIndex !== pen.calculative.frameIndex;
1442
+ // 新循环播放
1443
+ var cycleChanged = cycleIndex > pen.calculative.cycleIndex;
1444
+ frameChanged && (pen.calculative.frameIndex = frameIndex);
1445
+ cycleChanged && (pen.calculative.cycleIndex = cycleIndex);
1446
+ if (frameChanged || cycleChanged) {
1447
+ // 以初始位置为参考点。因为网页在后台时,不执行动画帧,网页恢复显示时,位置不确定
1448
+ pen.calculative.x = pen.calculative.initRect.x;
1449
+ pen.calculative.y = pen.calculative.initRect.y;
1450
+ pen.calculative.rotate = pen.calculative.initRect.rotate || 0;
1451
+ if (frameIndex > 0) {
1452
+ pen.prevFrame = {};
1453
+ var prevFrame = pen.frames[frameIndex - 1];
1454
+ for (var k in prevFrame) {
1455
+ pen.prevFrame[k] = prevFrame[k];
1456
+ }
1457
+ Object.assign(pen.prevFrame, {
1458
+ rotate: prevFrame.rotate || 0,
1459
+ x: prevFrame.x || 0,
1460
+ y: prevFrame.y || 0,
1461
+ scale: prevFrame.scale || 1,
1462
+ });
1463
+ }
1464
+ else {
1465
+ initPrevFrame(pen);
1466
+ }
1467
+ }
1468
+ }
1469
+ var process = ((now - pen.calculative.frameStart) / pen.calculative.frameDuration) % 1;
1470
+ setNodeAnimateProcess(pen, process);
1471
+ return true;
1472
+ }
1473
+ // 把前一个动画帧初始化为播放前状态
1474
+ export function initPrevFrame(pen) {
1475
+ pen.prevFrame = {};
1476
+ for (var k in pen) {
1477
+ if (typeof pen[k] !== 'object' || k === 'lineDash') {
1478
+ pen.prevFrame[k] = pen[k];
1479
+ }
1480
+ }
1481
+ pen.prevFrame.rotate = 0;
1482
+ pen.prevFrame.x = 0;
1483
+ pen.prevFrame.y = 0;
1484
+ pen.prevFrame.scale = 1;
1485
+ }
1486
+ // 根据process进度值(纯小数),计算节点动画属性
1487
+ export function setNodeAnimateProcess(pen, process) {
1488
+ var _a;
1489
+ if (process < 0) {
1490
+ return;
1491
+ }
1492
+ if (process > 1) {
1493
+ process = 1;
1494
+ }
1495
+ var frame = pen.frames[pen.calculative.frameIndex];
1496
+ for (var k in frame) {
1497
+ if (k === 'duration') {
1498
+ continue;
1499
+ }
1500
+ else if (k === 'scale') {
1501
+ pen.calculative.worldRect = deepClone(pen.calculative.initRect);
1502
+ scaleRect(pen.calculative.worldRect, pen.prevFrame.scale, pen.calculative.worldRect.center);
1503
+ var newScale = pen.prevFrame.scale + (frame[k] - pen.prevFrame.scale) * process;
1504
+ scaleRect(pen.calculative.worldRect, newScale / pen.prevFrame.scale, pen.calculative.worldRect.center);
1505
+ pen.calculative.patchFlags = true;
1506
+ }
1507
+ else if (k === 'x') {
1508
+ var lastVal = getFrameValue(pen, k, pen.calculative.frameIndex);
1509
+ pen.calculative.worldRect.x = pen.calculative.initRect.x + lastVal;
1510
+ pen.calculative.worldRect.ex = pen.calculative.initRect.ex + lastVal;
1511
+ translateRect(pen.calculative.worldRect, frame[k] * process * pen.calculative.canvas.store.data.scale, 0);
1512
+ pen.calculative.patchFlags = true;
1513
+ }
1514
+ else if (k === 'y') {
1515
+ var lastVal = getFrameValue(pen, k, pen.calculative.frameIndex);
1516
+ pen.calculative.worldRect.y = pen.calculative.initRect.y + lastVal;
1517
+ pen.calculative.worldRect.ey = pen.calculative.initRect.ey + lastVal;
1518
+ translateRect(pen.calculative.worldRect, 0, frame[k] * process * pen.calculative.canvas.store.data.scale);
1519
+ pen.calculative.patchFlags = true;
1520
+ }
1521
+ else if (k === 'rotate') {
1522
+ if (pen.prevFrame[k] >= 360) {
1523
+ pen.prevFrame[k] %= 360;
1524
+ }
1525
+ var lastVal = getFrameValue(pen, k, pen.calculative.frameIndex);
1526
+ var offsetRotate = ((pen.calculative.initRect.rotate + lastVal + frame[k] * process) %
1527
+ 360) -
1528
+ pen.calculative.rotate;
1529
+ if ((_a = pen.children) === null || _a === void 0 ? void 0 : _a.length) {
1530
+ rotatePen(pen, offsetRotate, pen.calculative.initRect);
1531
+ }
1532
+ else {
1533
+ pen.calculative.rotate =
1534
+ (pen.calculative.initRect.rotate + lastVal + frame[k] * process) %
1535
+ 360;
1536
+ }
1537
+ pen.calculative.patchFlags = true;
1538
+ }
1539
+ else if (k === 'image') {
1540
+ pen.image = frame['image'];
1541
+ pen.calculative.image = undefined;
1542
+ pen.calculative.canvas.loadImage(pen);
1543
+ if (pen.isBottom) {
1544
+ pen.calculative.canvas.canvasImageBottom.init();
1545
+ }
1546
+ else {
1547
+ pen.calculative.canvas.canvasImage.init();
1548
+ }
1549
+ }
1550
+ else if (isLinear(frame[k], k, pen)) {
1551
+ if (pen.prevFrame[k] == null) {
1552
+ if (k === 'globalAlpha') {
1553
+ pen.prevFrame[k] = 1;
1554
+ }
1555
+ else {
1556
+ pen.prevFrame[k] = 0;
1557
+ }
1558
+ }
1559
+ var current = pen.prevFrame[k] + (frame[k] - pen.prevFrame[k]) * process;
1560
+ pen.calculative[k] = Math.round(current * 100) / 100;
1561
+ }
1562
+ else {
1563
+ pen.calculative[k] = frame[k];
1564
+ var v = {};
1565
+ v[k] = frame[k];
1566
+ setChildValue(pen, v);
1567
+ }
1568
+ if (k === 'text') {
1569
+ calcTextLines(pen);
1570
+ }
1571
+ }
1572
+ }
1573
+ /**
1574
+ * 值类型为 number , pen.linear 为 false 时,且 key 不属于 noLinear 时,返回 true
1575
+ * @param value 值
1576
+ * @param key 键值
1577
+ * @param pen 画笔
1578
+ * @returns
1579
+ */
1580
+ function isLinear(value, key, pen) {
1581
+ // 不线性变化的属性
1582
+ var noLinear = ['strokeType', 'bkType', 'showChild'];
1583
+ return (typeof value === 'number' &&
1584
+ pen.linear !== false &&
1585
+ !noLinear.includes(key));
1586
+ }
1587
+ export function setLineAnimate(pen, now) {
1588
+ if (pen.calculative.start === 0) {
1589
+ pen.calculative.start = undefined;
1590
+ return 0;
1591
+ }
1592
+ if (!pen.animateCycle) {
1593
+ pen.animateCycle = Infinity;
1594
+ }
1595
+ if (!pen.animateSpan) {
1596
+ pen.animateSpan = 1;
1597
+ }
1598
+ pen.calculative.animatePos +=
1599
+ pen.animateSpan * (pen.calculative.canvas.store.data.scale || 1);
1600
+ if (!pen.calculative.start) {
1601
+ pen.calculative.start = Date.now();
1602
+ pen.calculative.animatePos =
1603
+ pen.animateSpan * (pen.calculative.canvas.store.data.scale || 1);
1604
+ pen.calculative.cycleIndex = 1;
1605
+ }
1606
+ else if (pen.calculative.animatePos > pen.length) {
1607
+ // 播放到尾了
1608
+ ++pen.calculative.cycleIndex;
1609
+ // 播放结束
1610
+ if (pen.calculative.cycleIndex > pen.animateCycle) {
1611
+ pen.calculative.start = undefined;
1612
+ return 0;
1613
+ }
1614
+ pen.calculative.animatePos = pen.animateSpan;
1615
+ }
1616
+ return true;
1617
+ }
1618
+ export function setChildrenActive(pen, active) {
1619
+ if (active === void 0) { active = true; }
1620
+ if (!pen.children) {
1621
+ return;
1622
+ }
1623
+ var store = pen.calculative.canvas.store;
1624
+ pen.children.forEach(function (id) {
1625
+ var child = store.pens[id];
1626
+ if (child) {
1627
+ child.calculative.active = active;
1628
+ setChildrenActive(child, active);
1629
+ }
1630
+ });
1631
+ }
1632
+ export function setHover(pen, hover) {
1633
+ if (hover === void 0) { hover = true; }
1634
+ if (!pen) {
1635
+ return;
1636
+ }
1637
+ var store = pen.calculative.canvas.store;
1638
+ pen.calculative.hover = hover;
1639
+ if (pen.children) {
1640
+ pen.children.forEach(function (id) {
1641
+ var _a, _b;
1642
+ // 子节点没有自己的独立hover,继承父节点hover
1643
+ if (((_a = store.pens[id]) === null || _a === void 0 ? void 0 : _a.hoverColor) == undefined &&
1644
+ ((_b = store.pens[id]) === null || _b === void 0 ? void 0 : _b.hoverBackground) == undefined) {
1645
+ setHover(store.pens[id], hover);
1646
+ }
1647
+ });
1648
+ }
1649
+ }
1650
+ export function setElemPosition(pen, elem) {
1651
+ if (!elem) {
1652
+ return;
1653
+ }
1654
+ var store = pen.calculative.canvas.store;
1655
+ var worldRect = pen.calculative.worldRect;
1656
+ elem.style.position = 'absolute';
1657
+ elem.style.outline = 'none';
1658
+ elem.style.left = worldRect.x + store.data.x + 'px';
1659
+ elem.style.top = worldRect.y + store.data.y + 'px';
1660
+ elem.style.width = worldRect.width + 'px';
1661
+ elem.style.height = worldRect.height + 'px';
1662
+ elem.style.display = pen.calculative.inView != false ? 'inline' : 'none'; // 是否隐藏元素
1663
+ !pen.calculative.rotate && (pen.calculative.rotate = 0);
1664
+ elem.style.transform = "rotate(" + pen.calculative.rotate + "deg)";
1665
+ if (!pen.calculative.rotate) {
1666
+ if (pen.calculative.flipX) {
1667
+ elem.style.transform = "rotateY(180deg)";
1668
+ }
1669
+ if (pen.calculative.flipY) {
1670
+ elem.style.transform = "rotateX(180deg)";
1671
+ }
1672
+ if (pen.calculative.flipX && pen.calculative.flipY) {
1673
+ elem.style.transform = "rotateZ(180deg)";
1674
+ }
1675
+ }
1676
+ elem.style.zIndex = pen.calculative.zIndex + '';
1677
+ if (pen.locked === LockState.DisableEdit ||
1678
+ pen.locked === LockState.DisableMove ||
1679
+ store.data.locked) {
1680
+ // gif 组合后,作为子节点可通过 lockedOnCombine 来决定自身的 locked 状态
1681
+ elem.style.userSelect = 'initial';
1682
+ elem.style.pointerEvents = 'initial';
1683
+ if (pen.name === 'gif') {
1684
+ elem.style.userSelect = 'none';
1685
+ elem.style.pointerEvents = 'none';
1686
+ }
1687
+ }
1688
+ else {
1689
+ // pen.locked LockState.Disable 不响应鼠标
1690
+ elem.style.userSelect = 'none';
1691
+ elem.style.pointerEvents = 'none';
1692
+ }
1693
+ }
1694
+ /**
1695
+ * 每个画笔 locked
1696
+ * @param pens 画笔
1697
+ * @returns
1698
+ */
1699
+ export function getPensLock(pens) {
1700
+ return pens.every(function (pen) { return pen.locked; });
1701
+ }
1702
+ /**
1703
+ * 画笔们的 disabledRotate = true
1704
+ * 即 全部禁止旋转 返回 true
1705
+ * @param pens 画笔
1706
+ * @returns
1707
+ */
1708
+ export function getPensDisableRotate(pens) {
1709
+ return pens.every(function (pen) { return pen.disableRotate; });
1710
+ }
1711
+ export function rotatePen(pen, angle, rect) {
1712
+ var _a;
1713
+ if (pen.type) {
1714
+ pen.calculative.worldAnchors.forEach(function (anchor) {
1715
+ rotatePoint(anchor, angle, rect.center);
1716
+ });
1717
+ initLineRect(pen);
1718
+ calcPenRect(pen);
1719
+ }
1720
+ else {
1721
+ if (pen.calculative.rotate) {
1722
+ pen.calculative.rotate += angle;
1723
+ }
1724
+ else {
1725
+ pen.calculative.rotate = angle;
1726
+ }
1727
+ rotatePoint(pen.calculative.worldRect.center, angle, rect.center);
1728
+ if (pen.parentId) {
1729
+ pen.calculative.worldRect.x =
1730
+ pen.calculative.worldRect.center.x -
1731
+ pen.calculative.worldRect.width / 2;
1732
+ pen.calculative.worldRect.y =
1733
+ pen.calculative.worldRect.center.y -
1734
+ pen.calculative.worldRect.height / 2;
1735
+ pen.x = (pen.calculative.worldRect.x - rect.x) / rect.width;
1736
+ pen.y = (pen.calculative.worldRect.y - rect.y) / rect.height;
1737
+ }
1738
+ }
1739
+ (_a = pen.children) === null || _a === void 0 ? void 0 : _a.forEach(function (id) {
1740
+ var child = pen.calculative.canvas.store.pens[id];
1741
+ rotatePen(child, angle, rect);
1742
+ });
1743
+ }
1744
+ function initLineRect(pen) {
1745
+ var _a;
1746
+ if (!((_a = pen.calculative.worldAnchors) === null || _a === void 0 ? void 0 : _a.length)) {
1747
+ return;
1748
+ }
1749
+ if (!isFinite(pen.x) || !isFinite(pen.x)) {
1750
+ return;
1751
+ }
1752
+ if (pen.x == null || pen.y == null) {
1753
+ return;
1754
+ }
1755
+ var rect = getLineRect(pen);
1756
+ if (!pen.parentId) {
1757
+ Object.assign(pen, rect);
1758
+ }
1759
+ var _b = pen.calculative.canvas.store.options, fontSize = _b.fontSize, lineHeight = _b.lineHeight;
1760
+ if (!pen.fontSize) {
1761
+ pen.fontSize = fontSize;
1762
+ pen.calculative.fontSize =
1763
+ pen.fontSize * pen.calculative.canvas.store.data.scale;
1764
+ }
1765
+ if (!pen.lineHeight) {
1766
+ pen.lineHeight = lineHeight;
1767
+ pen.calculative.lineHeight = pen.lineHeight;
1768
+ }
1769
+ calcCenter(rect);
1770
+ pen.calculative.worldRect = rect;
1771
+ calcPadding(pen, rect);
1772
+ calcTextRect(pen);
1773
+ if (pen.calculative.worldAnchors) {
1774
+ pen.anchors = pen.calculative.worldAnchors.map(function (pt) {
1775
+ return calcRelativePoint(pt, pen.calculative.worldRect);
1776
+ });
1777
+ }
1778
+ }
1779
+ /**
1780
+ * 画笔们的 disableSize = true
1781
+ * 即 全部不允许改变大小 返回 true
1782
+ * @param pens 画笔
1783
+ * @returns
1784
+ */
1785
+ export function getPensDisableResize(pens) {
1786
+ return pens.every(function (pen) { return pen.disableSize; });
1787
+ }
1788
+ export function getFrameValue(pen, prop, frameIndex) {
1789
+ if (!pen.frames || !prop) {
1790
+ return 0;
1791
+ }
1792
+ var v = 0;
1793
+ for (var i = 0; i < frameIndex; i++) {
1794
+ if (pen.frames[i]) {
1795
+ v += pen.frames[i][prop] || 0;
1796
+ }
1797
+ }
1798
+ return v;
1799
+ }
1800
+ /**
1801
+ * 判断该画笔 是否是组合为状态中 展示的画笔
1802
+ */
1803
+ export function isShowChild(pen, store) {
1804
+ var _a;
1805
+ var selfPen = pen;
1806
+ while (selfPen && selfPen.parentId) {
1807
+ var oldPen = selfPen;
1808
+ selfPen = store.pens[selfPen.parentId];
1809
+ var showChildIndex = (_a = selfPen === null || selfPen === void 0 ? void 0 : selfPen.calculative) === null || _a === void 0 ? void 0 : _a.showChild;
1810
+ if (showChildIndex != undefined) {
1811
+ var showChildId = selfPen.children[showChildIndex];
1812
+ if (showChildId !== oldPen.id) {
1813
+ // toPng 不展示它
1814
+ return false;
1815
+ }
1816
+ }
1817
+ }
1818
+ return true;
1819
+ }
1820
+ /**
1821
+ * 计算画笔的 inView
1822
+ * @param pen 画笔
1823
+ * @param calcChild 是否计算子画笔
1824
+ */
1825
+ export function calcInView(pen, calcChild) {
1826
+ var _a, _b;
1827
+ if (calcChild === void 0) { calcChild = false; }
1828
+ var _c = pen.calculative.canvas, store = _c.store, canvasRect = _c.canvasRect;
1829
+ if (calcChild) {
1830
+ (_a = pen.children) === null || _a === void 0 ? void 0 : _a.forEach(function (id) {
1831
+ var child = store.pens[id];
1832
+ child && calcInView(child, true);
1833
+ });
1834
+ }
1835
+ pen.calculative.inView = true;
1836
+ if (!isShowChild(pen, store) ||
1837
+ pen.visible == false ||
1838
+ pen.calculative.visible == false) {
1839
+ pen.calculative.inView = false;
1840
+ }
1841
+ else {
1842
+ var _d = pen.calculative.worldRect, x = _d.x, y = _d.y, width = _d.width, height = _d.height, rotate = _d.rotate;
1843
+ var penRect = {
1844
+ x: x + store.data.x,
1845
+ y: y + store.data.y,
1846
+ width: width,
1847
+ height: height,
1848
+ rotate: rotate,
1849
+ };
1850
+ calcRightBottom(penRect);
1851
+ if (!rectInRect(penRect, canvasRect)) {
1852
+ pen.calculative.inView = false;
1853
+ }
1854
+ }
1855
+ // TODO: 语义化上,用 onValue 更合适,但 onValue 会触发 echarts 图形的重绘,没有必要
1856
+ // 更改 view 后,修改 dom 节点的显示隐藏
1857
+ (_b = pen.onMove) === null || _b === void 0 ? void 0 : _b.call(pen, pen);
1858
+ }
1859
+ /**
1860
+ * 绘制 rect ,上线后可查看 rect 位置
1861
+ */
1862
+ function inspectRect(ctx, store, pen) {
1863
+ if (store.fillWorldTextRect) {
1864
+ ctx.save();
1865
+ ctx.fillStyle = '#c3deb7';
1866
+ var _a = pen.calculative.worldTextRect, x = _a.x, y = _a.y, width = _a.width, height = _a.height;
1867
+ ctx.fillRect(x, y, width, height);
1868
+ ctx.restore();
1869
+ }
1870
+ }
1871
+ export function setGlobalAlpha(ctx, pen) {
1872
+ var globalAlpha = pen.calculative.globalAlpha;
1873
+ if (globalAlpha < 1) {
1874
+ ctx.globalAlpha = globalAlpha;
1875
+ }
1876
+ }
1877
+ /**
1878
+ * ctx 绘制图纸,并非 Path2D
1879
+ * @param ctx 画布上下文
1880
+ * @param pen 画笔
1881
+ */
1882
+ function ctxDrawCanvas(ctx, pen) {
1883
+ var canvasDraw = globalStore.canvasDraws[pen.name];
1884
+ if (canvasDraw) {
1885
+ // TODO: 后续考虑优化 save / restore
1886
+ ctx.save();
1887
+ // TODO: 原有 return 终止后续操作,必要性不大
1888
+ canvasDraw(ctx, pen);
1889
+ ctx.restore();
1890
+ }
1891
+ }
1892
+ export function setChildValue(pen, data) {
1893
+ for (var k in data) {
1894
+ if (inheritanceProps.includes(k)) {
1895
+ pen[k] = data[k];
1896
+ pen.calculative[k] = data[k];
1897
+ }
1898
+ if (pen.calculative.canvas.parent.isCombine(pen) &&
1899
+ pen.showChild === undefined) {
1900
+ var children = pen.children;
1901
+ children === null || children === void 0 ? void 0 : children.forEach(function (childId) {
1902
+ var child = pen.calculative.canvas.store.pens[childId];
1903
+ setChildValue(child, data);
1904
+ });
1905
+ }
1906
+ }
1907
+ }
1908
+ //# sourceMappingURL=render.js.map