vislite 0.5.2-alpha.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/.editorconfig +18 -18
  2. package/AUTHORS.txt +6 -6
  3. package/CHANGELOG +143 -137
  4. package/LICENSE +20 -20
  5. package/README.md +69 -69
  6. package/lib/Buffer/index.umd.js +36 -36
  7. package/lib/Buffer/index.umd.min.js +2 -2
  8. package/lib/Canvas/index.umd.js +650 -650
  9. package/lib/Canvas/index.umd.min.js +2 -2
  10. package/lib/Cardinal/index.umd.js +91 -91
  11. package/lib/Cardinal/index.umd.min.js +2 -2
  12. package/lib/Eoap/index.umd.js +34 -34
  13. package/lib/Eoap/index.umd.min.js +2 -2
  14. package/lib/Geometry/index.umd.js +142 -142
  15. package/lib/Geometry/index.umd.min.js +2 -2
  16. package/lib/Hermite/index.umd.js +36 -36
  17. package/lib/Hermite/index.umd.min.js +2 -2
  18. package/lib/Matrix4/index.umd.js +119 -119
  19. package/lib/Matrix4/index.umd.min.js +2 -2
  20. package/lib/Mercator/index.umd.js +20 -20
  21. package/lib/Mercator/index.umd.min.js +2 -2
  22. package/lib/OralCanvas/index.es.js +598 -598
  23. package/lib/OralCanvas/index.es.min.js +2 -2
  24. package/lib/OralWebGL/index.es.js +532 -532
  25. package/lib/OralWebGL/index.es.min.js +3 -3
  26. package/lib/SVG/index.umd.js +470 -405
  27. package/lib/SVG/index.umd.min.js +3 -3
  28. package/lib/Shader/index.umd.js +69 -69
  29. package/lib/Shader/index.umd.min.js +3 -3
  30. package/lib/Texture/index.umd.js +53 -53
  31. package/lib/Texture/index.umd.min.js +2 -2
  32. package/lib/TreeLayout/index.umd.js +363 -363
  33. package/lib/TreeLayout/index.umd.min.js +2 -2
  34. package/lib/WebGL/index.umd.js +579 -579
  35. package/lib/WebGL/index.umd.min.js +3 -3
  36. package/lib/animation/index.umd.js +72 -72
  37. package/lib/animation/index.umd.min.js +2 -2
  38. package/lib/formatColor/index.umd.js +40 -40
  39. package/lib/formatColor/index.umd.min.js +2 -2
  40. package/lib/getLoopColors/index.umd.js +36 -36
  41. package/lib/getLoopColors/index.umd.min.js +2 -2
  42. package/lib/getWebGLContext/index.umd.js +44 -44
  43. package/lib/getWebGLContext/index.umd.min.js +2 -2
  44. package/lib/index.umd.js +2744 -2662
  45. package/lib/index.umd.min.js +3 -3
  46. package/lib/move/index.umd.js +27 -0
  47. package/lib/move/index.umd.min.js +11 -0
  48. package/lib/resizeObserver/index.umd.js +29 -29
  49. package/lib/resizeObserver/index.umd.min.js +2 -2
  50. package/lib/rotate/index.umd.js +8 -8
  51. package/lib/rotate/index.umd.min.js +2 -2
  52. package/lib/ruler/index.umd.js +126 -126
  53. package/lib/ruler/index.umd.min.js +2 -2
  54. package/lib/scale/index.umd.js +26 -0
  55. package/lib/scale/index.umd.min.js +11 -0
  56. package/lib/throttle/index.umd.js +49 -49
  57. package/lib/throttle/index.umd.min.js +2 -2
  58. package/lib/viewHandler/index.umd.js +120 -120
  59. package/lib/viewHandler/index.umd.min.js +2 -2
  60. package/miniprogram/ui-canvas/index.js +107 -107
  61. package/miniprogram/ui-canvas/index.json +4 -4
  62. package/miniprogram/ui-canvas/index.wxml +4 -4
  63. package/miniprogram/ui-canvas/index.wxss +5 -5
  64. package/package/Buffer/index.ts +2 -2
  65. package/package/Canvas/index.ts +2 -2
  66. package/package/Cardinal/index.ts +2 -2
  67. package/package/Eoap/index.ts +2 -2
  68. package/package/Geometry/index.ts +2 -2
  69. package/package/Hermite/index.ts +2 -2
  70. package/package/Matrix4/index.ts +2 -2
  71. package/package/Mercator/index.ts +2 -2
  72. package/package/OralCanvas/index.ts +2 -2
  73. package/package/OralWebGL/index.ts +2 -2
  74. package/package/SVG/index.ts +2 -2
  75. package/package/Shader/index.ts +39 -39
  76. package/package/Texture/index.ts +2 -2
  77. package/package/TreeLayout/index.ts +2 -2
  78. package/package/WebGL/index.ts +2 -2
  79. package/package/animation/index.ts +2 -2
  80. package/package/formatColor/index.ts +2 -2
  81. package/package/getLoopColors/index.ts +2 -2
  82. package/package/getWebGLContext/index.ts +39 -39
  83. package/package/index.ts +76 -72
  84. package/package/move/index.ts +3 -0
  85. package/package/resizeObserver/index.ts +2 -2
  86. package/package/rotate/index.ts +2 -2
  87. package/package/ruler/index.ts +2 -2
  88. package/package/scale/index.ts +3 -0
  89. package/package/throttle/index.ts +2 -2
  90. package/package/viewHandler/index.ts +2 -2
  91. package/package.json +66 -66
  92. package/src/Canvas.ts +67 -67
  93. package/src/Eoap.ts +66 -66
  94. package/src/Geometry.ts +82 -82
  95. package/src/Matrix4/index.ts +76 -76
  96. package/src/Matrix4/move.ts +12 -12
  97. package/src/Matrix4/rotate.ts +15 -15
  98. package/src/Matrix4/scale.ts +11 -11
  99. package/src/Matrix4/transform.ts +64 -64
  100. package/src/Mercator.ts +27 -27
  101. package/src/SVG.ts +24 -24
  102. package/src/TreeLayout/index.ts +186 -186
  103. package/src/WebGL.ts +66 -66
  104. package/src/animation.ts +98 -98
  105. package/src/common/assemble.ts +22 -22
  106. package/src/common/canvas/arc.ts +50 -50
  107. package/src/common/canvas/config.ts +158 -158
  108. package/src/common/canvas/gradient.ts +29 -29
  109. package/src/common/canvas/index.ts +168 -168
  110. package/src/common/canvas/painter.ts +426 -426
  111. package/src/common/canvas/texts.ts +18 -18
  112. package/src/common/geometry/prism-horizontal.ts +33 -33
  113. package/src/common/geometry/prism-vertical.ts +40 -40
  114. package/src/common/geometry/sphere-fragment.ts +39 -39
  115. package/src/common/geometry/tool/circle.ts +11 -11
  116. package/src/common/mergeOption.ts +6 -6
  117. package/src/common/setStyle.ts +5 -5
  118. package/src/common/svg/config.ts +186 -174
  119. package/src/common/svg/gradient.ts +63 -0
  120. package/src/common/svg/index.ts +356 -344
  121. package/src/common/svg/tool.ts +44 -44
  122. package/src/common/tree/index.ts +25 -25
  123. package/src/common/tree/toInnerTree.ts +56 -56
  124. package/src/common/tree/toPlainTree.ts +131 -131
  125. package/src/common/webgl/buffer.ts +79 -79
  126. package/src/common/webgl/doDraw.ts +108 -108
  127. package/src/common/webgl/getColorFactory.ts +26 -26
  128. package/src/common/webgl/getShader.ts +48 -48
  129. package/src/common/webgl/getTexture.ts +11 -11
  130. package/src/common/webgl/getWebGLContext.ts +27 -27
  131. package/src/common/webgl/index.ts +223 -223
  132. package/src/common/webgl/shader.ts +75 -75
  133. package/src/common/webgl/texture.ts +96 -96
  134. package/src/formatColor.ts +44 -44
  135. package/src/getLoopColors.ts +42 -42
  136. package/src/interpolation/Cardinal.ts +110 -110
  137. package/src/interpolation/Hermite.ts +65 -65
  138. package/src/move.ts +8 -0
  139. package/src/resizeObserver.ts +36 -36
  140. package/src/rotate.ts +7 -7
  141. package/src/ruler.ts +188 -188
  142. package/src/scale.ts +7 -0
  143. package/src/shader/fsColor.c +7 -7
  144. package/src/shader/fsColors.c +7 -7
  145. package/src/shader/fsCube.c +8 -8
  146. package/src/shader/fsImage.c +8 -8
  147. package/src/shader/vsColor.c +17 -17
  148. package/src/shader/vsColors.c +13 -13
  149. package/src/shader/vsCube.c +13 -13
  150. package/src/shader/vsImage.c +13 -13
  151. package/src/throttle.ts +51 -51
  152. package/src/viewHandler.ts +158 -158
  153. package/types/Buffer.d.ts +23 -23
  154. package/types/Canvas.d.ts +300 -300
  155. package/types/CanvasConfig.d.ts +106 -106
  156. package/types/CanvasOption.d.ts +7 -7
  157. package/types/Cardinal.d.ts +13 -13
  158. package/types/Geometry.d.ts +40 -40
  159. package/types/GeometryOption.d.ts +11 -11
  160. package/types/GeometryResult.d.ts +18 -18
  161. package/types/Gradient.d.ts +14 -14
  162. package/types/Hermite.d.ts +18 -18
  163. package/types/Map.d.ts +9 -9
  164. package/types/Matrix4.d.ts +42 -42
  165. package/types/Object3D.d.ts +114 -114
  166. package/types/SVG.d.ts +238 -215
  167. package/types/SVGConfig.d.ts +76 -76
  168. package/types/Scene3D.d.ts +6 -6
  169. package/types/Shader.d.ts +21 -21
  170. package/types/Texture.d.ts +16 -16
  171. package/types/Tree.d.ts +69 -69
  172. package/types/TreeConfig.d.ts +24 -24
  173. package/types/TreeLayout.d.ts +54 -54
  174. package/types/TreeOption.d.ts +48 -48
  175. package/types/WebGL.d.ts +38 -38
  176. package/types/WebGLOption.d.ts +7 -7
  177. package/types/animation.d.ts +6 -6
  178. package/types/formatColor.d.ts +2 -2
  179. package/types/getLoopColors.d.ts +2 -2
  180. package/types/getWebGLContext.d.ts +2 -2
  181. package/types/index.d.ts +175 -169
  182. package/types/move.d.ts +3 -0
  183. package/types/painterConfig.d.ts +2 -2
  184. package/types/resizeObserver.d.ts +2 -2
  185. package/types/rotate.d.ts +2 -2
  186. package/types/ruler.d.ts +7 -7
  187. package/types/scale.d.ts +3 -0
  188. package/types/throttle.d.ts +4 -4
  189. package/types/throttleOption.d.ts +21 -21
  190. package/types/viewHandler.d.ts +13 -13
  191. package/uni-app/ui-canvas.vue +226 -226
@@ -1,344 +1,356 @@
1
- import type SVGConfigType from "../../../types/SVGConfig"
2
- import type { arcCapType, textAlignType, textBaselineType } from "../../../types/painterConfig"
3
-
4
- import { initText, initCircle, initPath, initRect, initArc } from "./config"
5
- import { toNode, setAttribute, getAttribute, full, fill, stroke } from "./tool"
6
- import rotate from "../../rotate"
7
-
8
- // 属性名向下兼容
9
- let oldAttrName = {
10
- "font-size": "fontSize",
11
- "font-family": "fontFamily",
12
- "arc-start-cap": "arcStartCap",
13
- "arc-end-cap": "arcEndCap",
14
- }
15
-
16
- class SVG {
17
- readonly name: string = "SVG"
18
-
19
- // 用于记录配置
20
- private __config = {
21
- // 基本设置
22
- fillStyle: "#000",
23
- strokeStyle: "#000",
24
- lineWidth: 1,
25
-
26
- // 文字对齐方式
27
- textAlign: <textAlignType>"left",
28
- textBaseline: <textBaselineType>"middle",
29
-
30
- // 文字设置
31
- "fontSize": 16,
32
- "fontFamily": "sans-serif",
33
-
34
- // arc二端闭合方式['butt':直线闭合,'round':圆帽闭合]
35
- "arcStartCap": <arcCapType>"butt",
36
- "arcEndCap": <arcCapType>"butt",
37
-
38
- // 虚线设置
39
- lineDash: [],
40
- }
41
-
42
- // 作用的节点
43
- private __useEl: SVGElement
44
-
45
- // 路径(和canvas2D的类似)
46
- private __path: string = ""
47
- private __currentPosition: number[] = []
48
-
49
- private __svg: SVGElement
50
- constructor(svg: SVGElement) {
51
- this.__svg = svg
52
- }
53
-
54
- // 属性设置或获取
55
- config(params: SVGConfigType) {
56
- if (typeof params !== "object") {
57
- return this.__config[oldAttrName[params] || params]
58
- } else {
59
- for (let key in params) {
60
- let _key = oldAttrName[key] || key
61
- this.__config[_key] = params[key]
62
- }
63
- }
64
- return this
65
- }
66
-
67
- /**
68
- * 基础方法
69
- * ---------------------------------
70
- */
71
-
72
- // 标记应用节点
73
- // 也就是后续操作都会作用在此节点
74
- useEl(el: SVGElement) {
75
- this.__useEl = el
76
- return this
77
- }
78
-
79
- // 获取当前应用的节点
80
- getEl() {
81
- return this.__useEl
82
- }
83
-
84
- // 追加节点
85
- // el可以是结点或字符串,字符串的话表示节点名称
86
- // context可选,表示追加位置,可选,默认根svg
87
- // 此外,和appendBoard等操作一样,执行后新加入的结点会自动变成应用节点
88
- appendEl(el: string | SVGElement, context?: SVGElement) {
89
- context = context || this.__svg
90
-
91
- if (typeof el == "string") el = toNode(el)
92
- context.appendChild(el as SVGElement)
93
-
94
- this.__useEl = el as SVGElement
95
- return this
96
- }
97
-
98
- // 追加绘制板
99
- // 参数和appendEl类似,只是el如果是字符串的话,表示需要绘制对应什么内容,
100
- // 比如el = “arc”,表示画弧(不是路径arc),那么我们会创建path节点,因为我们是使用path实现的
101
- appendBoard(el: string | SVGElement, context?: SVGElement) {
102
- let _el = el
103
-
104
- if (typeof el == "string")
105
- _el =
106
- {
107
- text: "text",
108
- path: "path",
109
- arc: "path",
110
- circle: "circle",
111
- rect: "rect",
112
- }[el] || ""
113
-
114
- if (_el == "") throw new Error("Unsupported drawing method:" + el)
115
- return this.appendEl(_el, context)
116
- }
117
-
118
- // 删除当前维护的节点
119
- remove() {
120
- if (!this.__useEl) {
121
- throw new Error("Currently, no node can be deleted.")
122
- } else {
123
- ((this.__useEl as SVGElement).parentNode as SVGElement).removeChild(
124
- this.__useEl
125
- )
126
- }
127
- return this
128
- }
129
-
130
- // 设置或获取节点属性
131
- attr(params: any) {
132
- if (!this.__useEl)
133
- throw new Error("Currently, no node can be modified or viewed.")
134
-
135
- if (typeof params !== "object") {
136
- return getAttribute(this.__useEl, params)
137
- } else {
138
- for (let key in params) {
139
- setAttribute(this.__useEl, key, params[key])
140
- }
141
- return this
142
- }
143
- }
144
-
145
- /**
146
- * 绘制方法
147
- * ---------------------------------
148
- */
149
-
150
- // 文字
151
- // deg表示文字旋转角度,是角度值,不是弧度
152
- fillText(text: string, x: number, y: number, deg: number = 0) {
153
- initText(this.__useEl, this.__config, x, y, deg)
154
- this.__useEl.textContent = text
155
- fill(this.__useEl, this.__config)
156
- return this
157
- }
158
- strokeText(text: string, x: number, y: number, deg: number = 0) {
159
- initText(this.__useEl, this.__config, x, y, deg)
160
- this.__useEl.textContent = text
161
- stroke(this.__useEl, this.__config)
162
- return this
163
- }
164
- fullText(text: string, x: number, y: number, deg: number = 0) {
165
- initText(this.__useEl, this.__config, x, y, deg)
166
- this.__useEl.textContent = text
167
- full(this.__useEl, this.__config)
168
- return this
169
- }
170
-
171
- // 弧
172
- fillArc(
173
- cx: number,
174
- cy: number,
175
- r1: number,
176
- r2: number,
177
- beginDeg: number,
178
- deg: number
179
- ) {
180
- initArc(this.__useEl, this.__config, cx, cy, r1, r2, beginDeg, deg)
181
- fill(this.__useEl, this.__config)
182
- return this
183
- }
184
- strokeArc(
185
- cx: number,
186
- cy: number,
187
- r1: number,
188
- r2: number,
189
- beginDeg: number,
190
- deg: number
191
- ) {
192
- initArc(this.__useEl, this.__config, cx, cy, r1, r2, beginDeg, deg)
193
- stroke(this.__useEl, this.__config)
194
- return this
195
- }
196
- fullArc(
197
- cx: number,
198
- cy: number,
199
- r1: number,
200
- r2: number,
201
- beginDeg: number,
202
- deg: number
203
- ) {
204
- initArc(this.__useEl, this.__config, cx, cy, r1, r2, beginDeg, deg)
205
- full(this.__useEl, this.__config)
206
- return this
207
- }
208
-
209
- // 圆形
210
- fillCircle(cx: number, cy: number, r: number) {
211
- initCircle(this.__useEl, cx, cy, r)
212
- fill(this.__useEl, this.__config)
213
- return this
214
- }
215
- strokeCircle(cx: number, cy: number, r: number) {
216
- initCircle(this.__useEl, cx, cy, r)
217
- stroke(this.__useEl, this.__config)
218
- return this
219
- }
220
- fullCircle(cx: number, cy: number, r: number) {
221
- initCircle(this.__useEl, cx, cy, r)
222
- full(this.__useEl, this.__config)
223
- return this
224
- }
225
-
226
- // 矩形
227
- fillRect(x: number, y: number, width: number, height: number) {
228
- initRect(this.__useEl, x, y, width, height)
229
- fill(this.__useEl, this.__config)
230
- return this
231
- }
232
- strokeRect(x: number, y: number, width: number, height: number) {
233
- initRect(this.__useEl, x, y, width, height)
234
- stroke(this.__useEl, this.__config)
235
- return this
236
- }
237
- fullRect(x: number, y: number, width: number, height: number) {
238
- initRect(this.__useEl, x, y, width, height)
239
- full(this.__useEl, this.__config)
240
- return this
241
- }
242
-
243
- // 路径
244
- beginPath() {
245
- this.__currentPosition = []
246
- this.__path = ""
247
- return this
248
- }
249
- closePath() {
250
- this.__path += "Z"
251
- return this
252
- }
253
- moveTo(x: number, y: number) {
254
- this.__currentPosition = [x, y]
255
-
256
- this.__path += "M" + x + " " + y
257
- return this
258
- }
259
- lineTo(x: number, y: number) {
260
- this.__currentPosition = [x, y]
261
-
262
- this.__path += (this.__path == "" ? "M" : "L") + x + " " + y
263
- return this
264
- }
265
- fill() {
266
- initPath(this.__useEl, this.__path)
267
- fill(this.__useEl, this.__config)
268
- return this
269
- }
270
- stroke() {
271
- initPath(this.__useEl, this.__path)
272
- stroke(this.__useEl, this.__config)
273
- return this
274
- }
275
- full() {
276
- initPath(this.__useEl, this.__path);
277
- full(this.__useEl, this.__config)
278
- return this
279
- }
280
-
281
- arc(x: number, y: number, r: number, beginDeg: number, deg: number) {
282
- let begPosition = rotate(x, y, (beginDeg / 180) * Math.PI, x + r, y)
283
- let endPosition = rotate(
284
- x,
285
- y,
286
- ((beginDeg + deg) / 180) * Math.PI,
287
- x + r,
288
- y
289
- )
290
- // 如果当前没有路径,说明是开始的,就移动到正确位置
291
- if (this.__path == "") {
292
- this.__path += "M" + begPosition[0] + "," + begPosition[1]
293
- }
294
- // 如果当前有路径,位置不正确,应该画到正确位置(和canvas保持一致)
295
- else if (
296
- begPosition[0] != this.__currentPosition[0] ||
297
- begPosition[1] != this.__currentPosition[1]
298
- ) {
299
- this.__path += "L" + begPosition[0] + "," + begPosition[1]
300
- }
301
- this.__path +=
302
- "A" +
303
- r +
304
- "," +
305
- r +
306
- " 0 " +
307
- (deg > 180 || deg < -180 ? 1 : 0) +
308
- "," +
309
- (deg > 0 ? 1 : 0) +
310
- " " +
311
- endPosition[0] +
312
- "," +
313
- endPosition[1]
314
- return this
315
- }
316
-
317
- // 路径 - 贝塞尔曲线
318
- quadraticCurveTo(cpx: number, cpy: number, x: number, y: number) {
319
- this.__path += "Q" + cpx + " " + cpy + "," + x + " " + y
320
- return this
321
- }
322
- bezierCurveTo(
323
- cp1x: number,
324
- cp1y: number,
325
- cp2x: number,
326
- cp2y: number,
327
- x: number,
328
- y: number
329
- ) {
330
- this.__path +=
331
- "C" + cp1x + " " + cp1y + "," + cp2x + " " + cp2y + "," + x + " " + y
332
- return this
333
- }
334
-
335
- // 绑定事件
336
- bind(eventType: string, callback: (event: Event, target: SVGElement) => void) {
337
- this.__useEl.addEventListener(eventType, function (event: Event) {
338
- callback.call(this, event, this)
339
- }, false)
340
- return this
341
- }
342
- }
343
-
344
- export default SVG
1
+ import type SVGConfigType from "../../../types/SVGConfig"
2
+ import type { arcCapType, textAlignType, textBaselineType } from "../../../types/painterConfig"
3
+
4
+ import { initText, initCircle, initPath, initRect, initArc } from "./config"
5
+ import { toNode, setAttribute, getAttribute, full, fill, stroke } from "./tool"
6
+ import rotate from "../../rotate"
7
+ import { linearGradient, radialGradient } from "./gradient"
8
+
9
+ // 属性名向下兼容
10
+ let oldAttrName = {
11
+ "font-size": "fontSize",
12
+ "font-family": "fontFamily",
13
+ "arc-start-cap": "arcStartCap",
14
+ "arc-end-cap": "arcEndCap",
15
+ }
16
+
17
+ class SVG {
18
+ readonly name: string = "SVG"
19
+
20
+ // 用于记录配置
21
+ private __config = {
22
+ // 基本设置
23
+ fillStyle: "#000",
24
+ strokeStyle: "#000",
25
+ lineWidth: 1,
26
+
27
+ // 文字对齐方式
28
+ textAlign: <textAlignType>"left",
29
+ textBaseline: <textBaselineType>"middle",
30
+
31
+ // 文字设置
32
+ "fontSize": 16,
33
+ "fontFamily": "sans-serif",
34
+
35
+ // arc二端闭合方式['butt':直线闭合,'round':圆帽闭合]
36
+ "arcStartCap": <arcCapType>"butt",
37
+ "arcEndCap": <arcCapType>"butt",
38
+
39
+ // 虚线设置
40
+ lineDash: [],
41
+ }
42
+
43
+ // 作用的节点
44
+ private __useEl: SVGElement
45
+
46
+ // 路径(和canvas2D的类似)
47
+ private __path: string = ""
48
+ private __currentPosition: number[] = []
49
+
50
+ private __svg: SVGElement
51
+ constructor(svg: SVGElement) {
52
+ this.__svg = svg
53
+ }
54
+
55
+ // 属性设置或获取
56
+ config(params: SVGConfigType) {
57
+ if (typeof params !== "object") {
58
+ return this.__config[oldAttrName[params] || params]
59
+ } else {
60
+ for (let key in params) {
61
+ let _key = oldAttrName[key] || key
62
+ this.__config[_key] = params[key]
63
+ }
64
+ }
65
+ return this
66
+ }
67
+
68
+ /**
69
+ * 基础方法
70
+ * ---------------------------------
71
+ */
72
+
73
+ // 标记应用节点
74
+ // 也就是后续操作都会作用在此节点
75
+ useEl(el: SVGElement) {
76
+ this.__useEl = el
77
+ return this
78
+ }
79
+
80
+ // 获取当前应用的节点
81
+ getEl() {
82
+ return this.__useEl
83
+ }
84
+
85
+ // 追加节点
86
+ // el可以是结点或字符串,字符串的话表示节点名称
87
+ // context可选,表示追加位置,可选,默认根svg
88
+ // 此外,和appendBoard等操作一样,执行后新加入的结点会自动变成应用节点
89
+ appendEl(el: string | SVGElement, context?: SVGElement) {
90
+ context = context || this.__svg
91
+
92
+ if (typeof el == "string") el = toNode(el)
93
+ context.appendChild(el as SVGElement)
94
+
95
+ this.__useEl = el as SVGElement
96
+ return this
97
+ }
98
+
99
+ // 追加绘制板
100
+ // 参数和appendEl类似,只是el如果是字符串的话,表示需要绘制对应什么内容,
101
+ // 比如el = “arc”,表示画弧(不是路径arc),那么我们会创建path节点,因为我们是使用path实现的
102
+ appendBoard(el: string | SVGElement, context?: SVGElement) {
103
+ let _el = el
104
+
105
+ if (typeof el == "string")
106
+ _el =
107
+ {
108
+ text: "text",
109
+ path: "path",
110
+ arc: "path",
111
+ circle: "circle",
112
+ rect: "rect",
113
+ }[el] || ""
114
+
115
+ if (_el == "") throw new Error("Unsupported drawing method:" + el)
116
+ return this.appendEl(_el, context)
117
+ }
118
+
119
+ // 删除当前维护的节点
120
+ remove() {
121
+ if (!this.__useEl) {
122
+ throw new Error("Currently, no node can be deleted.")
123
+ } else {
124
+ ((this.__useEl as SVGElement).parentNode as SVGElement).removeChild(
125
+ this.__useEl
126
+ )
127
+ }
128
+ return this
129
+ }
130
+
131
+ // 设置或获取节点属性
132
+ attr(params: any) {
133
+ if (!this.__useEl)
134
+ throw new Error("Currently, no node can be modified or viewed.")
135
+
136
+ if (typeof params !== "object") {
137
+ return getAttribute(this.__useEl, params)
138
+ } else {
139
+ for (let key in params) {
140
+ setAttribute(this.__useEl, key, params[key])
141
+ }
142
+ return this
143
+ }
144
+ }
145
+
146
+ /**
147
+ * 绘制方法
148
+ * ---------------------------------
149
+ */
150
+
151
+ // 文字
152
+ // deg表示文字旋转角度,是角度值,不是弧度
153
+ fillText(text: string, x: number, y: number, deg: number = 0) {
154
+ initText(this.__useEl, this.__config, x, y, deg)
155
+ this.__useEl.textContent = text
156
+ fill(this.__useEl, this.__config)
157
+ return this
158
+ }
159
+ strokeText(text: string, x: number, y: number, deg: number = 0) {
160
+ initText(this.__useEl, this.__config, x, y, deg)
161
+ this.__useEl.textContent = text
162
+ stroke(this.__useEl, this.__config)
163
+ return this
164
+ }
165
+ fullText(text: string, x: number, y: number, deg: number = 0) {
166
+ initText(this.__useEl, this.__config, x, y, deg)
167
+ this.__useEl.textContent = text
168
+ full(this.__useEl, this.__config)
169
+ return this
170
+ }
171
+
172
+ // 弧
173
+ fillArc(
174
+ cx: number,
175
+ cy: number,
176
+ r1: number,
177
+ r2: number,
178
+ beginDeg: number,
179
+ deg: number
180
+ ) {
181
+ initArc(this.__useEl, this.__config, cx, cy, r1, r2, beginDeg, deg)
182
+ fill(this.__useEl, this.__config)
183
+ return this
184
+ }
185
+ strokeArc(
186
+ cx: number,
187
+ cy: number,
188
+ r1: number,
189
+ r2: number,
190
+ beginDeg: number,
191
+ deg: number
192
+ ) {
193
+ initArc(this.__useEl, this.__config, cx, cy, r1, r2, beginDeg, deg)
194
+ stroke(this.__useEl, this.__config)
195
+ return this
196
+ }
197
+ fullArc(
198
+ cx: number,
199
+ cy: number,
200
+ r1: number,
201
+ r2: number,
202
+ beginDeg: number,
203
+ deg: number
204
+ ) {
205
+ initArc(this.__useEl, this.__config, cx, cy, r1, r2, beginDeg, deg)
206
+ full(this.__useEl, this.__config)
207
+ return this
208
+ }
209
+
210
+ // 圆形
211
+ fillCircle(cx: number, cy: number, r: number) {
212
+ initCircle(this.__useEl, cx, cy, r)
213
+ fill(this.__useEl, this.__config)
214
+ return this
215
+ }
216
+ strokeCircle(cx: number, cy: number, r: number) {
217
+ initCircle(this.__useEl, cx, cy, r)
218
+ stroke(this.__useEl, this.__config)
219
+ return this
220
+ }
221
+ fullCircle(cx: number, cy: number, r: number) {
222
+ initCircle(this.__useEl, cx, cy, r)
223
+ full(this.__useEl, this.__config)
224
+ return this
225
+ }
226
+
227
+ // 矩形
228
+ fillRect(x: number, y: number, width: number, height: number) {
229
+ initRect(this.__useEl, x, y, width, height)
230
+ fill(this.__useEl, this.__config)
231
+ return this
232
+ }
233
+ strokeRect(x: number, y: number, width: number, height: number) {
234
+ initRect(this.__useEl, x, y, width, height)
235
+ stroke(this.__useEl, this.__config)
236
+ return this
237
+ }
238
+ fullRect(x: number, y: number, width: number, height: number) {
239
+ initRect(this.__useEl, x, y, width, height)
240
+ full(this.__useEl, this.__config)
241
+ return this
242
+ }
243
+
244
+ // 路径
245
+ beginPath() {
246
+ this.__currentPosition = []
247
+ this.__path = ""
248
+ return this
249
+ }
250
+ closePath() {
251
+ this.__path += "Z"
252
+ return this
253
+ }
254
+ moveTo(x: number, y: number) {
255
+ this.__currentPosition = [x, y]
256
+
257
+ this.__path += "M" + x + " " + y
258
+ return this
259
+ }
260
+ lineTo(x: number, y: number) {
261
+ this.__currentPosition = [x, y]
262
+
263
+ this.__path += (this.__path == "" ? "M" : "L") + x + " " + y
264
+ return this
265
+ }
266
+ fill() {
267
+ initPath(this.__useEl, this.__path)
268
+ fill(this.__useEl, this.__config)
269
+ return this
270
+ }
271
+ stroke() {
272
+ initPath(this.__useEl, this.__path)
273
+ stroke(this.__useEl, this.__config)
274
+ return this
275
+ }
276
+ full() {
277
+ initPath(this.__useEl, this.__path);
278
+ full(this.__useEl, this.__config)
279
+ return this
280
+ }
281
+
282
+ arc(x: number, y: number, r: number, beginDeg: number, deg: number) {
283
+ let begPosition = rotate(x, y, (beginDeg / 180) * Math.PI, x + r, y)
284
+ let endPosition = rotate(
285
+ x,
286
+ y,
287
+ ((beginDeg + deg) / 180) * Math.PI,
288
+ x + r,
289
+ y
290
+ )
291
+ // 如果当前没有路径,说明是开始的,就移动到正确位置
292
+ if (this.__path == "") {
293
+ this.__path += "M" + begPosition[0] + "," + begPosition[1]
294
+ }
295
+ // 如果当前有路径,位置不正确,应该画到正确位置(和canvas保持一致)
296
+ else if (
297
+ begPosition[0] != this.__currentPosition[0] ||
298
+ begPosition[1] != this.__currentPosition[1]
299
+ ) {
300
+ this.__path += "L" + begPosition[0] + "," + begPosition[1]
301
+ }
302
+ this.__path +=
303
+ "A" +
304
+ r +
305
+ "," +
306
+ r +
307
+ " 0 " +
308
+ (deg > 180 || deg < -180 ? 1 : 0) +
309
+ "," +
310
+ (deg > 0 ? 1 : 0) +
311
+ " " +
312
+ endPosition[0] +
313
+ "," +
314
+ endPosition[1]
315
+ return this
316
+ }
317
+
318
+ // 路径 - 贝塞尔曲线
319
+ quadraticCurveTo(cpx: number, cpy: number, x: number, y: number) {
320
+ this.__path += "Q" + cpx + " " + cpy + "," + x + " " + y
321
+ return this
322
+ }
323
+ bezierCurveTo(
324
+ cp1x: number,
325
+ cp1y: number,
326
+ cp2x: number,
327
+ cp2y: number,
328
+ x: number,
329
+ y: number
330
+ ) {
331
+ this.__path +=
332
+ "C" + cp1x + " " + cp1y + "," + cp2x + " " + cp2y + "," + x + " " + y
333
+ return this
334
+ }
335
+
336
+ // 绑定事件
337
+ bind(eventType: string, callback: (event: Event, target: SVGElement) => void) {
338
+ this.__useEl.addEventListener(eventType, function (event: Event) {
339
+ callback.call(this, event, this)
340
+ }, false)
341
+ return this
342
+ }
343
+
344
+
345
+ // 线性渐变
346
+ createLinearGradient(x0: number, y0: number, x1: number, y1: number) {
347
+ return linearGradient(this.__svg, x0, y0, x1, y1)
348
+ }
349
+
350
+ // 环形渐变
351
+ createRadialGradient(cx: number, cy: number, r: number) {
352
+ return radialGradient(this.__svg, cx, cy, r)
353
+ }
354
+ }
355
+
356
+ export default SVG