vislite 0.6.0-alpha.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/.eslintrc.js +3 -1
  2. package/CHANGELOG +1 -1
  3. package/README.md +11 -3
  4. package/lib/Buffer/index.umd.js +2 -2
  5. package/lib/Buffer/index.umd.min.js +2 -2
  6. package/lib/Canvas/index.umd.js +2 -2
  7. package/lib/Canvas/index.umd.min.js +2 -2
  8. package/lib/Cardinal/index.umd.js +2 -2
  9. package/lib/Cardinal/index.umd.min.js +2 -2
  10. package/lib/Eoap/index.umd.js +2 -2
  11. package/lib/Eoap/index.umd.min.js +2 -2
  12. package/lib/Geometry/index.umd.js +2 -2
  13. package/lib/Geometry/index.umd.min.js +2 -2
  14. package/lib/Hermite/index.umd.js +2 -2
  15. package/lib/Hermite/index.umd.min.js +2 -2
  16. package/lib/Matrix4/index.umd.js +2 -2
  17. package/lib/Matrix4/index.umd.min.js +2 -2
  18. package/lib/Mercator/index.umd.js +2 -2
  19. package/lib/Mercator/index.umd.min.js +2 -2
  20. package/lib/OralCanvas/index.es.js +2 -2
  21. package/lib/OralCanvas/index.es.min.js +2 -2
  22. package/lib/OralWebGL/index.es.js +2 -2
  23. package/lib/OralWebGL/index.es.min.js +2 -2
  24. package/lib/SVG/index.umd.js +2 -2
  25. package/lib/SVG/index.umd.min.js +2 -2
  26. package/lib/Shader/index.umd.js +2 -2
  27. package/lib/Shader/index.umd.min.js +2 -2
  28. package/lib/Texture/index.umd.js +2 -2
  29. package/lib/Texture/index.umd.min.js +2 -2
  30. package/lib/TreeLayout/index.umd.js +2 -2
  31. package/lib/TreeLayout/index.umd.min.js +2 -2
  32. package/lib/WebGL/index.umd.js +2 -2
  33. package/lib/WebGL/index.umd.min.js +2 -2
  34. package/lib/animation/index.umd.js +2 -2
  35. package/lib/animation/index.umd.min.js +2 -2
  36. package/lib/formatColor/index.umd.js +2 -2
  37. package/lib/formatColor/index.umd.min.js +2 -2
  38. package/lib/getLoopColors/index.umd.js +2 -2
  39. package/lib/getLoopColors/index.umd.min.js +2 -2
  40. package/lib/getWebGLContext/index.umd.js +2 -2
  41. package/lib/getWebGLContext/index.umd.min.js +2 -2
  42. package/lib/index.umd.js +2 -2
  43. package/lib/index.umd.min.js +2 -2
  44. package/lib/move/index.umd.js +2 -2
  45. package/lib/move/index.umd.min.js +2 -2
  46. package/lib/resizeObserver/index.umd.js +2 -2
  47. package/lib/resizeObserver/index.umd.min.js +2 -2
  48. package/lib/rotate/index.umd.js +2 -2
  49. package/lib/rotate/index.umd.min.js +2 -2
  50. package/lib/ruler/index.umd.js +2 -2
  51. package/lib/ruler/index.umd.min.js +2 -2
  52. package/lib/scale/index.umd.js +2 -2
  53. package/lib/scale/index.umd.min.js +2 -2
  54. package/lib/throttle/index.umd.js +2 -2
  55. package/lib/throttle/index.umd.min.js +2 -2
  56. package/lib/viewHandler/index.umd.js +2 -2
  57. package/lib/viewHandler/index.umd.min.js +2 -2
  58. package/package.json +1 -1
  59. package/src/Canvas.ts +70 -70
  60. package/src/Eoap.ts +66 -66
  61. package/src/Geometry.ts +82 -82
  62. package/src/Matrix4/move.ts +12 -12
  63. package/src/Matrix4/rotate.ts +15 -15
  64. package/src/Matrix4/transform.ts +64 -64
  65. package/src/Mercator.ts +27 -27
  66. package/src/SVG.ts +24 -24
  67. package/src/WebGL.ts +66 -66
  68. package/src/animation.ts +98 -98
  69. package/src/common/assemble.ts +22 -22
  70. package/src/common/canvas/arc.ts +51 -51
  71. package/src/common/canvas/config.ts +158 -158
  72. package/src/common/canvas/gradient.ts +29 -29
  73. package/src/common/canvas/painter.ts +417 -417
  74. package/src/common/geometry/prism-horizontal.ts +35 -35
  75. package/src/common/geometry/prism-vertical.ts +40 -40
  76. package/src/common/geometry/sphere-fragment.ts +40 -40
  77. package/src/common/geometry/tool/circle.ts +11 -11
  78. package/src/common/mergeOption.ts +6 -6
  79. package/src/common/setStyle.ts +5 -5
  80. package/src/common/svg/config.ts +186 -186
  81. package/src/common/svg/gradient.ts +63 -63
  82. package/src/common/svg/index.ts +356 -356
  83. package/src/common/svg/tool.ts +44 -44
  84. package/src/common/tree/toInnerTree.ts +57 -57
  85. package/src/common/tree/toPlainTree.ts +132 -132
  86. package/src/common/webgl/buffer.ts +79 -79
  87. package/src/common/webgl/doDraw.ts +108 -108
  88. package/src/common/webgl/getColorFactory.ts +26 -26
  89. package/src/common/webgl/getShader.ts +48 -48
  90. package/src/common/webgl/getTexture.ts +11 -11
  91. package/src/common/webgl/getWebGLContext.ts +28 -28
  92. package/src/common/webgl/index.ts +224 -224
  93. package/src/common/webgl/shader.ts +75 -75
  94. package/src/common/webgl/texture.ts +97 -97
  95. package/src/formatColor.ts +44 -44
  96. package/src/getLoopColors.ts +42 -42
  97. package/src/interpolation/Cardinal.ts +110 -110
  98. package/src/interpolation/Hermite.ts +65 -65
  99. package/src/move.ts +7 -7
  100. package/src/resizeObserver.ts +37 -37
  101. package/src/rotate.ts +7 -7
  102. package/src/throttle.ts +52 -52
  103. package/src/viewHandler.ts +158 -158
  104. package/types/CanvasOption.d.ts +8 -8
@@ -1,356 +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
- import { linearGradient, radialGradient } from "./gradient"
8
-
9
- // 属性名向下兼容
10
- const 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 (const key in params) {
61
- const _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 (const 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
- const begPosition = rotate(x, y, (beginDeg / 180) * Math.PI, x + r, y)
284
- const 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
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
+ const 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 (const key in params) {
61
+ const _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 (const 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
+ const begPosition = rotate(x, y, (beginDeg / 180) * Math.PI, x + r, y)
284
+ const 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