@meta2d/core 1.0.55 → 1.0.56

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 (302) hide show
  1. package/README.md +13 -0
  2. package/package.build.json +39 -0
  3. package/package.json +38 -38
  4. package/src/canvas/canvas.ts +8639 -0
  5. package/src/canvas/canvasImage.ts +525 -0
  6. package/src/canvas/canvasTemplate.ts +257 -0
  7. package/src/canvas/magnifierCanvas.ts +142 -0
  8. package/src/canvas/offscreen.ts +14 -0
  9. package/src/core.ts +5128 -0
  10. package/src/data.ts +86 -0
  11. package/src/diagrams/arrow.ts +50 -0
  12. package/src/diagrams/circle.ts +19 -0
  13. package/src/diagrams/cloud.ts +34 -0
  14. package/src/diagrams/cube.ts +94 -0
  15. package/src/diagrams/diamond.ts +14 -0
  16. package/src/diagrams/file.ts +19 -0
  17. package/src/diagrams/gif.ts +105 -0
  18. package/src/diagrams/{hexagon.js → hexagon.ts} +19 -14
  19. package/src/diagrams/iframe.ts +365 -0
  20. package/src/diagrams/{index.js → index.ts} +36 -34
  21. package/src/diagrams/line/arrow.ts +175 -0
  22. package/src/diagrams/line/curve.ts +260 -0
  23. package/src/diagrams/line/line.ts +409 -0
  24. package/src/diagrams/line/polyline.ts +676 -0
  25. package/src/diagrams/line/smooth.ts +133 -0
  26. package/src/diagrams/message.ts +17 -0
  27. package/src/diagrams/mindLine.ts +31 -0
  28. package/src/diagrams/mindNode.ts +177 -0
  29. package/src/diagrams/panel.ts +149 -0
  30. package/src/diagrams/pentagon.ts +48 -0
  31. package/src/diagrams/pentagram.ts +63 -0
  32. package/src/diagrams/people.ts +23 -0
  33. package/src/diagrams/rectangle.ts +29 -0
  34. package/src/diagrams/svg/parse.ts +319 -0
  35. package/src/diagrams/svgPath.ts +53 -0
  36. package/src/diagrams/triangle.ts +43 -0
  37. package/src/diagrams/video.ts +202 -0
  38. package/src/dialog/dialog.ts +177 -0
  39. package/src/event/event.ts +142 -0
  40. package/src/map/map.ts +239 -0
  41. package/src/options.ts +205 -0
  42. package/src/pen/arrow.ts +259 -0
  43. package/src/pen/math.ts +253 -0
  44. package/src/pen/model.ts +785 -0
  45. package/src/pen/plugin.ts +57 -0
  46. package/src/pen/render.ts +3725 -0
  47. package/src/pen/text.ts +341 -0
  48. package/src/pen/utils.ts +21 -0
  49. package/src/point/point.ts +232 -0
  50. package/src/rect/rect.ts +507 -0
  51. package/src/rect/triangle.ts +16 -0
  52. package/src/scroll/scroll.ts +277 -0
  53. package/src/store/global.ts +38 -0
  54. package/src/store/store.ts +293 -0
  55. package/src/theme.ts +35 -0
  56. package/src/title/title.ts +115 -0
  57. package/src/tooltip/tooltip.ts +199 -0
  58. package/src/utils/clone.ts +79 -0
  59. package/src/utils/color.ts +126 -0
  60. package/src/utils/error.ts +7 -0
  61. package/src/utils/file.ts +46 -0
  62. package/src/utils/{index.d.ts → index.ts} +1 -0
  63. package/src/utils/math.ts +120 -0
  64. package/src/utils/object.ts +23 -0
  65. package/src/utils/padding.ts +48 -0
  66. package/src/utils/time.ts +25 -0
  67. package/src/utils/url.ts +30 -0
  68. package/src/utils/uuid.ts +15 -0
  69. package/index.js +0 -10
  70. package/index.js.map +0 -1
  71. package/src/canvas/canvas.d.ts +0 -456
  72. package/src/canvas/canvas.js +0 -8187
  73. package/src/canvas/canvas.js.map +0 -1
  74. package/src/canvas/canvasImage.d.ts +0 -28
  75. package/src/canvas/canvasImage.js +0 -503
  76. package/src/canvas/canvasImage.js.map +0 -1
  77. package/src/canvas/canvasTemplate.d.ts +0 -19
  78. package/src/canvas/canvasTemplate.js +0 -229
  79. package/src/canvas/canvasTemplate.js.map +0 -1
  80. package/src/canvas/index.js +0 -3
  81. package/src/canvas/index.js.map +0 -1
  82. package/src/canvas/magnifierCanvas.d.ts +0 -20
  83. package/src/canvas/magnifierCanvas.js +0 -101
  84. package/src/canvas/magnifierCanvas.js.map +0 -1
  85. package/src/canvas/offscreen.d.ts +0 -2
  86. package/src/canvas/offscreen.js +0 -14
  87. package/src/canvas/offscreen.js.map +0 -1
  88. package/src/core.d.ts +0 -479
  89. package/src/core.js +0 -5199
  90. package/src/core.js.map +0 -1
  91. package/src/data.d.ts +0 -34
  92. package/src/data.js +0 -85
  93. package/src/data.js.map +0 -1
  94. package/src/diagrams/arrow.d.ts +0 -4
  95. package/src/diagrams/arrow.js +0 -47
  96. package/src/diagrams/arrow.js.map +0 -1
  97. package/src/diagrams/circle.d.ts +0 -2
  98. package/src/diagrams/circle.js +0 -9
  99. package/src/diagrams/circle.js.map +0 -1
  100. package/src/diagrams/cloud.d.ts +0 -2
  101. package/src/diagrams/cloud.js +0 -12
  102. package/src/diagrams/cloud.js.map +0 -1
  103. package/src/diagrams/cube.d.ts +0 -2
  104. package/src/diagrams/cube.js +0 -70
  105. package/src/diagrams/cube.js.map +0 -1
  106. package/src/diagrams/diamond.d.ts +0 -2
  107. package/src/diagrams/diamond.js +0 -13
  108. package/src/diagrams/diamond.js.map +0 -1
  109. package/src/diagrams/file.d.ts +0 -2
  110. package/src/diagrams/file.js +0 -18
  111. package/src/diagrams/file.js.map +0 -1
  112. package/src/diagrams/gif.d.ts +0 -5
  113. package/src/diagrams/gif.js +0 -90
  114. package/src/diagrams/gif.js.map +0 -1
  115. package/src/diagrams/hexagon.d.ts +0 -2
  116. package/src/diagrams/hexagon.js.map +0 -1
  117. package/src/diagrams/iframe.d.ts +0 -2
  118. package/src/diagrams/iframe.js +0 -356
  119. package/src/diagrams/iframe.js.map +0 -1
  120. package/src/diagrams/index.d.ts +0 -71
  121. package/src/diagrams/index.js.map +0 -1
  122. package/src/diagrams/line/arrow.d.ts +0 -2
  123. package/src/diagrams/line/arrow.js +0 -128
  124. package/src/diagrams/line/arrow.js.map +0 -1
  125. package/src/diagrams/line/curve.d.ts +0 -16
  126. package/src/diagrams/line/curve.js +0 -236
  127. package/src/diagrams/line/curve.js.map +0 -1
  128. package/src/diagrams/line/index.js +0 -6
  129. package/src/diagrams/line/index.js.map +0 -1
  130. package/src/diagrams/line/line.d.ts +0 -42
  131. package/src/diagrams/line/line.js +0 -431
  132. package/src/diagrams/line/line.js.map +0 -1
  133. package/src/diagrams/line/polyline.d.ts +0 -10
  134. package/src/diagrams/line/polyline.js +0 -657
  135. package/src/diagrams/line/polyline.js.map +0 -1
  136. package/src/diagrams/line/smooth.d.ts +0 -3
  137. package/src/diagrams/line/smooth.js +0 -174
  138. package/src/diagrams/line/smooth.js.map +0 -1
  139. package/src/diagrams/message.d.ts +0 -2
  140. package/src/diagrams/message.js +0 -15
  141. package/src/diagrams/message.js.map +0 -1
  142. package/src/diagrams/mindLine.d.ts +0 -3
  143. package/src/diagrams/mindLine.js +0 -31
  144. package/src/diagrams/mindLine.js.map +0 -1
  145. package/src/diagrams/mindNode.d.ts +0 -3
  146. package/src/diagrams/mindNode.js +0 -189
  147. package/src/diagrams/mindNode.js.map +0 -1
  148. package/src/diagrams/panel.d.ts +0 -2
  149. package/src/diagrams/panel.js +0 -131
  150. package/src/diagrams/panel.js.map +0 -1
  151. package/src/diagrams/pentagon.d.ts +0 -3
  152. package/src/diagrams/pentagon.js +0 -46
  153. package/src/diagrams/pentagon.js.map +0 -1
  154. package/src/diagrams/pentagram.d.ts +0 -3
  155. package/src/diagrams/pentagram.js +0 -77
  156. package/src/diagrams/pentagram.js.map +0 -1
  157. package/src/diagrams/people.d.ts +0 -2
  158. package/src/diagrams/people.js +0 -19
  159. package/src/diagrams/people.js.map +0 -1
  160. package/src/diagrams/rectangle.d.ts +0 -3
  161. package/src/diagrams/rectangle.js +0 -26
  162. package/src/diagrams/rectangle.js.map +0 -1
  163. package/src/diagrams/svg/parse.d.ts +0 -15
  164. package/src/diagrams/svg/parse.js +0 -326
  165. package/src/diagrams/svg/parse.js.map +0 -1
  166. package/src/diagrams/svgPath.d.ts +0 -2
  167. package/src/diagrams/svgPath.js +0 -30
  168. package/src/diagrams/svgPath.js.map +0 -1
  169. package/src/diagrams/triangle.d.ts +0 -3
  170. package/src/diagrams/triangle.js +0 -41
  171. package/src/diagrams/triangle.js.map +0 -1
  172. package/src/diagrams/video.d.ts +0 -5
  173. package/src/diagrams/video.js +0 -185
  174. package/src/diagrams/video.js.map +0 -1
  175. package/src/dialog/dialog.d.ts +0 -21
  176. package/src/dialog/dialog.js +0 -98
  177. package/src/dialog/dialog.js.map +0 -1
  178. package/src/dialog/index.js +0 -2
  179. package/src/dialog/index.js.map +0 -1
  180. package/src/event/event.d.ts +0 -102
  181. package/src/event/event.js +0 -22
  182. package/src/event/event.js.map +0 -1
  183. package/src/event/index.js +0 -2
  184. package/src/event/index.js.map +0 -1
  185. package/src/map/index.js +0 -2
  186. package/src/map/index.js.map +0 -1
  187. package/src/map/map.d.ts +0 -21
  188. package/src/map/map.js +0 -210
  189. package/src/map/map.js.map +0 -1
  190. package/src/options.d.ts +0 -130
  191. package/src/options.js +0 -80
  192. package/src/options.js.map +0 -1
  193. package/src/pen/arrow.d.ts +0 -4
  194. package/src/pen/arrow.js +0 -188
  195. package/src/pen/arrow.js.map +0 -1
  196. package/src/pen/index.js +0 -7
  197. package/src/pen/index.js.map +0 -1
  198. package/src/pen/math.d.ts +0 -28
  199. package/src/pen/math.js +0 -304
  200. package/src/pen/math.js.map +0 -1
  201. package/src/pen/model.d.ts +0 -514
  202. package/src/pen/model.js +0 -210
  203. package/src/pen/model.js.map +0 -1
  204. package/src/pen/plugin.d.ts +0 -5
  205. package/src/pen/plugin.js +0 -88
  206. package/src/pen/plugin.js.map +0 -1
  207. package/src/pen/render.d.ts +0 -147
  208. package/src/pen/render.js +0 -3236
  209. package/src/pen/render.js.map +0 -1
  210. package/src/pen/text.d.ts +0 -8
  211. package/src/pen/text.js +0 -375
  212. package/src/pen/text.js.map +0 -1
  213. package/src/pen/utils.d.ts +0 -2
  214. package/src/pen/utils.js +0 -41
  215. package/src/pen/utils.js.map +0 -1
  216. package/src/point/index.js +0 -2
  217. package/src/point/index.js.map +0 -1
  218. package/src/point/point.d.ts +0 -65
  219. package/src/point/point.js +0 -179
  220. package/src/point/point.js.map +0 -1
  221. package/src/rect/index.js +0 -2
  222. package/src/rect/index.js.map +0 -1
  223. package/src/rect/rect.d.ts +0 -52
  224. package/src/rect/rect.js +0 -486
  225. package/src/rect/rect.js.map +0 -1
  226. package/src/rect/triangle.d.ts +0 -2
  227. package/src/rect/triangle.js +0 -10
  228. package/src/rect/triangle.js.map +0 -1
  229. package/src/scroll/index.js +0 -2
  230. package/src/scroll/index.js.map +0 -1
  231. package/src/scroll/scroll.d.ts +0 -35
  232. package/src/scroll/scroll.js +0 -221
  233. package/src/scroll/scroll.js.map +0 -1
  234. package/src/store/global.d.ts +0 -25
  235. package/src/store/global.js +0 -18
  236. package/src/store/global.js.map +0 -1
  237. package/src/store/index.js +0 -3
  238. package/src/store/index.js.map +0 -1
  239. package/src/store/store.d.ts +0 -228
  240. package/src/store/store.js +0 -121
  241. package/src/store/store.js.map +0 -1
  242. package/src/theme.d.ts +0 -13
  243. package/src/theme.js +0 -23
  244. package/src/theme.js.map +0 -1
  245. package/src/title/index.js +0 -2
  246. package/src/title/index.js.map +0 -1
  247. package/src/title/title.d.ts +0 -30
  248. package/src/title/title.js +0 -99
  249. package/src/title/title.js.map +0 -1
  250. package/src/tooltip/index.js +0 -2
  251. package/src/tooltip/index.js.map +0 -1
  252. package/src/tooltip/tooltip.d.ts +0 -40
  253. package/src/tooltip/tooltip.js +0 -167
  254. package/src/tooltip/tooltip.js.map +0 -1
  255. package/src/utils/browser.d.ts +0 -1
  256. package/src/utils/browser.js +0 -4
  257. package/src/utils/browser.js.map +0 -1
  258. package/src/utils/clone.d.ts +0 -8
  259. package/src/utils/clone.js +0 -88
  260. package/src/utils/clone.js.map +0 -1
  261. package/src/utils/color.d.ts +0 -3
  262. package/src/utils/color.js +0 -126
  263. package/src/utils/color.js.map +0 -1
  264. package/src/utils/error.d.ts +0 -2
  265. package/src/utils/error.js +0 -6
  266. package/src/utils/error.js.map +0 -1
  267. package/src/utils/file.d.ts +0 -3
  268. package/src/utils/file.js +0 -92
  269. package/src/utils/file.js.map +0 -1
  270. package/src/utils/index.js +0 -9
  271. package/src/utils/index.js.map +0 -1
  272. package/src/utils/math.d.ts +0 -18
  273. package/src/utils/math.js +0 -152
  274. package/src/utils/math.js.map +0 -1
  275. package/src/utils/object.d.ts +0 -2
  276. package/src/utils/object.js +0 -21
  277. package/src/utils/object.js.map +0 -1
  278. package/src/utils/padding.d.ts +0 -7
  279. package/src/utils/padding.js +0 -47
  280. package/src/utils/padding.js.map +0 -1
  281. package/src/utils/time.d.ts +0 -1
  282. package/src/utils/time.js +0 -17
  283. package/src/utils/time.js.map +0 -1
  284. package/src/utils/url.d.ts +0 -4
  285. package/src/utils/url.js +0 -27
  286. package/src/utils/url.js.map +0 -1
  287. package/src/utils/uuid.d.ts +0 -4
  288. package/src/utils/uuid.js +0 -13
  289. package/src/utils/uuid.js.map +0 -1
  290. /package/{index.d.ts → index.ts} +0 -0
  291. /package/src/canvas/{index.d.ts → index.ts} +0 -0
  292. /package/src/diagrams/line/{index.d.ts → index.ts} +0 -0
  293. /package/src/dialog/{index.d.ts → index.ts} +0 -0
  294. /package/src/event/{index.d.ts → index.ts} +0 -0
  295. /package/src/map/{index.d.ts → index.ts} +0 -0
  296. /package/src/pen/{index.d.ts → index.ts} +0 -0
  297. /package/src/point/{index.d.ts → index.ts} +0 -0
  298. /package/src/rect/{index.d.ts → index.ts} +0 -0
  299. /package/src/scroll/{index.d.ts → index.ts} +0 -0
  300. /package/src/store/{index.d.ts → index.ts} +0 -0
  301. /package/src/title/{index.d.ts → index.ts} +0 -0
  302. /package/src/tooltip/{index.d.ts → index.ts} +0 -0
package/src/pen/render.js DELETED
@@ -1,3236 +0,0 @@
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 { CanvasLayer, LineAnimateType, LockState, } from './model';
49
- import { drawArrow, 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, calcPivot } 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 { pSBC, rgba } from '../utils';
59
- /**
60
- * ancestor 是否是 pen 的祖先
61
- * @param pen 当前画笔
62
- * @param ancestor 祖先画笔
63
- */
64
- export function isAncestor(pen, ancestor) {
65
- if (!pen || !ancestor) {
66
- return false;
67
- }
68
- var parent = getParent(pen);
69
- while (parent) {
70
- if (parent.id === ancestor.id) {
71
- return true;
72
- }
73
- parent = getParent(parent);
74
- }
75
- return false;
76
- }
77
- export function getParent(pen, root) {
78
- if (!pen || !pen.parentId || !pen.calculative) {
79
- return undefined;
80
- }
81
- var store = pen.calculative.canvas.store;
82
- var parent = store.pens[pen.parentId];
83
- if (!root) {
84
- return parent;
85
- }
86
- return getParent(parent, root) || parent;
87
- }
88
- export function getAllChildren(pen, store) {
89
- if (!pen || !pen.children) {
90
- return [];
91
- }
92
- var children = [];
93
- pen.children.forEach(function (id) {
94
- var child = store.pens[id];
95
- if (child) {
96
- children.push(child);
97
- children.push.apply(children, __spreadArray([], __read(getAllChildren(child, store)), false));
98
- }
99
- });
100
- return children;
101
- }
102
- export function getAllFollowers(pen, store) {
103
- if (!pen || !pen.followers) {
104
- return [];
105
- }
106
- var followers = [];
107
- pen.followers.forEach(function (id) {
108
- var follower = store.pens[id];
109
- if (follower && !follower.parentId) {
110
- followers.push(follower);
111
- followers.push.apply(followers, __spreadArray([], __read(getAllFollowers(follower, store)), false));
112
- }
113
- });
114
- return followers;
115
- }
116
- function drawBkLinearGradient(ctx, pen) {
117
- var _a = pen.calculative, worldRect = _a.worldRect, gradientFromColor = _a.gradientFromColor, gradientToColor = _a.gradientToColor, gradientAngle = _a.gradientAngle;
118
- return linearGradient(ctx, worldRect, gradientFromColor, gradientToColor, gradientAngle);
119
- }
120
- /**
121
- * 避免副作用,把创建好后的径向渐变对象返回出来
122
- * @param ctx 画布绘制对象
123
- * @param pen 当前画笔
124
- * @returns 径向渐变
125
- */
126
- function drawBkRadialGradient(ctx, pen) {
127
- var _a = pen.calculative, worldRect = _a.worldRect, gradientFromColor = _a.gradientFromColor, gradientToColor = _a.gradientToColor, gradientRadius = _a.gradientRadius;
128
- if (!gradientFromColor || !gradientToColor) {
129
- return;
130
- }
131
- var width = worldRect.width, height = worldRect.height, center = worldRect.center;
132
- var centerX = center.x, centerY = center.y;
133
- var r = width;
134
- if (r < height) {
135
- r = height;
136
- }
137
- r *= 0.5;
138
- var grd = ctx.createRadialGradient(centerX, centerY, r * (gradientRadius || 0), centerX, centerY, r);
139
- grd.addColorStop(0, gradientFromColor);
140
- grd.addColorStop(1, gradientToColor);
141
- return grd;
142
- }
143
- function getLinearGradientPoints(x1, y1, x2, y2, r) {
144
- var slantAngle = 0;
145
- slantAngle = Math.PI / 2 - Math.atan2(y2 - y1, x2 - x1);
146
- var originX = (x1 + x2) / 2;
147
- var originY = (y1 + y2) / 2;
148
- var perpX1 = originX + r * Math.sin((90 * Math.PI) / 180 - slantAngle);
149
- var perpY1 = originY + r * -Math.cos((90 * Math.PI) / 180 - slantAngle);
150
- var perpX2 = originX + r * Math.sin((270 * Math.PI) / 180 - slantAngle);
151
- var perpY2 = originY + r * -Math.cos((270 * Math.PI) / 180 - slantAngle);
152
- return [perpX1, perpY1, perpX2, perpY2];
153
- }
154
- function getBkRadialGradient(ctx, pen) {
155
- var _a = pen.calculative, worldRect = _a.worldRect, gradientColors = _a.gradientColors, gradientRadius = _a.gradientRadius;
156
- if (!gradientColors) {
157
- return;
158
- }
159
- var width = worldRect.width, height = worldRect.height, center = worldRect.center;
160
- var centerX = center.x, centerY = center.y;
161
- var r = width;
162
- if (r < height) {
163
- r = height;
164
- }
165
- r *= 0.5;
166
- var colors = formatGradient(gradientColors).colors;
167
- var grd = ctx.createRadialGradient(centerX, centerY, r * (gradientRadius || 0), centerX, centerY, r);
168
- colors.forEach(function (stop) {
169
- grd.addColorStop(stop.i, stop.color);
170
- });
171
- return grd;
172
- }
173
- function getBkGradient(ctx, pen) {
174
- var _a = pen.calculative.worldRect, x = _a.x, y = _a.y, ex = _a.ex, width = _a.width, height = _a.height, center = _a.center;
175
- var points = [
176
- { x: ex, y: y + height / 2 },
177
- { x: x, y: y + height / 2 },
178
- ];
179
- var _b = formatGradient(pen.calculative.gradientColors), angle = _b.angle, colors = _b.colors;
180
- var r = getGradientR(angle, width, height);
181
- points.forEach(function (point) {
182
- rotatePoint(point, angle, center);
183
- });
184
- return getLinearGradient(ctx, points, colors, r);
185
- }
186
- function getTextRadialGradient(ctx, pen) {
187
- var _a = pen.calculative, worldRect = _a.worldRect, textGradientColors = _a.textGradientColors;
188
- if (!textGradientColors) {
189
- return;
190
- }
191
- var width = worldRect.width, height = worldRect.height, center = worldRect.center;
192
- var centerX = center.x, centerY = center.y;
193
- var r = width;
194
- if (r < height) {
195
- r = height;
196
- }
197
- r *= 0.5;
198
- var colors = formatGradient(textGradientColors).colors;
199
- var grd = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, r);
200
- colors.forEach(function (stop) {
201
- grd.addColorStop(stop.i, stop.color);
202
- });
203
- return grd;
204
- }
205
- function getTextGradient(ctx, pen) {
206
- var _a = pen.calculative.worldRect, x = _a.x, y = _a.y, ex = _a.ex, width = _a.width, height = _a.height, center = _a.center;
207
- var points = [
208
- { x: ex, y: y + height / 2 },
209
- { x: x, y: y + height / 2 },
210
- ];
211
- var _b = formatGradient(pen.calculative.textGradientColors), angle = _b.angle, colors = _b.colors;
212
- var r = getGradientR(angle, width, height);
213
- points.forEach(function (point) {
214
- rotatePoint(point, angle, center);
215
- });
216
- return getLinearGradient(ctx, points, colors, r);
217
- }
218
- function getGradientR(angle, width, height) {
219
- var dividAngle = (Math.atan(height / width) / Math.PI) * 180;
220
- var calculateAngle = (angle - 90) % 360;
221
- var r = 0;
222
- if ((calculateAngle > dividAngle && calculateAngle < 180 - dividAngle) ||
223
- (calculateAngle > 180 + dividAngle && calculateAngle < 360 - dividAngle) ||
224
- calculateAngle < 0) {
225
- //根据高计算
226
- if (calculateAngle > 270) {
227
- calculateAngle = 360 - calculateAngle;
228
- }
229
- else if (calculateAngle > 180) {
230
- calculateAngle = calculateAngle - 180;
231
- }
232
- else if (calculateAngle > 90) {
233
- calculateAngle = 180 - calculateAngle;
234
- }
235
- r = Math.abs(height / Math.sin((calculateAngle / 180) * Math.PI) / 2);
236
- }
237
- else {
238
- //根据宽计算
239
- if (calculateAngle > 270) {
240
- calculateAngle = 360 - calculateAngle;
241
- }
242
- else if (calculateAngle > 180) {
243
- calculateAngle = calculateAngle - 180;
244
- }
245
- else if (calculateAngle > 90) {
246
- calculateAngle = 180 - calculateAngle;
247
- }
248
- r = Math.abs(width / Math.cos((calculateAngle / 180) * Math.PI) / 2);
249
- }
250
- return r;
251
- }
252
- function formatGradient(color) {
253
- if (typeof color == 'string' && color.startsWith('linear-gradient')) {
254
- var arr = color.slice(16, -2).split('deg,');
255
- if (arr.length > 1) {
256
- var _arr = arr[1].split('%,');
257
- var colors_1 = [];
258
- _arr.forEach(function (stap) {
259
- if (/rgba?/.test(stap)) {
260
- var _arr_1 = stap.split(') ');
261
- colors_1.push({
262
- color: rgbaToHex(_arr_1[0] + ')'),
263
- i: parseFloat(_arr_1[1]) / 100,
264
- });
265
- }
266
- else {
267
- var _arr_2 = stap.split(' ');
268
- if (_arr_2.length > 2) {
269
- colors_1.push({
270
- color: _arr_2[1],
271
- i: parseFloat(_arr_2[2]) / 100,
272
- });
273
- }
274
- else {
275
- colors_1.push({
276
- color: _arr_2[0],
277
- i: parseFloat(_arr_2[1]) / 100,
278
- });
279
- }
280
- }
281
- });
282
- return {
283
- angle: parseFloat(arr[0]),
284
- colors: colors_1,
285
- };
286
- }
287
- else {
288
- return {
289
- angle: parseFloat(arr[0]),
290
- colors: [],
291
- };
292
- }
293
- }
294
- else {
295
- return {
296
- angle: 0,
297
- colors: [],
298
- };
299
- }
300
- }
301
- function rgbaToHex(value) {
302
- if (/rgba?/.test(value)) {
303
- var array = value.split(',');
304
- //不符合rgb或rgb规则直接return
305
- if (array.length < 3)
306
- return '';
307
- value = '#';
308
- for (var i = 0, color = void 0; (color = array[i++]);) {
309
- if (i < 4) {
310
- //前三位转换成16进制
311
- color = parseInt(color.replace(/[^\d]/gi, ''), 10).toString(16);
312
- value += color.length == 1 ? '0' + color : color;
313
- }
314
- else {
315
- //rgba的透明度转换成16进制
316
- color = color.replace(')', '');
317
- var colorA = parseInt(color * 255 + '');
318
- var colorAHex = colorA.toString(16);
319
- colorAHex = colorAHex.length === 2 ? colorAHex : '0' + colorAHex;
320
- value += colorAHex;
321
- }
322
- }
323
- value = value.toUpperCase();
324
- }
325
- return value;
326
- }
327
- function getLineGradient(ctx, pen) {
328
- var _a = pen.calculative.worldRect, x = _a.x, y = _a.y, ex = _a.ex, width = _a.width, height = _a.height, center = _a.center;
329
- var points = [
330
- { x: ex, y: y + height / 2 },
331
- { x: x, y: y + height / 2 },
332
- ];
333
- var _b = formatGradient(pen.calculative.lineGradientColors), angle = _b.angle, colors = _b.colors;
334
- var r = getGradientR(angle, width, height);
335
- points.forEach(function (point) {
336
- rotatePoint(point, angle, center);
337
- });
338
- return getLinearGradient(ctx, points, colors, r);
339
- }
340
- function getLinearGradient(ctx, points, colors, radius) {
341
- var arr = getLinearGradientPoints(points[0].x, points[0].y, points[1].x, points[1].y, radius);
342
- var gradient = ctx.createLinearGradient(arr[0], arr[1], arr[2], arr[3]);
343
- colors.forEach(function (stop) {
344
- gradient.addColorStop(stop.i, stop.color);
345
- });
346
- return gradient;
347
- }
348
- function drawLinearGradientLine(ctx, pen, points) {
349
- var colors = [];
350
- if (pen.calculative.gradientColorStop) {
351
- colors = pen.calculative.gradientColorStop;
352
- }
353
- else {
354
- colors = formatGradient(pen.calculative.lineGradientColors).colors;
355
- pen.calculative.gradientColorStop = colors;
356
- }
357
- ctx.strokeStyle = getLinearGradient(ctx, points, colors, pen.calculative.lineWidth / 2);
358
- ctx.beginPath();
359
- ctx.moveTo(points[0].x, points[0].y);
360
- ctx.lineTo(points[1].x, points[1].y);
361
- ctx.stroke();
362
- }
363
- function ctxDrawLinearGradientPath(ctx, pen) {
364
- var anchors = pen.calculative.worldAnchors;
365
- var smoothLenth = pen.calculative.lineWidth * (pen.calculative.gradientSmooth || pen.calculative.lineSmooth || 0);
366
- for (var i = 0; i < anchors.length - 1; i++) {
367
- if ((pen.lineName === 'curve' || pen.lineName === 'mind') &&
368
- anchors[i].curvePoints) {
369
- if (i > 0) {
370
- var lastCurvePoints = anchors[i - 1].curvePoints;
371
- if (lastCurvePoints) {
372
- //上一个存在锚点
373
- smoothTransition(ctx, pen, smoothLenth, lastCurvePoints[lastCurvePoints.length - 1], anchors[i], anchors[i].curvePoints[0]);
374
- }
375
- else {
376
- smoothTransition(ctx, pen, smoothLenth, anchors[i - 1], anchors[i], anchors[i].curvePoints[0]);
377
- }
378
- //获取当前相对于0的位置
379
- var next = getSmoothAdjacent(smoothLenth, anchors[i], anchors[i].curvePoints[0]);
380
- drawLinearGradientLine(ctx, pen, [next, anchors[i].curvePoints[1]]);
381
- }
382
- else {
383
- drawLinearGradientLine(ctx, pen, [
384
- anchors[i],
385
- anchors[i].curvePoints[0],
386
- ]);
387
- drawLinearGradientLine(ctx, pen, [
388
- anchors[i].curvePoints[0],
389
- anchors[i].curvePoints[1],
390
- ]);
391
- }
392
- var len = anchors[i].curvePoints.length - 1;
393
- for (var j = 1; j < len; j++) {
394
- drawLinearGradientLine(ctx, pen, [
395
- anchors[i].curvePoints[j],
396
- anchors[i].curvePoints[j + 1],
397
- ]);
398
- }
399
- var last = getSmoothAdjacent(smoothLenth, anchors[i + 1], anchors[i].curvePoints[len]);
400
- drawLinearGradientLine(ctx, pen, [anchors[i].curvePoints[len], last]);
401
- }
402
- else {
403
- var _next = anchors[i];
404
- var _last = anchors[i + 1];
405
- if (i > 0 && i < anchors.length - 1) {
406
- //有突兀的地方
407
- var lastCurvePoints = anchors[i - 1].curvePoints;
408
- if (lastCurvePoints) {
409
- smoothTransition(ctx, pen, smoothLenth, lastCurvePoints[lastCurvePoints.length - 1], anchors[i], anchors[i + 1]);
410
- }
411
- else {
412
- smoothTransition(ctx, pen, smoothLenth, anchors[i - 1], anchors[i], anchors[i + 1]);
413
- }
414
- }
415
- if (i > 0 && i < anchors.length - 1) {
416
- _next = getSmoothAdjacent(smoothLenth, anchors[i], anchors[i + 1]);
417
- }
418
- if (i < anchors.length - 2) {
419
- _last = getSmoothAdjacent(smoothLenth, anchors[i + 1], anchors[i]);
420
- }
421
- var flag = false;
422
- if (i === 0) {
423
- if (pen.fromLineCap && pen.fromLineCap !== 'butt') {
424
- ctx.save();
425
- flag = true;
426
- ctx.lineCap = pen.fromLineCap;
427
- }
428
- }
429
- if (i !== 0 && i === anchors.length - 2) {
430
- if (pen.toLineCap && pen.toLineCap !== 'butt') {
431
- ctx.save();
432
- flag = true;
433
- ctx.lineCap = pen.toLineCap;
434
- }
435
- }
436
- drawLinearGradientLine(ctx, pen, [_next, _last]);
437
- if (flag) {
438
- ctx.restore();
439
- }
440
- if (anchors.length === 2 && i === 0) {
441
- ctx.save();
442
- flag = true;
443
- ctx.lineCap = pen.toLineCap;
444
- var _y = 0.1;
445
- var _x = 0.1;
446
- if (_next.x - _last.x === 0) {
447
- _x = 0;
448
- }
449
- else {
450
- _y = (_next.y - _last.y) / (_next.x - _last.x) * 0.1;
451
- }
452
- drawLinearGradientLine(ctx, pen, [{ x: _last.x - _x, y: _last.y - _y }, _last]);
453
- ctx.restore();
454
- }
455
- }
456
- }
457
- }
458
- function getSmoothAdjacent(smoothLenth, p1, p2) {
459
- var nexLength = Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
460
- if (nexLength === 0) {
461
- return {
462
- x: p1.x,
463
- y: p1.y,
464
- };
465
- }
466
- if (smoothLenth < nexLength) {
467
- return {
468
- x: p1.x + ((p2.x - p1.x) * smoothLenth) / nexLength,
469
- y: p1.y + ((p2.y - p1.y) * smoothLenth) / nexLength,
470
- };
471
- }
472
- else {
473
- return {
474
- x: p1.x + (p2.x - p1.x) / nexLength / 2,
475
- y: p1.y + (p2.y - p1.y) / nexLength / 2,
476
- };
477
- }
478
- }
479
- function smoothTransition(ctx, pen, smoothLenth, p1, p2, p3) {
480
- var last = getSmoothAdjacent(smoothLenth, p2, p1);
481
- var next = getSmoothAdjacent(smoothLenth, p2, p3);
482
- var contrlPoint = { x: p2.x, y: p2.y };
483
- var points = getBezierPoints(pen.calculative.canvas.store.data.smoothNum || 20, last, contrlPoint, next);
484
- for (var k = 0; k < points.length - 1; k++) {
485
- drawLinearGradientLine(ctx, pen, [
486
- {
487
- x: points[k].x,
488
- y: points[k].y,
489
- },
490
- {
491
- x: points[k + 1].x,
492
- y: points[k + 1].y,
493
- },
494
- ]);
495
- }
496
- }
497
- function smoothAnimateTransition(ctx, smoothLenth, p2, p3) {
498
- var next = getSmoothAdjacent(smoothLenth, p2, p3);
499
- var contrlPoint = { x: p2.x, y: p2.y };
500
- ctx.quadraticCurveTo(contrlPoint.x, contrlPoint.y, next.x, next.y);
501
- }
502
- export function getGradientAnimatePath(pen) {
503
- var anchors = pen.calculative.worldAnchors;
504
- var smoothLenth = pen.calculative.lineWidth * (pen.calculative.gradientSmooth || pen.calculative.lineSmooth || 0);
505
- //只创建一次
506
- var _path = new Path2D();
507
- for (var i = 0; i < anchors.length - 1; i++) {
508
- var _next = anchors[i];
509
- var _last = anchors[i + 1];
510
- if (i == 0) {
511
- _path.moveTo(anchors[i].x, anchors[i].y);
512
- }
513
- if (i > 0 && i < anchors.length - 1) {
514
- //有突兀的地方
515
- var lastCurvePoints = anchors[i - 1].curvePoints;
516
- // const path = new Path2D();
517
- if (lastCurvePoints) {
518
- smoothAnimateTransition(_path, smoothLenth, anchors[i], anchors[i + 1]);
519
- }
520
- else {
521
- smoothAnimateTransition(_path, smoothLenth, anchors[i], anchors[i + 1]);
522
- }
523
- }
524
- if (i > 0 && i < anchors.length - 1) {
525
- _next = getSmoothAdjacent(smoothLenth, anchors[i], anchors[i + 1]);
526
- }
527
- if (i < anchors.length - 2) {
528
- _last = getSmoothAdjacent(smoothLenth, anchors[i + 1], anchors[i]);
529
- }
530
- _path.lineTo(_last.x, _last.y);
531
- }
532
- return _path;
533
- }
534
- function getAngle(p1, p2, p3) {
535
- var a = { x: 0, y: 0 }, b = { x: 0, y: 0 };
536
- a.x = p1.x - p2.x;
537
- a.y = p1.y - p2.y;
538
- b.x = p3.x - p2.x;
539
- b.y = p3.y - p2.y;
540
- return ((Math.acos((a.x * b.x + a.y * b.y) /
541
- (Math.sqrt(a.x * a.x + a.y * a.y) * Math.sqrt(b.x * b.x + b.y * b.y))) /
542
- Math.PI) *
543
- 180);
544
- }
545
- function getBezierPoints(num, p1, p2, p3, p4) {
546
- if (num === void 0) { num = 100; }
547
- var func = null;
548
- var points = [];
549
- if (!p3 && !p4) {
550
- func = oneBezier;
551
- }
552
- else if (p3 && !p4) {
553
- func = twoBezier;
554
- }
555
- else if (p3 && p4) {
556
- func = threeBezier;
557
- }
558
- for (var i = 0; i < num; i++) {
559
- points.push(func(i / num, p1, p2, p3, p4));
560
- }
561
- if (p4) {
562
- points.push(p4);
563
- }
564
- else if (p3) {
565
- points.push(p3);
566
- }
567
- return points;
568
- }
569
- /**
570
- * @desc 一阶贝塞尔
571
- * @param t 当前百分比
572
- * @param p1 起点坐标
573
- * @param p2 终点坐标
574
- */
575
- function oneBezier(t, p1, p2) {
576
- var x1 = p1.x, y1 = p1.y;
577
- var x2 = p2.x, y2 = p2.y;
578
- var x = x1 + (x2 - x1) * t;
579
- var y = y1 + (y2 - y1) * t;
580
- return { x: x, y: y };
581
- }
582
- /**
583
- * @desc 二阶贝塞尔
584
- * @param t 当前百分比
585
- * @param p1 起点坐标
586
- * @param p2 终点坐标
587
- * @param cp 控制点
588
- */
589
- function twoBezier(t, p1, cp, p2) {
590
- var x1 = p1.x, y1 = p1.y;
591
- var cx = cp.x, cy = cp.y;
592
- var x2 = p2.x, y2 = p2.y;
593
- var x = (1 - t) * (1 - t) * x1 + 2 * t * (1 - t) * cx + t * t * x2;
594
- var y = (1 - t) * (1 - t) * y1 + 2 * t * (1 - t) * cy + t * t * y2;
595
- return { x: x, y: y };
596
- }
597
- /**
598
- * @desc 三阶贝塞尔
599
- * @param t 当前百分比
600
- * @param p1 起点坐标
601
- * @param p2 终点坐标
602
- * @param cp1 控制点1
603
- * @param cp2 控制点2
604
- */
605
- function threeBezier(t, p1, cp1, cp2, p2) {
606
- var x1 = p1.x, y1 = p1.y;
607
- var x2 = p2.x, y2 = p2.y;
608
- var cx1 = cp1.x, cy1 = cp1.y;
609
- var cx2 = cp2.x, cy2 = cp2.y;
610
- var x = x1 * (1 - t) * (1 - t) * (1 - t) +
611
- 3 * cx1 * t * (1 - t) * (1 - t) +
612
- 3 * cx2 * t * t * (1 - t) +
613
- x2 * t * t * t;
614
- var y = y1 * (1 - t) * (1 - t) * (1 - t) +
615
- 3 * cy1 * t * (1 - t) * (1 - t) +
616
- 3 * cy2 * t * t * (1 - t) +
617
- y2 * t * t * t;
618
- return { x: x, y: y };
619
- }
620
- function strokeLinearGradient(ctx, pen) {
621
- var _a = pen.calculative, worldRect = _a.worldRect, lineGradientFromColor = _a.lineGradientFromColor, lineGradientToColor = _a.lineGradientToColor, lineGradientAngle = _a.lineGradientAngle;
622
- return linearGradient(ctx, worldRect, lineGradientFromColor, lineGradientToColor, lineGradientAngle);
623
- }
624
- /**
625
- * 避免副作用,把创建好后的线性渐变对象返回出来
626
- * @param ctx 画布绘制对象
627
- * @param worldRect 世界坐标
628
- * @returns 线性渐变
629
- */
630
- function linearGradient(ctx, worldRect, fromColor, toColor, angle) {
631
- if (!fromColor || !toColor) {
632
- return;
633
- }
634
- var x = worldRect.x, y = worldRect.y, center = worldRect.center, ex = worldRect.ex, ey = worldRect.ey;
635
- var from = {
636
- x: x,
637
- y: center.y,
638
- };
639
- var to = {
640
- x: ex,
641
- y: center.y,
642
- };
643
- if (angle % 90 === 0 && angle % 180) {
644
- from.x = center.x;
645
- to.x = center.x;
646
- if (angle % 270) {
647
- from.y = y;
648
- to.y = ey;
649
- }
650
- else {
651
- from.y = ey;
652
- to.y = y;
653
- }
654
- }
655
- else if (angle) {
656
- rotatePoint(from, angle, worldRect.center);
657
- rotatePoint(to, angle, worldRect.center);
658
- }
659
- // contributor: https://github.com/sunnyguohua/meta2d
660
- var grd = ctx.createLinearGradient(from.x, from.y, to.x, to.y);
661
- grd.addColorStop(0, fromColor);
662
- grd.addColorStop(1, toColor);
663
- return grd;
664
- }
665
- /**
666
- * 根据图片的宽高, imageRatio iconAlign 来获取图片的实际位置
667
- * @param pen 画笔
668
- */
669
- function getImagePosition(pen) {
670
- var _a = pen.calculative, rect = _a.worldIconRect, iconWidth = _a.iconWidth, iconHeight = _a.iconHeight, imgNaturalWidth = _a.imgNaturalWidth, imgNaturalHeight = _a.imgNaturalHeight;
671
- var x = rect.x, y = rect.y, w = rect.width, h = rect.height;
672
- if (iconWidth) {
673
- w = iconWidth;
674
- }
675
- if (iconHeight) {
676
- h = iconHeight;
677
- }
678
- if (imgNaturalWidth && imgNaturalHeight && pen.imageRatio) {
679
- var scaleW = rect.width / imgNaturalWidth;
680
- var scaleH = rect.height / imgNaturalHeight;
681
- var scaleMin = Math.min(scaleW, scaleH);
682
- var wDivideH = imgNaturalWidth / imgNaturalHeight;
683
- if (iconWidth) {
684
- h = iconWidth / wDivideH;
685
- }
686
- else if (iconHeight) {
687
- w = iconHeight * wDivideH;
688
- }
689
- else {
690
- w = scaleMin * imgNaturalWidth;
691
- h = scaleMin * imgNaturalHeight;
692
- }
693
- }
694
- x += (rect.width - w) / 2;
695
- y += (rect.height - h) / 2;
696
- switch (pen.iconAlign) {
697
- case 'top':
698
- y = rect.y;
699
- break;
700
- case 'bottom':
701
- y = rect.ey - h;
702
- break;
703
- case 'left':
704
- x = rect.x;
705
- break;
706
- case 'right':
707
- x = rect.ex - w;
708
- break;
709
- case 'left-top':
710
- x = rect.x;
711
- y = rect.y;
712
- break;
713
- case 'right-top':
714
- x = rect.ex - w;
715
- y = rect.y;
716
- break;
717
- case 'left-bottom':
718
- x = rect.x;
719
- y = rect.ey - h;
720
- break;
721
- case 'right-bottom':
722
- x = rect.ex - w;
723
- y = rect.ey - h;
724
- break;
725
- }
726
- return {
727
- x: x,
728
- y: y,
729
- width: w,
730
- height: h,
731
- };
732
- }
733
- export function drawImage(ctx, pen) {
734
- var _a = getImagePosition(pen), x = _a.x, y = _a.y, width = _a.width, height = _a.height;
735
- var _b = pen.calculative, worldIconRect = _b.worldIconRect, iconRotate = _b.iconRotate, img = _b.img;
736
- if (iconRotate) {
737
- var _c = worldIconRect.center, centerX = _c.x, centerY = _c.y;
738
- ctx.translate(centerX, centerY);
739
- ctx.rotate((iconRotate * Math.PI) / 180);
740
- ctx.translate(-centerX, -centerY);
741
- }
742
- if (pen.imageRadius) {
743
- ctx.save();
744
- var wr = pen.calculative.imageRadius || 0, hr = wr;
745
- var _d = pen.calculative.worldRect, _x = _d.x, _y = _d.y, w = _d.width, h = _d.height, ex = _d.ex, ey = _d.ey;
746
- if (wr < 1) {
747
- wr = w * wr;
748
- hr = h * hr;
749
- }
750
- var r = wr < hr ? wr : hr;
751
- if (w < 2 * r) {
752
- r = w / 2;
753
- }
754
- if (h < 2 * r) {
755
- r = h / 2;
756
- }
757
- ctx.beginPath();
758
- ctx.moveTo(_x + r, _y);
759
- ctx.arcTo(ex, _y, ex, ey, r);
760
- ctx.arcTo(ex, ey, _x, ey, r);
761
- ctx.arcTo(_x, ey, _x, _y, r);
762
- ctx.arcTo(_x, _y, ex, _y, r);
763
- ctx.clip();
764
- ctx.drawImage(img, x, y, width, height);
765
- ctx.restore();
766
- }
767
- else {
768
- ctx.drawImage(img, x, y, width, height);
769
- }
770
- }
771
- /**
772
- * 获取文字颜色, textColor 优先其次 color
773
- */
774
- export function getTextColor(pen, store) {
775
- var _a = pen.calculative, textColor = _a.textColor, color = _a.color;
776
- var data = store.data, options = store.options;
777
- return (textColor ||
778
- color ||
779
- data.textColor ||
780
- data.color ||
781
- options.textColor ||
782
- options.color);
783
- }
784
- function drawText(ctx, pen) {
785
- 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, textType = _a.textType;
786
- if (text == undefined || hiddenText) {
787
- return;
788
- }
789
- var store = canvas.store;
790
- ctx.save();
791
- if (!textHasShadow) {
792
- ctx.shadowBlur = 0;
793
- ctx.shadowOffsetX = 0;
794
- ctx.shadowOffsetY = 0;
795
- }
796
- var fill = undefined;
797
- if (pen.calculative.disabled) {
798
- fill = pen.disabledTextColor || pen.disabledColor || pSBC(0.4, getTextColor(pen, store));
799
- }
800
- else if (pen.calculative.hover) {
801
- fill = pen.hoverTextColor || pen.hoverColor || store.options.hoverColor;
802
- }
803
- else if (pen.calculative.active) {
804
- fill = pen.activeTextColor || pen.activeColor || store.options.activeColor;
805
- }
806
- var gradient = undefined;
807
- if (textType === Gradient.Linear) {
808
- gradient = getTextGradient(ctx, pen);
809
- }
810
- else if (textType === Gradient.Radial) {
811
- gradient = getTextRadialGradient(ctx, pen);
812
- }
813
- ctx.fillStyle = fill || gradient || getTextColor(pen, store);
814
- ctx.font = getFont({
815
- fontStyle: fontStyle,
816
- fontWeight: fontWeight,
817
- fontFamily: fontFamily || store.options.fontFamily,
818
- fontSize: fontSize,
819
- lineHeight: lineHeight,
820
- });
821
- !pen.calculative.textDrawRect && calcTextDrawRect(ctx, pen);
822
- var _b = pen.calculative.textDrawRect, drawRectX = _b.x, drawRectY = _b.y, width = _b.width, height = _b.height;
823
- if (textBackground) {
824
- ctx.save();
825
- ctx.fillStyle = textBackground;
826
- ctx.fillRect(drawRectX, drawRectY, width, height);
827
- ctx.restore();
828
- }
829
- var y = 0.55;
830
- var textAlign = pen.textAlign || store.options.textAlign;
831
- var oneRowHeight = fontSize * lineHeight;
832
- pen.calculative.textLines.forEach(function (text, i) {
833
- var textLineWidth = pen.calculative.textLineWidths[i];
834
- var x = 0;
835
- if (textAlign === 'center') {
836
- x = (width - textLineWidth) / 2;
837
- }
838
- else if (textAlign === 'right') {
839
- x = width - textLineWidth;
840
- }
841
- // 下划线
842
- ctx.fillText(text, drawRectX + x, drawRectY + (i + y) * oneRowHeight);
843
- var textDecorationColor = pen.textDecorationColor, textDecorationDash = pen.textDecorationDash, textDecoration = pen.textDecoration;
844
- if (textDecoration) {
845
- drawUnderLine(ctx, {
846
- x: drawRectX + x,
847
- y: drawRectY + (i + y) * oneRowHeight,
848
- width: textLineWidth,
849
- }, { textDecorationColor: textDecorationColor, textDecorationDash: textDecorationDash, fontSize: fontSize });
850
- }
851
- // 删除线
852
- var textStrickoutColor = pen.textStrickoutColor, textStrickoutDash = pen.textStrickoutDash, textStrickout = pen.textStrickout;
853
- if (textStrickout) {
854
- drawStrickout(ctx, {
855
- x: drawRectX + x,
856
- y: drawRectY + (i + y) * oneRowHeight,
857
- width: textLineWidth,
858
- }, { textStrickoutColor: textStrickoutColor, textStrickoutDash: textStrickoutDash, fontSize: fontSize });
859
- }
860
- });
861
- ctx.restore();
862
- }
863
- function drawUnderLine(ctx, location, config) {
864
- var textDecorationColor = config.textDecorationColor, textDecorationDash = config.textDecorationDash, fontSize = config.fontSize;
865
- var x = location.x, y = location.y, width = location.width;
866
- switch (ctx.textBaseline) {
867
- case 'top':
868
- y += fontSize;
869
- break;
870
- case 'middle':
871
- y += fontSize / 2;
872
- break;
873
- }
874
- ctx.save();
875
- ctx.beginPath();
876
- ctx.strokeStyle = textDecorationColor ? textDecorationColor : ctx.fillStyle;
877
- ctx.lineWidth = 1;
878
- ctx.moveTo(x, y);
879
- ctx.setLineDash(textDecorationDash || []);
880
- ctx.lineTo(x + width, y);
881
- ctx.stroke();
882
- ctx.restore();
883
- }
884
- function drawStrickout(ctx, location, config) {
885
- var textStrickoutColor = config.textStrickoutColor, textStrickoutDash = config.textStrickoutDash, fontSize = config.fontSize;
886
- var x = location.x, y = location.y, width = location.width;
887
- switch (ctx.textBaseline) {
888
- case 'top':
889
- y += fontSize / 2;
890
- break;
891
- case 'bottom':
892
- y -= fontSize / 2;
893
- break;
894
- }
895
- ctx.save();
896
- ctx.beginPath();
897
- ctx.strokeStyle = textStrickoutColor ? textStrickoutColor : ctx.fillStyle;
898
- ctx.lineWidth = 1;
899
- ctx.moveTo(x, y);
900
- ctx.setLineDash(textStrickoutDash || []);
901
- ctx.lineTo(x + width, y);
902
- ctx.stroke();
903
- ctx.restore();
904
- }
905
- function drawFillText(ctx, pen, text) {
906
- var e_1, _a;
907
- if (text == undefined) {
908
- return;
909
- }
910
- var _b = pen.calculative, fontStyle = _b.fontStyle, fontWeight = _b.fontWeight, fontSize = _b.fontSize, fontFamily = _b.fontFamily, lineHeight = _b.lineHeight, canvas = _b.canvas;
911
- var store = canvas.store;
912
- ctx.save();
913
- var fill = undefined;
914
- if (pen.calculative.hover) {
915
- fill = pen.hoverTextColor || pen.hoverColor || store.options.hoverColor;
916
- }
917
- else if (pen.calculative.active) {
918
- fill = pen.activeTextColor || pen.activeColor || store.options.activeColor;
919
- }
920
- ctx.fillStyle = fill || getTextColor(pen, store);
921
- ctx.font = getFont({
922
- fontStyle: fontStyle,
923
- fontWeight: fontWeight,
924
- fontFamily: fontFamily || store.options.fontFamily,
925
- fontSize: fontSize,
926
- lineHeight: lineHeight,
927
- });
928
- var w = ctx.measureText(text).width;
929
- var t;
930
- var prev;
931
- try {
932
- for (var _c = __values(pen.calculative.worldAnchors), _d = _c.next(); !_d.done; _d = _c.next()) {
933
- var anchor = _d.value;
934
- if (!prev) {
935
- prev = anchor;
936
- continue;
937
- }
938
- var dis = distance(prev, anchor);
939
- var n = Math.floor(dis / w);
940
- t = '';
941
- for (var i = 0; i < n; i++) {
942
- t += text;
943
- }
944
- var angle = calcRotate(prev, anchor) - 270;
945
- ctx.save();
946
- if (angle % 360 !== 0) {
947
- var x = prev.x, y = prev.y;
948
- ctx.translate(x, y);
949
- var rotate = (angle * Math.PI) / 180;
950
- ctx.rotate(rotate);
951
- ctx.translate(-x, -y);
952
- }
953
- ctx.fillText(t, prev.x, prev.y + lineHeight / 2);
954
- ctx.restore();
955
- prev = anchor;
956
- }
957
- }
958
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
959
- finally {
960
- try {
961
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
962
- }
963
- finally { if (e_1) throw e_1.error; }
964
- }
965
- ctx.restore();
966
- }
967
- export function drawIcon(ctx, pen) {
968
- var store = pen.calculative.canvas.store;
969
- ctx.save();
970
- ctx.shadowColor = '';
971
- ctx.shadowBlur = 0;
972
- ctx.shadowOffsetX = 0;
973
- ctx.shadowOffsetY = 0;
974
- ctx.textAlign = 'center';
975
- ctx.textBaseline = 'middle';
976
- var iconRect = pen.calculative.worldIconRect;
977
- var x = iconRect.x + iconRect.width / 2;
978
- var y = iconRect.y + iconRect.height / 2;
979
- switch (pen.iconAlign) {
980
- case 'top':
981
- y = iconRect.y;
982
- ctx.textBaseline = 'top';
983
- break;
984
- case 'bottom':
985
- y = iconRect.ey;
986
- ctx.textBaseline = 'bottom';
987
- break;
988
- case 'left':
989
- x = iconRect.x;
990
- ctx.textAlign = 'left';
991
- break;
992
- case 'right':
993
- x = iconRect.ex;
994
- ctx.textAlign = 'right';
995
- break;
996
- case 'left-top':
997
- x = iconRect.x;
998
- y = iconRect.y;
999
- ctx.textAlign = 'left';
1000
- ctx.textBaseline = 'top';
1001
- break;
1002
- case 'right-top':
1003
- x = iconRect.ex;
1004
- y = iconRect.y;
1005
- ctx.textAlign = 'right';
1006
- ctx.textBaseline = 'top';
1007
- break;
1008
- case 'left-bottom':
1009
- x = iconRect.x;
1010
- y = iconRect.ey;
1011
- ctx.textAlign = 'left';
1012
- ctx.textBaseline = 'bottom';
1013
- break;
1014
- case 'right-bottom':
1015
- x = iconRect.ex;
1016
- y = iconRect.ey;
1017
- ctx.textAlign = 'right';
1018
- ctx.textBaseline = 'bottom';
1019
- break;
1020
- }
1021
- var fontWeight = pen.calculative.iconWeight;
1022
- var fontSize = undefined;
1023
- var fontFamily = pen.calculative.iconFamily;
1024
- if (pen.calculative.iconSize > 0) {
1025
- fontSize = pen.calculative.iconSize;
1026
- }
1027
- else if (iconRect.width > iconRect.height) {
1028
- fontSize = iconRect.height;
1029
- }
1030
- else {
1031
- fontSize = iconRect.width;
1032
- }
1033
- ctx.font = getFont({
1034
- fontSize: fontSize,
1035
- fontWeight: fontWeight,
1036
- fontFamily: fontFamily,
1037
- });
1038
- ctx.fillStyle = pen.calculative.iconColor || getTextColor(pen, store);
1039
- if (pen.calculative.iconRotate) {
1040
- ctx.translate(iconRect.center.x, iconRect.center.y);
1041
- ctx.rotate((pen.calculative.iconRotate * Math.PI) / 180);
1042
- ctx.translate(-iconRect.center.x, -iconRect.center.y);
1043
- }
1044
- ctx.beginPath();
1045
- ctx.fillText(pen.calculative.icon, x, y);
1046
- ctx.restore();
1047
- }
1048
- /**
1049
- * canvas2svg 中对 font 的解析规则比 canvas 中简单,能识别的类型很少
1050
- * @returns ctx.font
1051
- */
1052
- export function getFont(_a) {
1053
- 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;
1054
- return fontStyle + " " + textDecoration + " " + fontWeight + " " + fontSize + "px/" + lineHeight + " " + fontFamily;
1055
- }
1056
- // TODO: 0.5 偏移量在 图片中可能存在问题
1057
- export function ctxFlip(ctx, pen) {
1058
- // worldRect 可能为 undefined
1059
- var _a = pen.calculative.worldRect || {}, x = _a.x, ex = _a.ex, y = _a.y, ey = _a.ey;
1060
- if (pen.calculative.flipX) {
1061
- ctx.translate(x + ex + 0.5, 0.5);
1062
- ctx.scale(-1, 1);
1063
- }
1064
- if (pen.calculative.flipY) {
1065
- ctx.translate(0.5, y + ey + 0.5);
1066
- ctx.scale(1, -1);
1067
- }
1068
- }
1069
- export function ctxRotate(ctx, pen, noFlip) {
1070
- if (noFlip === void 0) { noFlip = false; }
1071
- var _a = pen.calculative.worldRect.pivot || pen.calculative.worldRect.center, x = _a.x, y = _a.y;
1072
- ctx.translate(x, y);
1073
- var rotate = (pen.calculative.rotate * Math.PI) / 180;
1074
- // 目前只有水平和垂直翻转,都需要 * -1
1075
- if (!noFlip) {
1076
- if (pen.calculative.flipX) {
1077
- rotate *= -1;
1078
- }
1079
- if (pen.calculative.flipY) {
1080
- rotate *= -1;
1081
- }
1082
- }
1083
- ctx.rotate(rotate);
1084
- ctx.translate(-x, -y);
1085
- }
1086
- export function renderPen(ctx, pen, download) {
1087
- var e_2, _a;
1088
- ctx.save();
1089
- ctx.translate(0.5, 0.5);
1090
- ctx.beginPath();
1091
- var store = pen.calculative.canvas.store;
1092
- var textFlip = pen.textFlip || store.options.textFlip;
1093
- var textRotate = pen.textRotate || store.options.textRotate;
1094
- if (!textFlip || !textRotate) {
1095
- ctx.save();
1096
- }
1097
- ctxFlip(ctx, pen);
1098
- if (pen.calculative.rotate && pen.name !== 'line') {
1099
- ctxRotate(ctx, pen);
1100
- }
1101
- if (pen.calculative.lineWidth > 1 || download) {
1102
- ctx.lineWidth = pen.calculative.lineWidth;
1103
- }
1104
- inspectRect(ctx, store, pen); // 审查 rect
1105
- var fill;
1106
- // 该变量控制在 hover active 状态下的节点是否设置填充颜色
1107
- // let setBack = true;
1108
- var lineGradientFlag = false;
1109
- var _stroke = undefined;
1110
- if (pen.calculative.disabled) {
1111
- _stroke = pen.disabledColor || store.options.disabledColor || pSBC(0.4, pen.calculative.color || getGlobalColor(store));
1112
- fill = pen.disabledBackground || store.options.disabledBackground || pSBC(0.4, pen.calculative.background || store.data.penBackground);
1113
- }
1114
- else if (pen.mouseDownValid && pen.calculative.mouseDown) {
1115
- _stroke = pen.mouseDownColor || pSBC(-0.4, pen.calculative.color || getGlobalColor(store));
1116
- fill = pen.mouseDownBackground || pSBC(-0.4, pen.calculative.background || store.data.penBackground);
1117
- }
1118
- else if (pen.calculative.hover) {
1119
- _stroke = pen.hoverColor || store.options.hoverColor;
1120
- fill = pen.hoverBackground || store.options.hoverBackground;
1121
- // ctx.fillStyle = fill;
1122
- // fill && (setBack = false);
1123
- }
1124
- else if (pen.calculative.active) {
1125
- _stroke = pen.activeColor || store.options.activeColor;
1126
- fill = pen.activeBackground || store.options.activeBackground;
1127
- // ctx.fillStyle = fill;
1128
- // fill && (setBack = false);
1129
- }
1130
- else if (pen.calculative.isDock) {
1131
- if (pen.type === PenType.Line) {
1132
- _stroke = store.options.dockPenColor;
1133
- }
1134
- else {
1135
- fill = rgba(store.options.dockPenColor, 0.2);
1136
- // ctx.fillStyle = fill;
1137
- // fill && (setBack = false);
1138
- }
1139
- }
1140
- // else {
1141
- var strokeImg = pen.calculative.strokeImg;
1142
- if (pen.calculative.strokeImage && strokeImg) {
1143
- ctx.strokeStyle = _stroke || ctx.createPattern(strokeImg, 'repeat');
1144
- // fill = true;
1145
- }
1146
- else {
1147
- var stroke = void 0;
1148
- // TODO: 线只有线性渐变
1149
- if (pen.calculative.strokeType) {
1150
- if (pen.calculative.lineGradientColors) {
1151
- if (pen.name === 'line') {
1152
- lineGradientFlag = true;
1153
- }
1154
- else {
1155
- if (pen.calculative.lineGradient) {
1156
- stroke = pen.calculative.lineGradient;
1157
- }
1158
- else {
1159
- stroke = getLineGradient(ctx, pen);
1160
- pen.calculative.lineGradient = stroke;
1161
- }
1162
- }
1163
- }
1164
- else {
1165
- stroke = strokeLinearGradient(ctx, pen);
1166
- }
1167
- }
1168
- else {
1169
- stroke = pen.calculative.color || getGlobalColor(store);
1170
- }
1171
- ctx.strokeStyle = _stroke || stroke;
1172
- }
1173
- // }
1174
- //if (setBack) {
1175
- var backgroundImg = pen.calculative.backgroundImg;
1176
- if (pen.calculative.backgroundImage && backgroundImg) {
1177
- ctx.fillStyle = fill || ctx.createPattern(backgroundImg, 'repeat');
1178
- fill = true;
1179
- }
1180
- else {
1181
- var back = void 0;
1182
- if (pen.calculative.bkType === Gradient.Linear) {
1183
- if (pen.calculative.gradientColors) {
1184
- // if (!pen.type) {
1185
- //连线不考虑渐进背景
1186
- if (pen.calculative.gradient) {
1187
- //位置变化/放大缩小操作不会触发重新计算
1188
- back = pen.calculative.gradient;
1189
- }
1190
- else {
1191
- back = getBkGradient(ctx, pen);
1192
- pen.calculative.gradient = back;
1193
- }
1194
- // }
1195
- }
1196
- else {
1197
- back = drawBkLinearGradient(ctx, pen);
1198
- }
1199
- }
1200
- else if (pen.calculative.bkType === Gradient.Radial) {
1201
- if (pen.calculative.gradientColors) {
1202
- if (pen.calculative.radialGradient) {
1203
- back = pen.calculative.radialGradient;
1204
- }
1205
- else {
1206
- back = getBkRadialGradient(ctx, pen);
1207
- pen.calculative.radialGradient = back;
1208
- }
1209
- }
1210
- else {
1211
- back = drawBkRadialGradient(ctx, pen);
1212
- }
1213
- }
1214
- else {
1215
- back = pen.calculative.background || store.data.penBackground;
1216
- }
1217
- ctx.fillStyle = fill || back;
1218
- fill = !!back;
1219
- }
1220
- // }
1221
- setLineCap(ctx, pen);
1222
- setLineJoin(ctx, pen);
1223
- setGlobalAlpha(ctx, pen);
1224
- if (pen.calculative.lineDash) {
1225
- ctx.setLineDash(pen.calculative.lineDash.map(function (item) { return item * pen.calculative.canvas.store.data.scale; }));
1226
- }
1227
- if (pen.calculative.lineDashOffset) {
1228
- ctx.lineDashOffset = pen.calculative.lineDashOffset;
1229
- }
1230
- if (pen.calculative.shadowColor) {
1231
- ctx.shadowColor = pen.calculative.shadowColor;
1232
- ctx.shadowOffsetX = pen.calculative.shadowOffsetX;
1233
- ctx.shadowOffsetY = pen.calculative.shadowOffsetY;
1234
- ctx.shadowBlur = pen.calculative.shadowBlur;
1235
- }
1236
- if (lineGradientFlag) {
1237
- ctxDrawLinearGradientPath(ctx, pen);
1238
- ctxDrawLinePath(true, ctx, pen, store);
1239
- }
1240
- else {
1241
- ctxDrawPath(true, ctx, pen, store, fill);
1242
- ctxDrawCanvas(ctx, pen);
1243
- }
1244
- if (!(pen.image && pen.calculative.img) && pen.calculative.icon) {
1245
- drawIcon(ctx, pen);
1246
- }
1247
- if (!textFlip || !textRotate) {
1248
- ctx.restore();
1249
- }
1250
- if (textFlip && !textRotate) {
1251
- ctxFlip(ctx, pen);
1252
- }
1253
- if (!textFlip && textRotate) {
1254
- if (pen.calculative.rotate && pen.name !== 'line') {
1255
- ctxRotate(ctx, pen, true);
1256
- }
1257
- }
1258
- drawText(ctx, pen);
1259
- if (pen.type === PenType.Line && pen.fillTexts) {
1260
- try {
1261
- for (var _b = __values(pen.fillTexts), _c = _b.next(); !_c.done; _c = _b.next()) {
1262
- var text = _c.value;
1263
- drawFillText(ctx, pen, text);
1264
- }
1265
- }
1266
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
1267
- finally {
1268
- try {
1269
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1270
- }
1271
- finally { if (e_2) throw e_2.error; }
1272
- }
1273
- }
1274
- ctx.restore();
1275
- }
1276
- /**
1277
- * 更改 ctx 的 lineCap 属性
1278
- */
1279
- export function setLineCap(ctx, pen) {
1280
- var lineCap = pen.lineCap || (pen.type ? 'round' : 'square');
1281
- if (lineCap) {
1282
- ctx.lineCap = lineCap;
1283
- }
1284
- else if (pen.type) {
1285
- ctx.lineCap = 'round';
1286
- }
1287
- }
1288
- /**
1289
- * 更改 ctx 的 lineJoin 属性
1290
- */
1291
- export function setLineJoin(ctx, pen) {
1292
- var lineJoin = pen.lineJoin;
1293
- if (lineJoin) {
1294
- ctx.lineJoin = lineJoin;
1295
- }
1296
- else if (pen.type) {
1297
- ctx.lineJoin = 'round';
1298
- }
1299
- }
1300
- /**
1301
- * 通常用在下载 svg
1302
- * canvas2svg 与 canvas ctx 设置 strokeStyle 表现不同
1303
- * 若设置值为 undefined ,canvas2svg 为空, canvas ctx 为上一个值
1304
- */
1305
- export function renderPenRaw(ctx, pen, rect, download) {
1306
- var e_3, _a;
1307
- var _b, _c;
1308
- ctx.save();
1309
- if (rect) {
1310
- ctx.translate(-rect.x, -rect.y);
1311
- }
1312
- // for canvas2svg
1313
- (_c = (_b = ctx).setAttrs) === null || _c === void 0 ? void 0 : _c.call(_b, pen);
1314
- // end
1315
- var lineGradientFlag = false;
1316
- var store = pen.calculative.canvas.store;
1317
- var textFlip = pen.textFlip || store.options.textFlip;
1318
- var textRotate = pen.textRotate || store.options.textRotate;
1319
- ctx.beginPath();
1320
- if (!textFlip || !textRotate) {
1321
- ctx.save();
1322
- }
1323
- if (pen.calculative.flipX) {
1324
- if (rect) {
1325
- ctx.translate(pen.calculative.worldRect.x + pen.calculative.worldRect.ex, 0);
1326
- }
1327
- else {
1328
- ctx.translate(pen.calculative.worldRect.x + pen.calculative.worldRect.ex, 0);
1329
- }
1330
- ctx.scale(-1, 1);
1331
- }
1332
- if (pen.calculative.flipY) {
1333
- if (rect) {
1334
- ctx.translate(0, pen.calculative.worldRect.y + pen.calculative.worldRect.ey);
1335
- }
1336
- else {
1337
- ctx.translate(0, pen.calculative.worldRect.y + pen.calculative.worldRect.ey);
1338
- }
1339
- ctx.scale(1, -1);
1340
- }
1341
- if (pen.calculative.rotate && pen.name !== 'line') {
1342
- ctxRotate(ctx, pen);
1343
- }
1344
- if (pen.calculative.lineWidth > 1 || download) {
1345
- ctx.lineWidth = pen.calculative.lineWidth;
1346
- }
1347
- var fill;
1348
- if (pen.calculative.hover) {
1349
- ctx.strokeStyle = pen.hoverColor || store.options.hoverColor;
1350
- ctx.fillStyle = pen.hoverBackground || store.options.hoverBackground;
1351
- fill = pen.hoverBackground || store.options.hoverBackground;
1352
- }
1353
- else if (pen.calculative.active) {
1354
- ctx.strokeStyle = pen.activeColor || store.options.activeColor;
1355
- ctx.fillStyle = pen.activeBackground || store.options.activeBackground;
1356
- fill = pen.activeBackground || store.options.activeBackground;
1357
- }
1358
- else {
1359
- if (pen.strokeImage) {
1360
- if (pen.calculative.strokeImg) {
1361
- ctx.strokeStyle = ctx.createPattern(pen.calculative.strokeImg, 'repeat');
1362
- fill = true;
1363
- }
1364
- }
1365
- else {
1366
- var stroke = void 0;
1367
- if (pen.calculative.strokeType && pen.calculative.lineGradientColors && pen.name === 'line') {
1368
- lineGradientFlag = true;
1369
- }
1370
- else {
1371
- stroke = pen.calculative.color || getGlobalColor(store);
1372
- }
1373
- ctx.strokeStyle = stroke;
1374
- }
1375
- if (pen.backgroundImage) {
1376
- if (pen.calculative.backgroundImg) {
1377
- ctx.fillStyle = ctx.createPattern(pen.calculative.backgroundImg, 'repeat');
1378
- fill = true;
1379
- }
1380
- }
1381
- else {
1382
- ctx.fillStyle = pen.background;
1383
- fill = !!pen.background;
1384
- }
1385
- }
1386
- setLineCap(ctx, pen);
1387
- setLineJoin(ctx, pen);
1388
- setGlobalAlpha(ctx, pen);
1389
- if (pen.calculative.lineDash) {
1390
- ctx.setLineDash(pen.calculative.lineDash);
1391
- }
1392
- if (pen.calculative.lineDashOffset) {
1393
- ctx.lineDashOffset = pen.calculative.lineDashOffset;
1394
- }
1395
- if (pen.calculative.shadowColor) {
1396
- ctx.shadowColor = pen.calculative.shadowColor;
1397
- ctx.shadowOffsetX = pen.calculative.shadowOffsetX;
1398
- ctx.shadowOffsetY = pen.calculative.shadowOffsetY;
1399
- ctx.shadowBlur = pen.calculative.shadowBlur;
1400
- }
1401
- if (lineGradientFlag) {
1402
- ctxDrawLinearGradientPath(ctx, pen);
1403
- ctxDrawLinePath(true, ctx, pen, store);
1404
- }
1405
- else {
1406
- ctxDrawPath(false, ctx, pen, store, fill);
1407
- ctxDrawCanvas(ctx, pen);
1408
- }
1409
- // renderPenRaw 用在 downloadPng svg , echarts 等图形需要
1410
- if (pen.calculative.img) {
1411
- ctx.save();
1412
- ctx.shadowColor = '';
1413
- ctx.shadowBlur = 0;
1414
- ctx.shadowOffsetX = 0;
1415
- ctx.shadowOffsetY = 0;
1416
- drawImage(ctx, pen);
1417
- ctx.restore();
1418
- }
1419
- else if (pen.calculative.icon) {
1420
- drawIcon(ctx, pen);
1421
- }
1422
- if (!textFlip || !textRotate) {
1423
- ctx.restore();
1424
- }
1425
- if (textFlip && !textRotate) {
1426
- if (pen.calculative.flipX) {
1427
- if (rect) {
1428
- ctx.translate(pen.calculative.worldRect.x + pen.calculative.worldRect.ex, 0);
1429
- }
1430
- else {
1431
- ctx.translate(pen.calculative.worldRect.x + pen.calculative.worldRect.ex, 0);
1432
- }
1433
- ctx.scale(-1, 1);
1434
- }
1435
- if (pen.calculative.flipY) {
1436
- if (rect) {
1437
- ctx.translate(0, pen.calculative.worldRect.y + pen.calculative.worldRect.ey);
1438
- }
1439
- else {
1440
- ctx.translate(0, pen.calculative.worldRect.y + pen.calculative.worldRect.ey);
1441
- }
1442
- ctx.scale(1, -1);
1443
- }
1444
- }
1445
- if (!textFlip && textRotate) {
1446
- if (pen.calculative.rotate && pen.name !== 'line') {
1447
- ctxRotate(ctx, pen, true);
1448
- }
1449
- }
1450
- drawText(ctx, pen);
1451
- if (pen.type === PenType.Line && pen.fillTexts) {
1452
- try {
1453
- for (var _d = __values(pen.fillTexts), _e = _d.next(); !_e.done; _e = _d.next()) {
1454
- var text = _e.value;
1455
- drawFillText(ctx, pen, text);
1456
- }
1457
- }
1458
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
1459
- finally {
1460
- try {
1461
- if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
1462
- }
1463
- finally { if (e_3) throw e_3.error; }
1464
- }
1465
- }
1466
- ctx.restore();
1467
- }
1468
- /**
1469
- * 根据 path2D 绘制 path
1470
- * @param canUsePath 是否可使用 Path2D, downloadSvg 不可使用 path2D
1471
- */
1472
- export function ctxDrawPath(canUsePath, ctx, pen, store, fill) {
1473
- if (canUsePath === void 0) { canUsePath = true; }
1474
- var path = canUsePath
1475
- ? store.path2dMap.get(pen)
1476
- : globalStore.path2dDraws[pen.name];
1477
- var path_from = null;
1478
- var path_to = null;
1479
- if (pen.type === PenType.Line) {
1480
- //线段的起始和结束线帽 分别配置
1481
- if (pen.fromLineCap && pen.fromLineCap !== 'butt') {
1482
- ctx.lineCap = 'butt';
1483
- path_from = new Path2D();
1484
- path_from.moveTo(pen.calculative.worldAnchors[0].x, pen.calculative.worldAnchors[0].y);
1485
- path_from.lineTo(pen.calculative.worldAnchors[0].x, pen.calculative.worldAnchors[0].y);
1486
- }
1487
- if (pen.toLineCap && pen.toLineCap !== 'butt') {
1488
- ctx.lineCap = 'butt';
1489
- path_to = new Path2D();
1490
- path_to.moveTo(pen.calculative.worldAnchors[pen.calculative.worldAnchors.length - 1].x, pen.calculative.worldAnchors[pen.calculative.worldAnchors.length - 1].y);
1491
- path_to.lineTo(pen.calculative.worldAnchors[pen.calculative.worldAnchors.length - 1].x, pen.calculative.worldAnchors[pen.calculative.worldAnchors.length - 1].y);
1492
- }
1493
- }
1494
- if (path) {
1495
- if (pen.type === PenType.Line && pen.borderWidth) {
1496
- ctx.save();
1497
- ctx.beginPath();
1498
- var lineWidth = pen.calculative.lineWidth + pen.calculative.borderWidth;
1499
- ctx.lineWidth = lineWidth;
1500
- ctx.strokeStyle = pen.borderColor;
1501
- if (path_from) {
1502
- ctx.save();
1503
- ctx.lineCap = pen.fromLineCap;
1504
- ctx.stroke(path_from);
1505
- ctx.restore();
1506
- }
1507
- if (path instanceof Path2D) {
1508
- fill && ctx.fill(path);
1509
- lineWidth && ctx.stroke(path);
1510
- }
1511
- else {
1512
- path(pen, ctx);
1513
- fill && ctx.fill();
1514
- lineWidth && ctx.stroke();
1515
- }
1516
- if (path_to) {
1517
- ctx.save();
1518
- ctx.lineCap = pen.toLineCap;
1519
- ctx.stroke(path_to);
1520
- ctx.restore();
1521
- }
1522
- ctx.restore();
1523
- }
1524
- if (path instanceof Path2D) {
1525
- if (pen.type) {
1526
- if (pen.close) {
1527
- fill && ctx.fill(path);
1528
- }
1529
- }
1530
- else {
1531
- //svgPath
1532
- fill && ctx.fill(path);
1533
- }
1534
- }
1535
- else {
1536
- ctx.save();
1537
- path(pen, ctx);
1538
- fill && ctx.fill();
1539
- ctx.restore();
1540
- }
1541
- var progress = pen.calculative.progress;
1542
- if (progress != null) {
1543
- // 从左往右 x, y, x + width * progress, y
1544
- // 从右往左 ex, y, x + width * (1-progress), y
1545
- // 从下往上 x, y, x, y + height * progress
1546
- // 从上往下 x, ey, x, y + height * (1 - progress)
1547
- ctx.save();
1548
- var _a = pen.calculative.worldRect, ex = _a.ex, x = _a.x, y = _a.y, width = _a.width, height = _a.height, ey = _a.ey;
1549
- var grd_1 = null;
1550
- if (!pen.verticalProgress) {
1551
- grd_1 = !pen.reverseProgress
1552
- ? ctx.createLinearGradient(x, y, x + width * progress, y)
1553
- : ctx.createLinearGradient(ex, y, x + width * (1 - progress), y);
1554
- }
1555
- else {
1556
- grd_1 = !pen.reverseProgress
1557
- ? ctx.createLinearGradient(x, ey, x, y + height * (1 - progress))
1558
- : ctx.createLinearGradient(x, y, x, y + height * progress);
1559
- }
1560
- if (pen.calculative.progressGradientColors) {
1561
- var colors = formatGradient(pen.calculative.progressGradientColors).colors;
1562
- colors.forEach(function (stop) {
1563
- grd_1.addColorStop(stop.i, stop.color);
1564
- });
1565
- }
1566
- else {
1567
- var color = pen.calculative.progressColor ||
1568
- pen.calculative.color ||
1569
- store.options.activeColor;
1570
- grd_1.addColorStop(0, color);
1571
- grd_1.addColorStop(1, color);
1572
- }
1573
- grd_1.addColorStop(1, 'transparent');
1574
- ctx.fillStyle = grd_1;
1575
- if (path instanceof Path2D) {
1576
- ctx.fill(path);
1577
- }
1578
- else {
1579
- path(pen, ctx);
1580
- ctx.fill();
1581
- }
1582
- ctx.restore();
1583
- }
1584
- if (pen.calculative.lineWidth) {
1585
- if (path instanceof Path2D) {
1586
- if (store.options.svgPathStroke || pen.name !== 'svgPath') {
1587
- if (path_from) {
1588
- ctx.save();
1589
- ctx.lineCap = pen.fromLineCap;
1590
- ctx.stroke(path_from);
1591
- ctx.restore();
1592
- }
1593
- ctx.stroke(path);
1594
- if (path_to) {
1595
- ctx.save();
1596
- ctx.lineCap = pen.toLineCap;
1597
- ctx.stroke(path_to);
1598
- ctx.restore();
1599
- }
1600
- }
1601
- }
1602
- else {
1603
- path(pen, ctx);
1604
- ctx.stroke();
1605
- }
1606
- }
1607
- if (pen.type) {
1608
- if (pen.calculative.animatePos) {
1609
- ctx.save();
1610
- setCtxLineAnimate(ctx, pen, store);
1611
- if (pen.lineAnimateType === LineAnimateType.Arrow || pen.lineAnimateType === LineAnimateType.WaterDrop) {
1612
- //箭头动画
1613
- var _path = drawArrow(pen, ctx);
1614
- if (_path instanceof Path2D) {
1615
- ctx.stroke(_path);
1616
- ctx.fill(_path);
1617
- }
1618
- else {
1619
- ctx.stroke();
1620
- ctx.fill();
1621
- }
1622
- }
1623
- else {
1624
- if (path instanceof Path2D) {
1625
- if (path_from && !pen.lineAnimateType) {
1626
- ctx.save();
1627
- ctx.lineCap = pen.fromLineCap;
1628
- ctx.stroke(path_from);
1629
- ctx.restore();
1630
- }
1631
- ctx.lineCap = pen.lineCap;
1632
- ctx.stroke(path);
1633
- }
1634
- else {
1635
- path(pen, ctx);
1636
- ctx.stroke();
1637
- }
1638
- }
1639
- ctx.restore();
1640
- }
1641
- pen.fromArrow && renderFromArrow(ctx, pen, store);
1642
- pen.toArrow && renderToArrow(ctx, pen, store);
1643
- if (pen.calculative.active && !pen.calculative.pencil && !store.options.disableAnchor && !store.data.locked) {
1644
- renderLineAnchors(ctx, pen);
1645
- }
1646
- }
1647
- }
1648
- }
1649
- /**
1650
- * 连线配置线条渐进后,动画效果、起始点、终点的绘制
1651
- */
1652
- export function ctxDrawLinePath(canUsePath, ctx, pen, store) {
1653
- if (canUsePath === void 0) { canUsePath = true; }
1654
- var path = canUsePath
1655
- ? store.path2dMap.get(pen)
1656
- : globalStore.path2dDraws[pen.name];
1657
- if (path) {
1658
- if (pen.type) {
1659
- if (pen.calculative.animatePos) {
1660
- ctx.save();
1661
- setCtxLineAnimate(ctx, pen, store);
1662
- ctx.beginPath();
1663
- if (path instanceof Path2D) {
1664
- if (pen.lineName === 'polyline' || pen.lineName === 'line') {
1665
- if (pen.lineAnimateType === LineAnimateType.Arrow || pen.lineAnimateType === LineAnimateType.WaterDrop) {
1666
- //箭头动画
1667
- var _path = drawArrow(pen);
1668
- ctx.stroke(_path);
1669
- ctx.fill(_path);
1670
- }
1671
- else {
1672
- if (pen.calculative.gradientSmooth || pen.calculative.lineSmooth) {
1673
- if (!pen.calculative.gradientAnimatePath) {
1674
- pen.calculative.gradientAnimatePath = getGradientAnimatePath(pen);
1675
- }
1676
- if (pen.calculative.gradientAnimatePath instanceof Path2D) {
1677
- ctx.stroke(pen.calculative.gradientAnimatePath);
1678
- }
1679
- }
1680
- else {
1681
- ctx.stroke(path);
1682
- }
1683
- }
1684
- }
1685
- else {
1686
- ctx.stroke(path);
1687
- }
1688
- }
1689
- else {
1690
- path(pen, ctx);
1691
- ctx.stroke();
1692
- }
1693
- ctx.restore();
1694
- }
1695
- pen.fromArrow && renderFromArrow(ctx, pen, store);
1696
- pen.toArrow && renderToArrow(ctx, pen, store);
1697
- //TODO 锚点处渐进色的过渡
1698
- if (pen.calculative.active && !pen.calculative.pencil && !store.options.disableAnchor && !store.data.locked) {
1699
- renderLineAnchors(ctx, pen);
1700
- }
1701
- }
1702
- }
1703
- }
1704
- /**
1705
- * 设置线条动画,ctx 的 strokeStyle lineDash 等属性更改
1706
- */
1707
- export function setCtxLineAnimate(ctx, pen, store) {
1708
- ctx.strokeStyle = pen.animateColor || store.options.animateColor;
1709
- if (pen.animateShadow) {
1710
- ctx.shadowBlur = pen.animateShadowBlur || pen.animateLineWidth || 6;
1711
- ctx.shadowColor = pen.animateShadowColor || pen.animateColor || store.options.animateColor;
1712
- }
1713
- pen.calculative.animateLineWidth &&
1714
- (ctx.lineWidth = pen.calculative.animateLineWidth * store.data.scale);
1715
- var len = 0;
1716
- switch (pen.lineAnimateType) {
1717
- case LineAnimateType.Beads:
1718
- if (pen.animateReverse) {
1719
- ctx.lineDashOffset = pen.calculative.animatePos;
1720
- }
1721
- else {
1722
- ctx.lineDashOffset = pen.length - pen.calculative.animatePos;
1723
- }
1724
- len = pen.calculative.lineWidth || 5;
1725
- if (len < 5) {
1726
- len = 5;
1727
- }
1728
- var dash = pen.animateLineDash &&
1729
- pen.animateLineDash.map(function (item) { return (item * len) / 5; });
1730
- ctx.setLineDash(dash || [len, len * 2]);
1731
- break;
1732
- case LineAnimateType.Dot:
1733
- if (pen.animateReverse) {
1734
- ctx.lineDashOffset = pen.calculative.animatePos;
1735
- }
1736
- else {
1737
- ctx.lineDashOffset = pen.length - pen.calculative.animatePos;
1738
- }
1739
- len =
1740
- pen.calculative.animateDotSize || pen.calculative.lineWidth * 2 || 6;
1741
- if (len < 6) {
1742
- len = 6;
1743
- }
1744
- if (len > 40) {
1745
- len = 40;
1746
- }
1747
- ctx.lineWidth =
1748
- (pen.calculative.animateLineWidth || len) * store.data.scale;
1749
- ctx.setLineDash([0.1, pen.length]);
1750
- break;
1751
- case LineAnimateType.Arrow:
1752
- ctx.fillStyle = pen.animateColor || store.options.animateColor;
1753
- ctx.lineWidth = 1;
1754
- break;
1755
- case LineAnimateType.WaterDrop:
1756
- ctx.fillStyle = pen.animateColor || store.options.animateColor;
1757
- ctx.lineWidth = 1;
1758
- break;
1759
- default:
1760
- if (pen.animateReverse) {
1761
- ctx.lineDashOffset = Number.EPSILON; //防止在执行动画时会绘制多余的远点
1762
- ctx.setLineDash([
1763
- 0,
1764
- pen.length - pen.calculative.animatePos + 1,
1765
- pen.calculative.animatePos,
1766
- ]);
1767
- }
1768
- else {
1769
- ctx.setLineDash([
1770
- pen.calculative.animatePos,
1771
- pen.length + 0.01 - pen.calculative.animatePos, //避免在缩放时,精度问题绘制多余圆点
1772
- ]);
1773
- }
1774
- break;
1775
- }
1776
- }
1777
- /**
1778
- * 全局 color
1779
- */
1780
- export function getGlobalColor(store) {
1781
- var data = store.data, options = store.options;
1782
- return data.color || options.color;
1783
- }
1784
- export function renderLineAnchors(ctx, pen) {
1785
- var store = pen.calculative.canvas.store;
1786
- ctx.save();
1787
- ctx.lineWidth = 1;
1788
- ctx.fillStyle = pen.activeColor || store.options.activeColor;
1789
- pen.calculative.worldAnchors.forEach(function (pt) {
1790
- !pt.hidden && !pt.isTemp && renderAnchor(ctx, pt, pen);
1791
- });
1792
- ctx.restore();
1793
- }
1794
- export function renderAnchor(ctx, pt, pen) {
1795
- if (!pt) {
1796
- return;
1797
- }
1798
- var active = pen.calculative.canvas.store.activeAnchor ===
1799
- pen.calculative.activeAnchor && pen.calculative.activeAnchor === pt;
1800
- var r = 3;
1801
- if (pen.calculative.lineWidth > 3) {
1802
- r = pen.calculative.lineWidth;
1803
- }
1804
- if (pen.anchorRadius) {
1805
- r = pen.anchorRadius;
1806
- }
1807
- if (pt.radius) {
1808
- r = pt.radius;
1809
- }
1810
- if (active) {
1811
- if (pt.prev) {
1812
- ctx.save();
1813
- ctx.strokeStyle = '#4dffff';
1814
- ctx.beginPath();
1815
- ctx.moveTo(pt.prev.x, pt.prev.y);
1816
- ctx.lineTo(pt.x, pt.y);
1817
- ctx.stroke();
1818
- ctx.restore();
1819
- ctx.save();
1820
- ctx.fillStyle = '#ffffff';
1821
- ctx.beginPath();
1822
- ctx.arc(pt.prev.x, pt.prev.y, r, 0, Math.PI * 2);
1823
- ctx.fill();
1824
- ctx.stroke();
1825
- ctx.restore();
1826
- }
1827
- if (pt.next) {
1828
- ctx.save();
1829
- ctx.strokeStyle = '#4dffff';
1830
- ctx.beginPath();
1831
- ctx.moveTo(pt.x, pt.y);
1832
- ctx.lineTo(pt.next.x, pt.next.y);
1833
- ctx.stroke();
1834
- ctx.restore();
1835
- ctx.save();
1836
- ctx.fillStyle = '#ffffff';
1837
- ctx.beginPath();
1838
- ctx.arc(pt.next.x, pt.next.y, r, 0, Math.PI * 2);
1839
- ctx.fill();
1840
- ctx.stroke();
1841
- ctx.restore();
1842
- ctx.beginPath();
1843
- ctx.arc(pt.x, pt.y, r, 0, Math.PI * 2);
1844
- ctx.fill();
1845
- ctx.stroke();
1846
- }
1847
- ctx.beginPath();
1848
- ctx.arc(pt.x, pt.y, r, 0, Math.PI * 2);
1849
- ctx.fill();
1850
- ctx.stroke();
1851
- }
1852
- else {
1853
- ctx.save();
1854
- ctx.fillStyle = '#ffffff';
1855
- ctx.beginPath();
1856
- ctx.arc(pt.x, pt.y, r, 0, Math.PI * 2);
1857
- ctx.fill();
1858
- ctx.stroke();
1859
- ctx.restore();
1860
- }
1861
- }
1862
- export function calcWorldRects(pen) {
1863
- var store = pen.calculative.canvas.store;
1864
- var rect = {
1865
- x: pen.x,
1866
- y: pen.y,
1867
- };
1868
- if (!pen.parentId || (pen.parentId && !store.pens[pen.parentId])) {
1869
- pen.parentId = undefined;
1870
- rect.width = pen.width;
1871
- rect.height = pen.height;
1872
- rect.rotate = pen.rotate;
1873
- calcRightBottom(rect);
1874
- calcCenter(rect);
1875
- if (pen.pivot) {
1876
- calcPivot(rect, pen.pivot);
1877
- }
1878
- }
1879
- else {
1880
- var parent_1 = store.pens[pen.parentId];
1881
- var parentRect = parent_1.calculative.worldRect;
1882
- if (!parentRect) {
1883
- parentRect = calcWorldRects(parent_1);
1884
- }
1885
- rect.x = parentRect.x + parentRect.width * pen.x;
1886
- rect.y = parentRect.y + parentRect.height * pen.y;
1887
- rect.width = parentRect.width * pen.width;
1888
- rect.height = parentRect.height * pen.height;
1889
- if (parent_1.flipX) {
1890
- rect.x =
1891
- parentRect.width - (rect.x - parentRect.x + rect.width) + parentRect.x;
1892
- }
1893
- if (parent_1.flipY) {
1894
- rect.y =
1895
- parentRect.height -
1896
- (rect.y - parentRect.y + rect.height) +
1897
- parentRect.y;
1898
- }
1899
- calcRightBottom(rect);
1900
- rect.rotate = parentRect.rotate + pen.rotate;
1901
- calcCenter(rect);
1902
- if (pen.pivot) {
1903
- calcPivot(rect, pen.pivot);
1904
- }
1905
- }
1906
- pen.calculative.worldRect = rect;
1907
- // 这里的 rect 均是绝对值
1908
- calcPadding(pen, rect);
1909
- return rect;
1910
- }
1911
- export function calcPadding(pen, rect) {
1912
- !pen.paddingTop && (pen.calculative.paddingTop = 0);
1913
- !pen.paddingBottom && (pen.calculative.paddingBottom = 0);
1914
- !pen.paddingLeft && (pen.calculative.paddingLeft = 0);
1915
- !pen.paddingRight && (pen.calculative.paddingRight = 0);
1916
- Math.abs(pen.calculative.paddingTop) < 1 &&
1917
- (pen.calculative.paddingTop *= rect.height);
1918
- Math.abs(pen.calculative.paddingBottom) < 1 &&
1919
- (pen.calculative.paddingBottom *= rect.height);
1920
- Math.abs(pen.calculative.paddingLeft) < 1 &&
1921
- (pen.calculative.paddingLeft *= rect.width);
1922
- Math.abs(pen.calculative.paddingRight) < 1 &&
1923
- (pen.calculative.paddingRight *= rect.width);
1924
- }
1925
- export function calcPenRect(pen) {
1926
- var worldRect = deepClone(pen.calculative.worldRect);
1927
- delete worldRect.pivot;
1928
- if (!pen.parentId) {
1929
- Object.assign(pen, worldRect);
1930
- return;
1931
- }
1932
- var store = pen.calculative.canvas.store;
1933
- var parentRect = store.pens[pen.parentId].calculative.worldRect;
1934
- Object.assign(pen, calcRelativeRect(worldRect, parentRect));
1935
- }
1936
- export function calcWorldAnchors(pen) {
1937
- var store = pen.calculative.canvas.store;
1938
- var anchors = [];
1939
- if (pen.anchors) {
1940
- var _anchors = deepClone(pen.anchors);
1941
- if (pen.flipX) {
1942
- _anchors.forEach(function (anchor) {
1943
- anchor.x = 0.5 - (anchor.x - 0.5);
1944
- });
1945
- }
1946
- if (pen.flipY) {
1947
- _anchors.forEach(function (anchor) {
1948
- anchor.y = 0.5 - (anchor.y - 0.5);
1949
- });
1950
- }
1951
- _anchors.forEach(function (anchor) {
1952
- anchors.push(calcWorldPointOfPen(pen, anchor));
1953
- });
1954
- }
1955
- // Default anchors of node
1956
- if (!anchors.length &&
1957
- !pen.type &&
1958
- !pen.calculative.canvas.parent.isCombine(pen)) {
1959
- var _a = pen.calculative.worldRect, x_1 = _a.x, y_1 = _a.y, width_1 = _a.width, height_1 = _a.height;
1960
- anchors = store.options.defaultAnchors.map(function (anchor, index) {
1961
- return {
1962
- id: "" + index,
1963
- penId: pen.id,
1964
- x: x_1 + width_1 * anchor.x,
1965
- y: y_1 + height_1 * anchor.y,
1966
- };
1967
- });
1968
- }
1969
- if (pen.calculative.rotate) {
1970
- anchors.forEach(function (anchor) {
1971
- rotatePoint(anchor, pen.calculative.rotate, pen.calculative.worldRect.pivot || pen.calculative.worldRect.center);
1972
- });
1973
- }
1974
- if (!pen.type || pen.anchors) {
1975
- pen.calculative.worldAnchors = anchors;
1976
- }
1977
- if (pen.calculative.activeAnchor && anchors.length) {
1978
- pen.calculative.activeAnchor = anchors.find(function (a) {
1979
- a.id === pen.calculative.activeAnchor.id;
1980
- });
1981
- }
1982
- pen.calculative.gradientAnimatePath = undefined;
1983
- }
1984
- export function calcChildrenInitRect(pen) {
1985
- var _a;
1986
- // 重新计算子节点初始化坐标
1987
- if ((_a = pen.children) === null || _a === void 0 ? void 0 : _a.length) {
1988
- var parentRect_1 = pen.calculative.worldRect;
1989
- pen.children.forEach(function (id) {
1990
- var child = pen.calculative.canvas.store.pens[id];
1991
- if (child.calculative.initRect && child.calculative.initRelativeRect) {
1992
- child.calculative.initRect.x =
1993
- parentRect_1.x +
1994
- parentRect_1.width * child.calculative.initRelativeRect.x;
1995
- child.calculative.initRect.y =
1996
- parentRect_1.y +
1997
- parentRect_1.height * child.calculative.initRelativeRect.y;
1998
- child.calculative.initRect.ex =
1999
- child.calculative.initRect.x +
2000
- parentRect_1.width * child.calculative.initRelativeRect.width;
2001
- child.calculative.initRect.ey =
2002
- child.calculative.initRect.y +
2003
- parentRect_1.height +
2004
- child.calculative.initRelativeRect.height;
2005
- calcCenter(child.calculative.initRect);
2006
- }
2007
- calcChildrenInitRect(child);
2008
- });
2009
- }
2010
- }
2011
- export function calcWorldPointOfPen(pen, pt) {
2012
- var p = __assign({}, pt);
2013
- var _a = pen.calculative.worldRect, x = _a.x, y = _a.y, width = _a.width, height = _a.height;
2014
- p.x = x + width * pt.x;
2015
- p.y = y + height * pt.y;
2016
- if (pt.prev) {
2017
- p.prev = {
2018
- penId: pen.id,
2019
- connectTo: pt.prev.connectTo,
2020
- x: x + width * pt.prev.x,
2021
- y: y + height * pt.prev.y,
2022
- };
2023
- }
2024
- if (pt.next) {
2025
- p.next = {
2026
- penId: pen.id,
2027
- connectTo: pt.next.connectTo,
2028
- x: x + width * pt.next.x,
2029
- y: y + height * pt.next.y,
2030
- };
2031
- }
2032
- return p;
2033
- }
2034
- export function calcIconRect(pens, pen) {
2035
- var _a = pen.calculative, paddingTop = _a.paddingTop, paddingBottom = _a.paddingBottom, paddingLeft = _a.paddingLeft, paddingRight = _a.paddingRight;
2036
- var x = paddingLeft;
2037
- var y = paddingTop;
2038
- var width = pen.calculative.worldRect.width - paddingLeft - paddingRight;
2039
- var height = pen.calculative.worldRect.height - paddingTop - paddingBottom;
2040
- var iconLeft = pen.calculative.iconLeft;
2041
- var iconTop = pen.calculative.iconTop;
2042
- if (iconLeft && Math.abs(iconLeft) < 1) {
2043
- iconLeft = pen.calculative.worldRect.width * iconLeft;
2044
- }
2045
- if (iconTop && Math.abs(iconTop) < 1) {
2046
- iconTop = pen.calculative.worldRect.height * iconTop;
2047
- }
2048
- x += iconLeft || 0;
2049
- y += iconTop || 0;
2050
- width -= iconLeft || 0;
2051
- height -= iconTop || 0;
2052
- var rotate = pen.calculative.iconRotate || 0;
2053
- if (pen.parentId) {
2054
- var parentPen = pens[pen.parentId].calculative;
2055
- if (parentPen) {
2056
- rotate += parentPen.rotate;
2057
- rotate %= 360;
2058
- }
2059
- }
2060
- x = pen.calculative.worldRect.x + x;
2061
- y = pen.calculative.worldRect.y + y;
2062
- pen.calculative.worldIconRect = {
2063
- x: x,
2064
- y: y,
2065
- width: width,
2066
- height: height,
2067
- rotate: rotate,
2068
- };
2069
- calcRightBottom(pen.calculative.worldIconRect);
2070
- calcCenter(pen.calculative.worldIconRect);
2071
- }
2072
- export function scalePen(pen, scale, center) {
2073
- scaleRect(pen.calculative.worldRect, scale, center, pen.pivot);
2074
- if (pen.calculative.initRect) {
2075
- scaleRect(pen.calculative.initRect, scale, center, pen.pivot);
2076
- }
2077
- scaleChildrenInitRect(pen, scale, center);
2078
- if (pen.calculative.x) {
2079
- scalePoint(pen.calculative, scale, center);
2080
- }
2081
- if (pen.type) {
2082
- calcWorldAnchors(pen);
2083
- }
2084
- }
2085
- export function scaleChildrenInitRect(pen, scale, center) {
2086
- var _a;
2087
- if (!pen) {
2088
- return;
2089
- }
2090
- if ((_a = pen.children) === null || _a === void 0 ? void 0 : _a.length) {
2091
- pen.children.forEach(function (id) {
2092
- var child = pen.calculative.canvas.store.pens[id];
2093
- if (child) {
2094
- if (child.calculative.initRect) {
2095
- scaleRect(child.calculative.initRect, scale, center);
2096
- }
2097
- scaleChildrenInitRect(child, scale, center);
2098
- }
2099
- });
2100
- }
2101
- }
2102
- export function pushPenAnchor(pen, pt) {
2103
- if (!pen.anchors) {
2104
- pen.anchors = [];
2105
- }
2106
- if (!pen.calculative.worldAnchors) {
2107
- pen.calculative.worldAnchors = [];
2108
- }
2109
- var worldAnchor = {
2110
- id: pt.id,
2111
- penId: pen.id,
2112
- x: pt.x,
2113
- y: pt.y,
2114
- };
2115
- pen.calculative.worldAnchors.push(worldAnchor);
2116
- if (pen.calculative.worldRect) {
2117
- if (pen.rotate % 360) {
2118
- rotatePoint(pt, -pen.rotate, pen.calculative.worldRect.center);
2119
- }
2120
- var anchor = {
2121
- id: pt.id,
2122
- penId: pen.id,
2123
- x: (pt.x - pen.calculative.worldRect.x) / pen.calculative.worldRect.width,
2124
- y: (pt.y - pen.calculative.worldRect.y) / pen.calculative.worldRect.height,
2125
- };
2126
- pen.anchors.push(anchor);
2127
- }
2128
- return worldAnchor;
2129
- }
2130
- export function addLineAnchor(pen, pt, index) {
2131
- if (!pen.anchors) {
2132
- pen.anchors = [];
2133
- }
2134
- if (!pen.calculative.worldAnchors) {
2135
- pen.calculative.worldAnchors = [];
2136
- }
2137
- var worldAnchor = getSplitAnchor(pen, pt, index);
2138
- pen.calculative.worldAnchors.splice(index + 1, 0, worldAnchor);
2139
- pen.anchors.splice(index + 1, 0, calcRelativePoint(worldAnchor, pen.calculative.worldRect));
2140
- pen.calculative.activeAnchor = worldAnchor;
2141
- return worldAnchor;
2142
- }
2143
- export function removePenAnchor(pen, anchor) {
2144
- if (!pen || !pen.calculative.worldAnchors) {
2145
- return;
2146
- }
2147
- var i = pen.calculative.worldAnchors.findIndex(function (a) { return a.id === anchor.id; });
2148
- if (i > -1) {
2149
- pen.calculative.worldAnchors.splice(i, 1);
2150
- }
2151
- i = pen.anchors.findIndex(function (a) { return a.id === anchor.id; });
2152
- if (i > -1) {
2153
- pen.anchors.splice(i, 1);
2154
- }
2155
- }
2156
- export function facePen(pt, pen) {
2157
- if (!pen || !pen.calculative || !pen.calculative.worldRect.center) {
2158
- return Direction.None;
2159
- }
2160
- if (pt.anchorId) {
2161
- var anchor = pen.anchors.filter(function (_anchor) { return _anchor.id === pt.anchorId; });
2162
- if (anchor.length) {
2163
- if (anchor[0].direction > -1) {
2164
- return anchor[0].direction;
2165
- }
2166
- }
2167
- }
2168
- return facePoint(pt, pen.calculative.worldRect.center);
2169
- }
2170
- export function nearestAnchor(pen, pt) {
2171
- var dis = Infinity;
2172
- var anchor;
2173
- pen.calculative.worldAnchors.forEach(function (a) {
2174
- var d = distance(pt, a);
2175
- if (dis > d) {
2176
- dis = d;
2177
- anchor = a;
2178
- }
2179
- });
2180
- return anchor;
2181
- }
2182
- export function translateLine(pen, x, y) {
2183
- pen.x += x;
2184
- pen.y += y;
2185
- if (pen.anchors) {
2186
- pen.anchors.forEach(function (a) {
2187
- translatePoint(a, x, y);
2188
- });
2189
- }
2190
- if (pen.calculative.worldAnchors) {
2191
- pen.calculative.worldAnchors.forEach(function (a) {
2192
- translatePoint(a, x, y);
2193
- });
2194
- }
2195
- }
2196
- export function deleteTempAnchor(pen) {
2197
- if (pen && pen.calculative && pen.calculative.worldAnchors.length) {
2198
- var to = getToAnchor(pen);
2199
- // 第一次画线
2200
- if (!pen.anchors || !pen.anchors.length) {
2201
- while (pen.calculative.worldAnchors.length &&
2202
- to !== pen.calculative.activeAnchor) {
2203
- pen.calculative.worldAnchors.pop();
2204
- to = getToAnchor(pen);
2205
- }
2206
- }
2207
- // 拖拽终点
2208
- else if (to === pen.calculative.activeAnchor) {
2209
- pen.calculative.worldAnchors = [pen.calculative.worldAnchors[0]];
2210
- }
2211
- // 拖拽起点
2212
- else if (pen.calculative.worldAnchors[0] === pen.calculative.activeAnchor) {
2213
- pen.calculative.worldAnchors = [
2214
- pen.calculative.worldAnchors[pen.calculative.worldAnchors.length - 1],
2215
- ];
2216
- }
2217
- }
2218
- }
2219
- /**
2220
- * 添加line到pen的connectedLines中,并关联相关属性
2221
- * 不添加连线到画布中,请确保画布中已经有该连线。
2222
- * */
2223
- export function connectLine(pen, anchor, line, lineAnchor) {
2224
- var _a, _b, _c, _d, _e, _f;
2225
- if (!pen ||
2226
- !anchor ||
2227
- !line ||
2228
- !lineAnchor ||
2229
- anchor.twoWay === TwoWay.DisableConnected ||
2230
- anchor.twoWay === TwoWay.Disable ||
2231
- lineAnchor.twoWay === TwoWay.DisableConnectTo ||
2232
- lineAnchor.twoWay === TwoWay.Disable) {
2233
- return;
2234
- }
2235
- if (anchor.twoWay === TwoWay.In) {
2236
- if (line.calculative.worldAnchors.length === 1) {
2237
- return;
2238
- }
2239
- var to = getToAnchor(line);
2240
- if (lineAnchor.id !== to.id) {
2241
- return;
2242
- }
2243
- }
2244
- if (anchor.twoWay === TwoWay.Out) {
2245
- var from = getFromAnchor(line);
2246
- if (lineAnchor.id !== from.id) {
2247
- return;
2248
- }
2249
- }
2250
- if (lineAnchor.connectTo === pen.id && lineAnchor.anchorId === anchor.id) {
2251
- return;
2252
- }
2253
- if (lineAnchor.connectTo) {
2254
- var p = pen.calculative.canvas.store.pens[lineAnchor.connectTo];
2255
- disconnectLine(p, getAnchor(p, lineAnchor.anchorId), line, lineAnchor);
2256
- }
2257
- if (!pen.connectedLines) {
2258
- pen.connectedLines = [];
2259
- }
2260
- var i = pen.connectedLines.findIndex(function (item) {
2261
- return item.lineId === line.id &&
2262
- item.lineAnchor === lineAnchor.id &&
2263
- item.anchor === anchor.id;
2264
- });
2265
- if (i < 0) {
2266
- pen.connectedLines.push({
2267
- lineId: line.id,
2268
- lineAnchor: lineAnchor.id,
2269
- anchor: anchor.id,
2270
- });
2271
- }
2272
- lineAnchor.connectTo = pen.id;
2273
- lineAnchor.anchorId = anchor.id;
2274
- // 如果两条连线,则相互关联
2275
- if (pen.type) {
2276
- connectLine(line, lineAnchor, pen, anchor);
2277
- }
2278
- pen.calculative.canvas.store.emitter.emit('connectLine', {
2279
- line: line,
2280
- lineAnchor: lineAnchor,
2281
- pen: pen,
2282
- anchor: anchor,
2283
- });
2284
- // 新增连线生命周期
2285
- var fromPen = (((_a = line.calculative.worldAnchors) === null || _a === void 0 ? void 0 : _a.length) >= 2) ? (_b = line.calculative.worldAnchors) === null || _b === void 0 ? void 0 : _b[0].connectTo : undefined;
2286
- var fromAnchor = (((_c = line.calculative.worldAnchors) === null || _c === void 0 ? void 0 : _c.length) >= 2) ? (_e = line.calculative.canvas.store.pens[(_d = line.calculative.worldAnchors) === null || _d === void 0 ? void 0 : _d[0].connectTo]) === null || _e === void 0 ? void 0 : _e.anchors.find(function (i) { var _a; return i.id === ((_a = line.calculative.worldAnchors) === null || _a === void 0 ? void 0 : _a[0].anchorId); }) : undefined; // num
2287
- (_f = pen.onConnectLine) === null || _f === void 0 ? void 0 : _f.call(// num
2288
- pen, pen, { line: line, lineAnchor: lineAnchor, pen: pen, anchor: anchor, fromPen: fromPen, fromAnchor: fromAnchor });
2289
- return true;
2290
- }
2291
- /**
2292
- * 从 pen.connectedLines 中删除 lineId 和 lineAnchor
2293
- */
2294
- export function disconnectLine(pen, anchor, line, lineAnchor) {
2295
- if (!pen || !anchor || !line || !lineAnchor) {
2296
- return;
2297
- }
2298
- if (!pen.connectedLines || !pen.connectedLines.length) {
2299
- return;
2300
- }
2301
- if (!line.lastConnected) {
2302
- line.lastConnected = {};
2303
- }
2304
- if (!line.lastConnected[pen.id]) {
2305
- line.lastConnected[pen.id] = deepClone(pen.connectedLines);
2306
- }
2307
- pen.connectedLines.forEach(function (item, index, arr) {
2308
- if ((item.lineId === line.id || item.lineId === line.id) &&
2309
- item.lineAnchor === lineAnchor.id &&
2310
- item.anchor === anchor.id) {
2311
- arr.splice(index, 1);
2312
- }
2313
- });
2314
- lineAnchor.connectTo = undefined;
2315
- lineAnchor.anchorId = undefined;
2316
- // 如果两条连线相互关联,则都取消关联
2317
- if (pen.type &&
2318
- anchor.connectTo === line.id &&
2319
- anchor.anchorId === lineAnchor.id) {
2320
- disconnectLine(line, lineAnchor, pen, anchor);
2321
- }
2322
- pen.calculative.canvas.store.emitter.emit('disconnectLine', {
2323
- line: line,
2324
- lineAnchor: lineAnchor,
2325
- pen: pen,
2326
- anchor: anchor,
2327
- });
2328
- return true;
2329
- }
2330
- export function getAnchor(pen, anchorId) {
2331
- var _a;
2332
- if (!pen || !anchorId) {
2333
- return;
2334
- }
2335
- return (_a = pen.calculative.worldAnchors) === null || _a === void 0 ? void 0 : _a.find(function (item) { return item.id === anchorId; });
2336
- }
2337
- export function getFromAnchor(pen) {
2338
- if (!pen || !pen.calculative.worldAnchors) {
2339
- return;
2340
- }
2341
- return pen.calculative.worldAnchors[0];
2342
- }
2343
- export function getToAnchor(pen) {
2344
- if (!pen || !pen.calculative.worldAnchors) {
2345
- return;
2346
- }
2347
- return pen.calculative.worldAnchors[pen.calculative.worldAnchors.length - 1];
2348
- }
2349
- export function setNodeAnimate(pen, now) {
2350
- var e_4, _a, e_5, _b;
2351
- var _c, _d;
2352
- if (pen.calculative.start === 0 || !pen.frames || !pen.frames.length) {
2353
- pen.calculative.start = undefined;
2354
- return 0;
2355
- }
2356
- if (!pen.calculative.duration) {
2357
- pen.calculative.duration = 0;
2358
- try {
2359
- for (var _e = __values(pen.frames), _f = _e.next(); !_f.done; _f = _e.next()) {
2360
- var f = _f.value;
2361
- pen.calculative.duration += f.duration;
2362
- for (var k in f) {
2363
- if (k !== 'duration' && !pen[k]) {
2364
- if (k === 'scale') {
2365
- pen[k] = 1;
2366
- }
2367
- }
2368
- }
2369
- }
2370
- }
2371
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
2372
- finally {
2373
- try {
2374
- if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
2375
- }
2376
- finally { if (e_4) throw e_4.error; }
2377
- }
2378
- }
2379
- if (!pen.animateCycle) {
2380
- pen.animateCycle = Infinity;
2381
- }
2382
- if (!pen.calculative.start) {
2383
- pen.calculative.start = now;
2384
- pen.calculative.frameIndex = 0;
2385
- pen.calculative.frameStart = pen.calculative.start;
2386
- pen.calculative.frameDuration = pen.frames[0].duration;
2387
- pen.calculative.frameEnd =
2388
- pen.calculative.frameStart + pen.calculative.frameDuration;
2389
- pen.calculative.cycleIndex = 1;
2390
- pen.calculative.x = pen.calculative.worldRect.x;
2391
- pen.calculative.y = pen.calculative.worldRect.y;
2392
- pen.calculative.initRect = deepClone(pen.calculative.worldRect);
2393
- if (pen.parentId) {
2394
- pen.calculative.initRelativeRect = {
2395
- x: pen.x,
2396
- y: pen.y,
2397
- width: pen.width,
2398
- height: pen.height,
2399
- };
2400
- }
2401
- if ((_c = pen.children) === null || _c === void 0 ? void 0 : _c.length) {
2402
- var store_1 = pen.calculative.canvas.store;
2403
- pen.calculative.childrenVisible = {};
2404
- pen.children.forEach(function (id) {
2405
- pen.calculative.childrenVisible[id] = store_1.pens[id].visible;
2406
- });
2407
- }
2408
- pen.calculative.initRect.rotate = pen.calculative.rotate || 0;
2409
- initPrevFrame(pen);
2410
- }
2411
- else {
2412
- var frameIndex = 0;
2413
- var cycleIndex = Math.ceil((now - pen.calculative.start) / pen.calculative.duration);
2414
- // 播放结束
2415
- if (cycleIndex > pen.animateCycle) {
2416
- pen.currentAnimation = undefined;
2417
- pen.calculative.start = undefined;
2418
- setNodeAnimateProcess(pen, 1);
2419
- return 0;
2420
- }
2421
- var pos = (now - pen.calculative.start) % pen.calculative.duration;
2422
- var d = 0;
2423
- try {
2424
- for (var _g = __values(pen.frames), _h = _g.next(); !_h.done; _h = _g.next()) {
2425
- var frame = _h.value;
2426
- d += frame.duration;
2427
- if (pos > d) {
2428
- ++frameIndex;
2429
- }
2430
- else {
2431
- break;
2432
- }
2433
- }
2434
- }
2435
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
2436
- finally {
2437
- try {
2438
- if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
2439
- }
2440
- finally { if (e_5) throw e_5.error; }
2441
- }
2442
- // 帧超出
2443
- if (!pen.frames[frameIndex]) {
2444
- return true;
2445
- }
2446
- pen.calculative.frameDuration = pen.frames[frameIndex].duration;
2447
- pen.calculative.frameStart =
2448
- pen.calculative.start + pen.calculative.duration * (cycleIndex - 1);
2449
- pen.calculative.frameEnd =
2450
- pen.calculative.frameStart + pen.calculative.frameDuration;
2451
- // 换帧
2452
- var frameChanged = frameIndex !== pen.calculative.frameIndex;
2453
- // 新循环播放
2454
- var cycleChanged = cycleIndex > pen.calculative.cycleIndex;
2455
- frameChanged && (pen.calculative.frameIndex = frameIndex);
2456
- cycleChanged && (pen.calculative.cycleIndex = cycleIndex);
2457
- if (frameChanged || cycleChanged) {
2458
- // 以初始位置为参考点。因为网页在后台时,不执行动画帧,网页恢复显示时,位置不确定
2459
- pen.calculative.x = pen.calculative.initRect.x;
2460
- pen.calculative.y = pen.calculative.initRect.y;
2461
- if (((_d = pen.children) === null || _d === void 0 ? void 0 : _d.length) && !pen.parentId) {
2462
- pen.calculative.canvas.rotatePen(pen, (pen.calculative.initRect.rotate || 0) - (pen.calculative.rotate || 0), pen.calculative.initRect);
2463
- }
2464
- else {
2465
- pen.calculative.rotate = pen.calculative.initRect.rotate || 0;
2466
- }
2467
- if (frameIndex > 0) {
2468
- pen.prevFrame = {};
2469
- var prevFrame = pen.frames[frameIndex - 1];
2470
- for (var k in prevFrame) {
2471
- pen.prevFrame[k] = prevFrame[k];
2472
- }
2473
- Object.assign(pen.prevFrame, {
2474
- rotate: prevFrame.rotate || 0,
2475
- x: prevFrame.x || 0,
2476
- y: prevFrame.y || 0,
2477
- scale: prevFrame.scale || 1,
2478
- });
2479
- }
2480
- else {
2481
- initPrevFrame(pen);
2482
- }
2483
- }
2484
- }
2485
- var process = ((now - pen.calculative.frameStart) / pen.calculative.frameDuration) % 1;
2486
- setNodeAnimateProcess(pen, process);
2487
- return true;
2488
- }
2489
- // 把前一个动画帧初始化为播放前状态
2490
- export function initPrevFrame(pen) {
2491
- pen.prevFrame = {};
2492
- for (var k in pen) {
2493
- if (typeof pen[k] !== 'object' || k === 'lineDash') {
2494
- pen.prevFrame[k] = pen[k];
2495
- }
2496
- }
2497
- pen.prevFrame.rotate = 0;
2498
- pen.prevFrame.x = 0;
2499
- pen.prevFrame.y = 0;
2500
- pen.prevFrame.scale = 1;
2501
- }
2502
- // 根据process进度值(纯小数),计算节点动画属性
2503
- export function setNodeAnimateProcess(pen, process) {
2504
- var _a, _b;
2505
- if (process < 0) {
2506
- return;
2507
- }
2508
- if (process > 1) {
2509
- process = 1;
2510
- }
2511
- var frame = pen.frames[pen.calculative.frameIndex];
2512
- for (var k in frame) {
2513
- if (k === 'duration') {
2514
- continue;
2515
- }
2516
- else if (k === 'scale') {
2517
- pen.calculative.worldRect = deepClone(pen.calculative.initRect);
2518
- scaleRect(pen.calculative.worldRect, pen.prevFrame.scale, pen.calculative.worldRect.center);
2519
- var newScale = pen.prevFrame.scale + (frame[k] - pen.prevFrame.scale) * process;
2520
- scaleRect(pen.calculative.worldRect, newScale / pen.prevFrame.scale, pen.calculative.worldRect.center);
2521
- pen.calculative.patchFlags = true;
2522
- }
2523
- else if (k === 'x') {
2524
- var lastVal = getFrameValue(pen, k, pen.calculative.frameIndex);
2525
- pen.calculative.worldRect.x = pen.calculative.initRect.x + lastVal;
2526
- pen.calculative.worldRect.ex = pen.calculative.initRect.ex + lastVal;
2527
- translateRect(pen.calculative.worldRect, frame[k] * process * pen.calculative.canvas.store.data.scale, 0);
2528
- pen.calculative.patchFlags = true;
2529
- }
2530
- else if (k === 'y') {
2531
- var lastVal = getFrameValue(pen, k, pen.calculative.frameIndex);
2532
- pen.calculative.worldRect.y = pen.calculative.initRect.y + lastVal;
2533
- pen.calculative.worldRect.ey = pen.calculative.initRect.ey + lastVal;
2534
- translateRect(pen.calculative.worldRect, 0, frame[k] * process * pen.calculative.canvas.store.data.scale);
2535
- pen.calculative.patchFlags = true;
2536
- }
2537
- else if (k === 'rotate') {
2538
- if (pen.prevFrame[k] >= 360) {
2539
- pen.prevFrame[k] %= 360;
2540
- }
2541
- var lastVal = getFrameValue(pen, k, pen.calculative.frameIndex);
2542
- var offsetRotate = ((pen.calculative.initRect.rotate + lastVal + frame[k] * process) %
2543
- 360) -
2544
- (pen.calculative.rotate || 0);
2545
- if ((_a = pen.children) === null || _a === void 0 ? void 0 : _a.length) {
2546
- pen.calculative.canvas.rotatePen(pen, offsetRotate, pen.calculative.initRect);
2547
- }
2548
- else {
2549
- pen.calculative.rotate =
2550
- (pen.calculative.initRect.rotate + lastVal + frame[k] * process) %
2551
- 360;
2552
- }
2553
- pen.calculative.patchFlags = true;
2554
- }
2555
- else if (k === 'image') {
2556
- pen.image = frame['image'];
2557
- pen.calculative.image = undefined;
2558
- pen.calculative.canvas.loadImage(pen);
2559
- // if (pen.isBottom) {
2560
- // pen.calculative.canvas.canvasImageBottom.init();
2561
- // } else {
2562
- // pen.calculative.canvas.canvasImage.init();
2563
- // }
2564
- if (pen.canvasLayer === CanvasLayer.CanvasImageBottom) {
2565
- pen.calculative.canvas.canvasImageBottom.init();
2566
- }
2567
- else if (pen.canvasLayer === CanvasLayer.CanvasImage) {
2568
- pen.calculative.canvas.canvasImage.init();
2569
- }
2570
- }
2571
- else if (isLinear(frame[k], k, pen)) {
2572
- if (pen.prevFrame[k] == null) {
2573
- if (k === 'globalAlpha') {
2574
- pen.prevFrame[k] = 1;
2575
- }
2576
- else {
2577
- pen.prevFrame[k] = 0;
2578
- }
2579
- }
2580
- var current = pen.prevFrame[k] + (frame[k] - pen.prevFrame[k]) * process;
2581
- pen.calculative[k] = Math.round(current * 100) / 100;
2582
- }
2583
- else {
2584
- if (k === 'visible') {
2585
- if (pen.calculative.image) {
2586
- // if (pen.isBottom) {
2587
- // pen.calculative.canvas.canvasImageBottom.init();
2588
- // } else {
2589
- // pen.calculative.canvas.canvasImage.init();
2590
- // }
2591
- if (pen.canvasLayer === CanvasLayer.CanvasImageBottom) {
2592
- pen.calculative.canvas.canvasImageBottom.init();
2593
- }
2594
- else if (pen.canvasLayer === CanvasLayer.CanvasImage) {
2595
- pen.calculative.canvas.canvasImage.init();
2596
- }
2597
- }
2598
- else if ((_b = pen.children) === null || _b === void 0 ? void 0 : _b.length) {
2599
- var childs = getAllChildren(pen, pen.calculative.canvas.store);
2600
- pen.calculative.canvas.initImageCanvas(childs);
2601
- }
2602
- }
2603
- pen.calculative[k] = frame[k];
2604
- var v = {};
2605
- v[k] = frame[k];
2606
- setChildValue(pen, v);
2607
- }
2608
- if (k === 'text') {
2609
- calcTextLines(pen);
2610
- }
2611
- }
2612
- }
2613
- /**
2614
- * 值类型为 number , pen.linear 为 false 时,且 key 不属于 noLinear 时,返回 true
2615
- * @param value 值
2616
- * @param key 键值
2617
- * @param pen 画笔
2618
- * @returns
2619
- */
2620
- function isLinear(value, key, pen) {
2621
- // 不线性变化的属性
2622
- var noLinear = ['strokeType', 'bkType', 'showChild'];
2623
- return (typeof value === 'number' &&
2624
- pen.linear !== false &&
2625
- !noLinear.includes(key));
2626
- }
2627
- export function setLineAnimate(pen, now) {
2628
- if (pen.calculative.start === 0) {
2629
- pen.calculative.start = undefined;
2630
- return 0;
2631
- }
2632
- if (!pen.animateCycle) {
2633
- pen.animateCycle = Infinity;
2634
- }
2635
- if (!pen.animateSpan) {
2636
- pen.animateSpan = 1;
2637
- }
2638
- pen.calculative.animatePos +=
2639
- pen.animateSpan * (pen.calculative.canvas.store.data.scale || 1);
2640
- if (!pen.calculative.start) {
2641
- pen.calculative.start = Date.now();
2642
- pen.calculative.animatePos =
2643
- pen.animateSpan * (pen.calculative.canvas.store.data.scale || 1);
2644
- pen.calculative.cycleIndex = 1;
2645
- }
2646
- else if (pen.calculative.animatePos > pen.length) {
2647
- // 播放到尾了
2648
- ++pen.calculative.cycleIndex;
2649
- // 播放结束
2650
- if (pen.calculative.cycleIndex > pen.animateCycle) {
2651
- pen.currentAnimation = undefined;
2652
- pen.calculative.start = undefined;
2653
- return 0;
2654
- }
2655
- pen.calculative.animatePos = pen.animateSpan;
2656
- }
2657
- return true;
2658
- }
2659
- export function setChildrenActive(pen, active) {
2660
- if (active === void 0) { active = true; }
2661
- if (!pen.children || pen.childActive === false) {
2662
- return;
2663
- }
2664
- var store = pen.calculative.canvas.store;
2665
- pen.children.forEach(function (id) {
2666
- var child = store.pens[id];
2667
- if (child) {
2668
- child.calculative.active = active;
2669
- setChildrenActive(child, active);
2670
- }
2671
- });
2672
- }
2673
- export function setHover(pen, hover) {
2674
- if (hover === void 0) { hover = true; }
2675
- if (!pen) {
2676
- return;
2677
- }
2678
- var store = pen.calculative.canvas.store;
2679
- pen.calculative.hover = hover;
2680
- if (pen.childHover === false) {
2681
- return;
2682
- }
2683
- if (pen.children) {
2684
- pen.children.forEach(function (id) {
2685
- var _a, _b;
2686
- // 子节点没有自己的独立hover,继承父节点hover
2687
- if (((_a = store.pens[id]) === null || _a === void 0 ? void 0 : _a.hoverColor) == undefined &&
2688
- ((_b = store.pens[id]) === null || _b === void 0 ? void 0 : _b.hoverBackground) == undefined) {
2689
- setHover(store.pens[id], hover);
2690
- }
2691
- });
2692
- }
2693
- }
2694
- export function setElemPosition(pen, elem) {
2695
- if (!elem) {
2696
- return;
2697
- }
2698
- var store = pen.calculative.canvas.store;
2699
- var worldRect = pen.calculative.worldRect;
2700
- elem.style.opacity = pen.globalAlpha + '';
2701
- elem.style.position = 'absolute';
2702
- elem.style.outline = 'none';
2703
- elem.style.left = worldRect.x + store.data.x + 'px';
2704
- elem.style.top = worldRect.y + store.data.y + 'px';
2705
- elem.style.width = worldRect.width + 'px';
2706
- elem.style.height = worldRect.height + 'px';
2707
- elem.style.display =
2708
- pen.calculative.inView != false
2709
- ? pen.calculative.cssDisplay || 'inline'
2710
- : 'none'; // 是否隐藏元素
2711
- !pen.calculative.rotate && (pen.calculative.rotate = 0);
2712
- elem.style.transform = "rotate(" + pen.calculative.rotate + "deg)";
2713
- if (!pen.calculative.rotate) {
2714
- if (pen.calculative.flipX) {
2715
- elem.style.transform = "rotateY(180deg)";
2716
- }
2717
- if (pen.calculative.flipY) {
2718
- elem.style.transform = "rotateX(180deg)";
2719
- }
2720
- if (pen.calculative.flipX && pen.calculative.flipY) {
2721
- elem.style.transform = "rotateZ(180deg)";
2722
- }
2723
- }
2724
- elem.style.zIndex =
2725
- pen.calculative.zIndex !== undefined ? pen.calculative.zIndex + '' : '5';
2726
- if (pen.calculative.zIndex > pen.calculative.canvas.maxZindex) {
2727
- pen.calculative.canvas.maxZindex = pen.calculative.zIndex;
2728
- }
2729
- if (pen.locked === LockState.DisableEdit ||
2730
- pen.locked === LockState.DisableMove ||
2731
- store.data.locked) {
2732
- // gif 组合后,作为子节点可通过 lockedOnCombine 来决定自身的 locked 状态
2733
- elem.style.userSelect = 'initial';
2734
- elem.style.pointerEvents = 'initial';
2735
- if (pen.name === 'gif') {
2736
- elem.style.userSelect = 'none';
2737
- elem.style.pointerEvents = 'none';
2738
- }
2739
- }
2740
- else {
2741
- // pen.locked LockState.Disable 不响应鼠标
2742
- elem.style.userSelect = 'none';
2743
- elem.style.pointerEvents = 'none';
2744
- }
2745
- }
2746
- export function setElemImg(pen, elem) {
2747
- if (!elem) {
2748
- return;
2749
- }
2750
- //https://github.com/niklasvh/html2canvas
2751
- globalThis.html2canvas &&
2752
- globalThis.html2canvas(elem).then(function (canvas) {
2753
- // document.body.appendChild(canvas);
2754
- var img = new Image();
2755
- img.src = canvas.toDataURL('image/png', 0.1);
2756
- if (img.src.length > 10) {
2757
- pen.calculative.img = img;
2758
- }
2759
- });
2760
- }
2761
- /**
2762
- * 每个画笔 locked
2763
- * @param pens 画笔
2764
- * @returns
2765
- */
2766
- export function getPensLock(pens) {
2767
- return pens.every(function (pen) { return pen.locked; });
2768
- }
2769
- /**
2770
- * 画笔们的 disabledRotate = true
2771
- * 即 全部禁止旋转 返回 true
2772
- * @param pens 画笔
2773
- * @returns
2774
- */
2775
- export function getPensDisableRotate(pens) {
2776
- return pens.every(function (pen) { return pen.disableRotate; });
2777
- }
2778
- export function rotatePen(pen, angle, rect) {
2779
- var _a;
2780
- if (pen.type) {
2781
- pen.calculative.worldAnchors.forEach(function (anchor) {
2782
- rotatePoint(anchor, angle, rect.center);
2783
- });
2784
- initLineRect(pen);
2785
- calcPenRect(pen);
2786
- }
2787
- else {
2788
- if (pen.calculative.rotate) {
2789
- pen.calculative.rotate += angle;
2790
- }
2791
- else {
2792
- pen.calculative.rotate = angle;
2793
- }
2794
- rotatePoint(pen.calculative.worldRect.center, angle, rect.center);
2795
- if (pen.parentId) {
2796
- pen.calculative.worldRect.x =
2797
- pen.calculative.worldRect.center.x -
2798
- pen.calculative.worldRect.width / 2;
2799
- pen.calculative.worldRect.y =
2800
- pen.calculative.worldRect.center.y -
2801
- pen.calculative.worldRect.height / 2;
2802
- pen.x = (pen.calculative.worldRect.x - rect.x) / rect.width;
2803
- pen.y = (pen.calculative.worldRect.y - rect.y) / rect.height;
2804
- }
2805
- }
2806
- (_a = pen.children) === null || _a === void 0 ? void 0 : _a.forEach(function (id) {
2807
- var child = pen.calculative.canvas.store.pens[id];
2808
- rotatePen(child, angle, rect);
2809
- });
2810
- }
2811
- function initLineRect(pen) {
2812
- var _a;
2813
- if (!((_a = pen.calculative.worldAnchors) === null || _a === void 0 ? void 0 : _a.length)) {
2814
- return;
2815
- }
2816
- if (!isFinite(pen.x) || !isFinite(pen.x)) {
2817
- return;
2818
- }
2819
- if (pen.x == null || pen.y == null) {
2820
- return;
2821
- }
2822
- var rect = getLineRect(pen);
2823
- if (!pen.parentId) {
2824
- Object.assign(pen, rect);
2825
- }
2826
- var _b = pen.calculative.canvas.store.options, fontSize = _b.fontSize, lineHeight = _b.lineHeight;
2827
- if (!pen.fontSize) {
2828
- pen.fontSize = fontSize;
2829
- pen.calculative.fontSize =
2830
- pen.fontSize * pen.calculative.canvas.store.data.scale;
2831
- }
2832
- if (!pen.lineHeight) {
2833
- pen.lineHeight = lineHeight;
2834
- pen.calculative.lineHeight = pen.lineHeight;
2835
- }
2836
- calcCenter(rect);
2837
- pen.calculative.worldRect = rect;
2838
- calcPadding(pen, rect);
2839
- calcTextRect(pen);
2840
- if (pen.calculative.worldAnchors) {
2841
- pen.anchors = pen.calculative.worldAnchors.map(function (pt) {
2842
- return calcRelativePoint(pt, pen.calculative.worldRect);
2843
- });
2844
- }
2845
- }
2846
- /**
2847
- * 画笔们的 disableSize = true
2848
- * 即 全部不允许改变大小 返回 true
2849
- * @param pens 画笔
2850
- * @returns
2851
- */
2852
- export function getPensDisableResize(pens) {
2853
- return pens.every(function (pen) { return pen.disableSize || pen.pivot; }); //旋转中心点图元不允许改变大小
2854
- }
2855
- export function getFrameValue(pen, prop, frameIndex) {
2856
- if (!pen.frames || !prop) {
2857
- return 0;
2858
- }
2859
- var v = 0;
2860
- for (var i = 0; i < frameIndex; i++) {
2861
- if (pen.frames[i]) {
2862
- v += pen.frames[i][prop] || 0;
2863
- }
2864
- }
2865
- return v;
2866
- }
2867
- /**
2868
- * 判断该画笔 是否是组合为状态中 展示的画笔
2869
- */
2870
- export function isShowChild(pen, store) {
2871
- var _a;
2872
- var selfPen = pen;
2873
- while (selfPen && selfPen.parentId) {
2874
- var oldPen = selfPen;
2875
- selfPen = store.pens[selfPen.parentId];
2876
- var showChildIndex = (_a = selfPen === null || selfPen === void 0 ? void 0 : selfPen.calculative) === null || _a === void 0 ? void 0 : _a.showChild;
2877
- if (showChildIndex != undefined) {
2878
- var showChildId = selfPen.children[showChildIndex];
2879
- if (showChildId !== oldPen.id) {
2880
- // toPng 不展示它
2881
- return false;
2882
- }
2883
- }
2884
- }
2885
- return true;
2886
- }
2887
- /**
2888
- * 计算画笔的 inView
2889
- * @param pen 画笔
2890
- * @param calcChild 是否计算子画笔
2891
- */
2892
- export function calcInView(pen, calcChild) {
2893
- var _a, _b;
2894
- if (calcChild === void 0) { calcChild = false; }
2895
- var _c = pen.calculative.canvas, store = _c.store, canvasRect = _c.canvasRect;
2896
- if (calcChild) {
2897
- (_a = pen.children) === null || _a === void 0 ? void 0 : _a.forEach(function (id) {
2898
- var child = store.pens[id];
2899
- child && calcInView(child, true);
2900
- });
2901
- }
2902
- pen.calculative.inView = true;
2903
- if (!isShowChild(pen, store) ||
2904
- pen.visible == false ||
2905
- pen.calculative.visible == false) {
2906
- pen.calculative.inView = false;
2907
- }
2908
- else {
2909
- var _d = pen.calculative.worldRect, x = _d.x, y = _d.y, width = _d.width, height = _d.height, rotate = _d.rotate;
2910
- var penRect = {
2911
- x: x + store.data.x,
2912
- y: y + store.data.y,
2913
- width: width,
2914
- height: height,
2915
- rotate: rotate,
2916
- };
2917
- calcRightBottom(penRect);
2918
- if (!rectInRect(penRect, canvasRect)) {
2919
- pen.calculative.inView = false;
2920
- }
2921
- }
2922
- // TODO: 语义化上,用 onValue 更合适,但 onValue 会触发 echarts 图形的重绘,没有必要
2923
- // 更改 view 后,修改 dom 节点的显示隐藏
2924
- // pen.onMove?.(pen);
2925
- ((_b = pen.calculative.singleton) === null || _b === void 0 ? void 0 : _b.div) && setElemPosition(pen, pen.calculative.singleton.div);
2926
- }
2927
- /**
2928
- * 绘制 rect ,上线后可查看 rect 位置
2929
- */
2930
- function inspectRect(ctx, store, pen) {
2931
- if (store.fillWorldTextRect) {
2932
- ctx.save();
2933
- ctx.fillStyle = '#c3deb7';
2934
- var _a = pen.calculative.worldTextRect, x = _a.x, y = _a.y, width = _a.width, height = _a.height;
2935
- ctx.fillRect(x, y, width, height);
2936
- ctx.restore();
2937
- }
2938
- }
2939
- export function setGlobalAlpha(ctx, pen) {
2940
- var globalAlpha = pen.calculative.globalAlpha;
2941
- if (typeof globalAlpha === 'number' &&
2942
- globalAlpha < 1 &&
2943
- !isNaN(globalAlpha)) {
2944
- ctx.globalAlpha = globalAlpha;
2945
- }
2946
- }
2947
- /**
2948
- * ctx 绘制图纸,并非 Path2D
2949
- * @param ctx 画布上下文
2950
- * @param pen 画笔
2951
- */
2952
- function ctxDrawCanvas(ctx, pen) {
2953
- var canvasDraw = drawFuncGenerator(ctx, pen) || globalStore.canvasDraws[pen.name];
2954
- if (canvasDraw) {
2955
- // TODO: 后续考虑优化 save / restore
2956
- ctx.save();
2957
- // TODO: 原有 return 终止后续操作,必要性不大
2958
- canvasDraw(ctx, pen);
2959
- ctx.restore();
2960
- }
2961
- }
2962
- function drawFuncGenerator(ctx, pen) {
2963
- // 进行数据的预处理
2964
- var drawCommand = pen.drawCommand;
2965
- if (!drawCommand || pen.name === 'line')
2966
- return;
2967
- // 单位转换 将其他单位转换为px
2968
- // 执行自定义绘画函数
2969
- return function (ctx, pen) {
2970
- // TODO 绘制命令的转换 (能否兼容多种指令??)
2971
- drawCommand.forEach(function (command) {
2972
- try {
2973
- command.steps.reduce(function (calculate, step) {
2974
- var cs = commandTransfer(step, pen, calculate.x, calculate.y);
2975
- // 应当保证顺序的正确
2976
- try {
2977
- if (cs.c) {
2978
- var l = [];
2979
- for (var csKey in cs.v) {
2980
- l.push(cs.v[csKey]);
2981
- }
2982
- // ctx.beginPath();
2983
- ctx[cs.c].apply(ctx, __spreadArray([], __read(l), false));
2984
- ctx.moveTo(cs.startX || cs.v.x, cs.startY || cs.v.y);
2985
- // command.prop.NoFill === '0'?ctx.fill():'';
2986
- return { x: cs.startX || cs.v.x, y: cs.startY || cs.v.y };
2987
- }
2988
- return { x: calculate.x, y: calculate.y };
2989
- }
2990
- catch (e) {
2991
- // pass
2992
- }
2993
- }, {});
2994
- }
2995
- catch (e) {
2996
- }
2997
- });
2998
- ctx.stroke();
2999
- };
3000
- }
3001
- function commandTransfer(command, pen, startX, startY) {
3002
- // TODO 是否支持扩展更多的命令?用于兼容未来的其他解析格式?
3003
- //1. 进行简单的命令解析
3004
- // VISIO
3005
- var map = {
3006
- 'visio': dealWithVisio,
3007
- 'dxf': dealWithDXF
3008
- };
3009
- // CAD
3010
- return map[pen.parseType](command, pen, startX, startY);
3011
- }
3012
- function dealWithDXF(command, pen, startX, startY) {
3013
- var _a = pen.calculative.worldRect, x = _a.x, y = _a.y, width = _a.width, height = _a.height;
3014
- switch (command.c) {
3015
- case "moveTo":
3016
- return {
3017
- c: 'moveTo',
3018
- v: {
3019
- x: command.v.x * pen.calculative.canvas.store.data.scale,
3020
- y: window.innerHeight - (command.v.y * pen.calculative.canvas.store.data.scale)
3021
- }
3022
- };
3023
- case "lineTo":
3024
- return {
3025
- c: 'lineTo',
3026
- v: {
3027
- x: command.v.x * pen.calculative.canvas.store.data.scale,
3028
- y: window.innerHeight - (command.v.y * pen.calculative.canvas.store.data.scale)
3029
- }
3030
- };
3031
- case "arc":
3032
- return {
3033
- c: 'arc',
3034
- v: {
3035
- x: command.v.x * pen.calculative.canvas.store.data.scale,
3036
- y: window.innerHeight - (command.v.y * pen.calculative.canvas.store.data.scale),
3037
- r: command.v.r * pen.calculative.canvas.store.data.scale,
3038
- startAngle: command.v.startAngle,
3039
- endAngle: command.v.endAngle
3040
- }
3041
- };
3042
- }
3043
- return command;
3044
- }
3045
- function dealWithVisio(command, pen, startX, startY) {
3046
- var _a = pen.calculative.worldRect, x = _a.x, y = _a.y, width = _a.width, height = _a.height;
3047
- var _b = pen.origin, originWidth = _b.width, originHeight = _b.height;
3048
- switch (command.c) {
3049
- case "MoveTo":
3050
- return {
3051
- c: "moveTo",
3052
- v: {
3053
- x: +command.v.X * (100) * (width / originWidth) + x,
3054
- y: +command.v.Y * (100) * (height / originHeight) + y
3055
- }
3056
- };
3057
- case "RelMoveTo":
3058
- return {
3059
- c: "moveTo",
3060
- v: {
3061
- x: +command.v.X * originWidth * (width / originWidth) + x,
3062
- y: +command.v.Y * originHeight * (height / originHeight) + y
3063
- }
3064
- };
3065
- case "LineTo":
3066
- return {
3067
- c: "lineTo",
3068
- v: {
3069
- x: +command.v.X * (100) * (width / originWidth) + x,
3070
- y: +command.v.Y * (100) * (height / originHeight) + y
3071
- }
3072
- };
3073
- case "RelLineTo":
3074
- return {
3075
- c: "lineTo",
3076
- v: {
3077
- x: +command.v.X * originWidth * (width / originWidth) + x,
3078
- y: +command.v.Y * originHeight * (height / originHeight) + y
3079
- }
3080
- };
3081
- case "Ellipse":
3082
- var centerX1 = command.v.X;
3083
- var centerY1 = command.v.Y;
3084
- var longAxis = Math.abs(command.v.A - command.v.C);
3085
- var shortAxis = Math.abs(command.v.B - command.v.D);
3086
- return {
3087
- c: "ellipse",
3088
- v: {
3089
- x: centerX1 * (100) * (width / originWidth) + x,
3090
- y: centerY1 * (100) * (height / originHeight) + y,
3091
- radiuX: longAxis * (100) * (width / originWidth),
3092
- radiuY: shortAxis * (100) * (height / originHeight),
3093
- rotation: 0,
3094
- startAngle: 0,
3095
- endAngle: Math.PI * 2,
3096
- anticlockwise: true
3097
- }
3098
- };
3099
- case "EllipticalArcTo":
3100
- var endX = command.v.X * 100 * (width / originWidth) + x; // 弧上结束顶点的 x 坐标
3101
- var endY = command.v.Y * 100 * (height / originHeight) + y; // 弧上结束顶点的 y 坐标
3102
- var ctrlX = command.v.A * 100 * (width / originWidth) + x; // 控制点的 x 坐标
3103
- var ctrlY = command.v.B * 100 * (height / originHeight) + y; // 控制点的 y 坐标
3104
- var angleDeg = command.v.C; // 主轴相对于 x 轴的角度 (度)
3105
- var axisRatio = command.v.D * (width / height) * (originHeight / originWidth); // 长轴和短轴的比率
3106
- //
3107
- var params = calculateEllipseParameters(startX, startY, endX, endY, ctrlX, ctrlY, axisRatio);
3108
- // 开始绘制路径
3109
- !command.orign && (command.orign = {});
3110
- !command.orign.startA && (command.orign.startA = calculateAngleInRadians(params.x0, params.y0, startX, startY));
3111
- !command.orign.endA && (command.orign.endA = calculateAngleInRadians(params.x0, params.y0, endX, endY));
3112
- return {
3113
- c: "ellipse",
3114
- v: {
3115
- centerX: params.x0,
3116
- centerY: params.y0,
3117
- radiuX: params.a,
3118
- radiuY: params.b,
3119
- // rotation:radiansToDegrees(angleDeg),
3120
- rotation: 0,
3121
- startAngle: command.orign.startA,
3122
- endAngle: command.orign.endA,
3123
- // startAngle: 0,
3124
- // endAngle: Math.PI * 2,
3125
- // anticlockwise: startA > 0 && startA>endA
3126
- anticlockwise: +angleDeg < 0
3127
- // anticlockwise: Math.abs(endA - startA) < Math.PI
3128
- },
3129
- startX: endX,
3130
- startY: endY,
3131
- };
3132
- case "ArcTo":
3133
- var endX2 = command.v.X * 100 * width / originWidth + x;
3134
- var endY2 = command.v.Y * 100 * height / originHeight + y;
3135
- var h = command.v.A * 100 * (width / height) * (originHeight / originWidth);
3136
- // 计算弦的中点
3137
- var xm = (startX + endX2) / 2;
3138
- var ym = (startY + endY2) / 2;
3139
- // 计算弦的长度
3140
- var d = Math.sqrt(Math.pow((endX2 - startX), 2) + Math.pow((endY2 - startY), 2));
3141
- // 计算圆弧的半径
3142
- var R = (Math.pow(d, 2)) / (8 * h) + h / 2;
3143
- // 计算单位垂直向量
3144
- var ux = -(endY2 - startY) / d;
3145
- var uy = (endX2 - startX) / d;
3146
- // 计算两个可能的圆心
3147
- var xc1 = xm + ux * R;
3148
- var yc1 = ym + uy * R;
3149
- var xc2 = xm - ux * R;
3150
- var yc2 = ym - uy * R;
3151
- // 选择一个圆心
3152
- var xc = xc1;
3153
- var yc = yc1;
3154
- // 计算起点和终点到圆心的角度
3155
- var startAngle = Math.atan2(startY - yc, startX - xc);
3156
- var endAngle = Math.atan2(endY2 - yc, endX2 - xc);
3157
- return {
3158
- c: 'arc',
3159
- v: {
3160
- x: xc,
3161
- y: yc,
3162
- radius: R,
3163
- startAngle: startAngle,
3164
- endAngle: endAngle,
3165
- aclockwise: true,
3166
- }
3167
- };
3168
- // case "RelCubBezTo":
3169
- // return {
3170
- // c:"bezierCurveTo",
3171
- // v:{
3172
- //
3173
- // }
3174
- // };
3175
- default:
3176
- // console.log(command.c);
3177
- return {};
3178
- }
3179
- }
3180
- export function setChildValue(pen, data) {
3181
- for (var k in data) {
3182
- if (inheritanceProps.includes(k)) {
3183
- pen[k] = data[k];
3184
- if (['fontSize', 'lineWidth'].includes(k)) {
3185
- pen.calculative[k] = data[k] * pen.calculative.canvas.store.data.scale;
3186
- calcTextRect(pen);
3187
- }
3188
- else {
3189
- pen.calculative[k] = data[k];
3190
- }
3191
- }
3192
- }
3193
- if (pen.calculative.canvas.parent.isCombine(pen)
3194
- //&& pen.showChild === undefined
3195
- ) {
3196
- var children = pen.children;
3197
- children === null || children === void 0 ? void 0 : children.forEach(function (childId) {
3198
- var _data = deepClone(data);
3199
- if (pen.calculative.childrenVisible) {
3200
- if (pen.calculative.childrenVisible[childId] === false) {
3201
- delete _data.visible;
3202
- }
3203
- }
3204
- var child = pen.calculative.canvas.store.pens[childId];
3205
- child && setChildValue(child, _data);
3206
- });
3207
- }
3208
- }
3209
- function calculateEllipseParameters(x1, y1, x2, y2, x3, y3, D) {
3210
- // Calculate x₀ using equation ⑥
3211
- var numeratorX0 = (x1 - x2) * (x1 + x2) * (y2 - y3) - (x2 - x3) * (x2 + x3) * (y1 - y2) + D * D * (y1 - y2) * (y2 - y3) * (y1 - y3);
3212
- var denominatorX0 = 2 * ((x1 - x2) * (y2 - y3) - (x2 - x3) * (y1 - y2));
3213
- var x0 = numeratorX0 / denominatorX0;
3214
- // Calculate y₀ using equation ⑦
3215
- var numeratorY0 = (x1 - x2) * (x2 - x3) * (x1 - x3) + D * D * ((x2 - x3) * (y1 - y2) * (y1 + y2) - (x1 - x2) * (y2 - y3) * (y2 + y3));
3216
- var denominatorY0 = 2 * D * D * ((x2 - x3) * (y1 - y2) - (x1 - x2) * (y2 - y3));
3217
- var y0 = numeratorY0 / denominatorY0;
3218
- // Calculate 'a' using equation ⑧, substituting x₀ and y₀
3219
- var a = Math.sqrt(Math.pow(x1 - x0, 2) + Math.pow(D * (y1 - y0), 2));
3220
- // Calculate 'b' using equation ⑨
3221
- var b = a / D;
3222
- return { x0: x0, y0: y0, a: a, b: b };
3223
- }
3224
- function calculateAngleInRadians(x1, y1, x2, y2) {
3225
- // 计算两个点的差值
3226
- var dx = x2 - x1;
3227
- var dy = y2 - y1;
3228
- // 使用 atan2 计算角度,结果为弧度
3229
- var angleRadians = Math.atan2(dy, dx);
3230
- // 如果角度为负值,加上2π
3231
- if (angleRadians < 0) {
3232
- angleRadians += 2 * Math.PI;
3233
- }
3234
- return angleRadians;
3235
- }
3236
- //# sourceMappingURL=render.js.map