vislite 1.2.0-alpha.0 → 1.2.0-alpha.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 (75) hide show
  1. package/CHANGELOG +5 -1
  2. package/README.md +165 -142
  3. package/lib/Buffer/index.es.js +1 -1
  4. package/lib/Buffer/index.es.min.js +1 -1
  5. package/lib/Canvas/index.es.js +15 -5
  6. package/lib/Canvas/index.es.min.js +2 -2
  7. package/lib/Cardinal/index.es.js +1 -1
  8. package/lib/Cardinal/index.es.min.js +1 -1
  9. package/lib/Eoap/index.es.js +1 -1
  10. package/lib/Eoap/index.es.min.js +1 -1
  11. package/lib/Hermite/index.es.js +1 -1
  12. package/lib/Hermite/index.es.min.js +1 -1
  13. package/lib/Matrix4/index.es.js +1 -1
  14. package/lib/Matrix4/index.es.min.js +1 -1
  15. package/lib/Mercator/index.es.js +1 -1
  16. package/lib/Mercator/index.es.min.js +1 -1
  17. package/lib/RawCanvas/index.es.js +1 -1
  18. package/lib/RawCanvas/index.es.min.js +1 -1
  19. package/lib/SVG/index.es.js +1 -1
  20. package/lib/SVG/index.es.min.js +1 -1
  21. package/lib/Shader/index.es.js +1 -1
  22. package/lib/Shader/index.es.min.js +1 -1
  23. package/lib/Texture/index.es.js +1 -1
  24. package/lib/Texture/index.es.min.js +1 -1
  25. package/lib/TreeLayout/index.es.js +6 -6
  26. package/lib/TreeLayout/index.es.min.js +1 -1
  27. package/lib/animation/index.es.js +1 -1
  28. package/lib/animation/index.es.min.js +1 -1
  29. package/lib/assemble/index.es.d.ts +3 -0
  30. package/lib/assemble/index.es.js +23 -0
  31. package/lib/assemble/index.es.min.d.ts +3 -0
  32. package/lib/assemble/index.es.min.js +5 -0
  33. package/lib/getLoopColors/index.es.js +1 -1
  34. package/lib/getLoopColors/index.es.min.js +1 -1
  35. package/lib/getWebGLContext/index.es.js +1 -1
  36. package/lib/getWebGLContext/index.es.min.js +1 -1
  37. package/lib/index.umd.js +58 -9
  38. package/lib/index.umd.min.js +2 -2
  39. package/lib/initOption/index.es.d.ts +3 -0
  40. package/lib/initOption/index.es.js +12 -0
  41. package/lib/initOption/index.es.min.d.ts +3 -0
  42. package/lib/initOption/index.es.min.js +5 -0
  43. package/lib/mergeOption/index.es.d.ts +3 -0
  44. package/lib/mergeOption/index.es.js +42 -0
  45. package/lib/mergeOption/index.es.min.d.ts +3 -0
  46. package/lib/mergeOption/index.es.min.js +5 -0
  47. package/lib/move/index.es.js +1 -1
  48. package/lib/move/index.es.min.js +1 -1
  49. package/lib/rotate/index.es.js +1 -1
  50. package/lib/rotate/index.es.min.js +1 -1
  51. package/lib/ruler/index.es.js +1 -1
  52. package/lib/ruler/index.es.min.js +1 -1
  53. package/lib/scale/index.es.js +1 -1
  54. package/lib/scale/index.es.min.js +1 -1
  55. package/lib/throttle/index.es.js +5 -5
  56. package/lib/throttle/index.es.min.js +1 -1
  57. package/package/assemble/index.ts +3 -0
  58. package/package/index.ts +11 -1
  59. package/package/initOption/index.ts +3 -0
  60. package/package/mergeOption/index.ts +3 -0
  61. package/package.json +1 -1
  62. package/src/Canvas.ts +13 -2
  63. package/src/animation.ts +98 -98
  64. package/src/common/canvas/painter.ts +475 -475
  65. package/src/common/option.ts +30 -0
  66. package/src/common/tree/index.ts +2 -2
  67. package/src/common/type.ts +27 -0
  68. package/src/layout/TreeLayout.ts +2 -2
  69. package/src/throttle.ts +2 -2
  70. package/types/Canvas.d.ts +330 -331
  71. package/types/Matrix4.d.ts +1 -1
  72. package/types/assemble.d.ts +6 -0
  73. package/types/index.d.ts +14 -1
  74. package/types/option.d.ts +13 -0
  75. package/src/common/mergeOption.ts +0 -7
@@ -1,476 +1,476 @@
1
- import type { arcCapType } from '../../../types/painterConfig'
2
- import type CanvasOptsType from '../../../types/CanvasOpts'
3
-
4
- import { initText, initArc, initCircle, initRect } from './config'
5
- import texts from './texts'
6
-
7
- class Painter {
8
-
9
- painter: CanvasRenderingContext2D
10
- __region: Painter | null | undefined = null
11
-
12
- __onlyRegion: boolean = false
13
- __onlyView: boolean = false
14
-
15
- __isPainter: boolean
16
-
17
- // 用于记录配置
18
- // 因为部分配置的设置比较特殊,只先记录意图
19
- __specialConfig = {
20
-
21
- // 文字大小
22
- "fontSize": 16,
23
-
24
- // 字体
25
- "fontFamily": "sans-serif",
26
-
27
- // 字重
28
- "fontWeight": 400,
29
-
30
- // 字类型
31
- "fontStyle": "normal",
32
-
33
- // 圆弧开始端闭合方式("butt"直线闭合、"round"圆帽闭合)
34
- "arcStartCap": <arcCapType>'butt',
35
-
36
- // 圆弧结束端闭合方式,和上一个类似
37
- "arcEndCap": <arcCapType>'butt'
38
- } as {
39
- [key: string]: any
40
- }
41
-
42
- private __initConfig = {
43
-
44
- // 填充色或图案
45
- "fillStyle": 'black',
46
-
47
- // 轮廓色或图案
48
- "strokeStyle": 'black',
49
-
50
- // 线条宽度(单位px,下同)
51
- "lineWidth": 1,
52
-
53
- // 文字水平对齐方式("left"左对齐、"center"居中和"right"右对齐)
54
- "textAlign": 'left',
55
-
56
- // 文字垂直对齐方式("middle"垂直居中、"top"上对齐和"bottom"下对齐)
57
- "textBaseline": 'middle',
58
-
59
- // 设置线条虚线,应该是一个数组[number,...]
60
- "lineDash": [],
61
-
62
- // 阴影的模糊系数,默认0,也就是无阴影
63
- "shadowBlur": 0,
64
-
65
- // 阴影的颜色
66
- "shadowColor": "black"
67
-
68
- }
69
-
70
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
71
- constructor(canvas: HTMLCanvasElement, opts: CanvasOptsType = {}, region?: Painter, isPainter = false, scaleSize = 1) {
72
- this.painter = canvas.getContext("2d", opts) as CanvasRenderingContext2D
73
- this.__region = region
74
- this.__isPainter = isPainter
75
-
76
- // 默认配置canvas2D对象已经存在的属性
77
- this.painter.textBaseline = 'middle'
78
- this.painter.textAlign = 'left'
79
- }
80
-
81
- useConfig(key: string, value: any) {
82
- if (this.__region && !this.__onlyView) {
83
- if (['fillStyle', 'strokeStyle', 'shadowBlur', 'shadowColor'].indexOf(key) < 0) {
84
- this.__region.useConfig(key, value)
85
- }
86
- }
87
-
88
- if (this.__isPainter && this.__onlyRegion) return this
89
-
90
- /**
91
- * -----------------------------
92
- * 特殊的设置开始
93
- * -----------------------------
94
- */
95
-
96
- if (key == 'lineDash') {
97
- if (this.painter.setLineDash) this.painter.setLineDash(value)
98
- }
99
-
100
- /**
101
- * -----------------------------
102
- * 常规的配置开始
103
- * -----------------------------
104
- */
105
-
106
- // 如果已经存在默认配置中,说明只需要缓存起来即可
107
- else if (key in this.__specialConfig) {
108
-
109
- // 文字非整数可能存在问题,修复一下
110
- if (key == 'fontSize') {
111
- value = Math.round(value)
112
- }
113
-
114
- this.__specialConfig[key] = value
115
- }
116
-
117
- // 其它情况直接生效即可
118
- else if (key in this.__initConfig) {
119
- this.painter[key] = value
120
- }
121
-
122
- // 如果属性未被定义
123
- else {
124
- throw new Error('Illegal configuration item of painter : ' + key + " !")
125
- }
126
-
127
- return this
128
- }
129
-
130
- // 文字
131
- fillText(text: string, x: number, y: number, deg: number = 0) {
132
- if (this.__region && !this.__onlyView) this.__region.fillText(text, x, y, deg)
133
-
134
- if (this.__isPainter && this.__onlyRegion) return this
135
-
136
- this.painter.save()
137
- initText(this.painter, this.__specialConfig, x, y, deg).fillText(text, 0, 0)
138
- this.painter.restore()
139
- return this
140
- }
141
- strokeText(text: string, x: number, y: number, deg: number = 0) {
142
- if (this.__region && !this.__onlyView) this.__region.strokeText(text, x, y, deg)
143
-
144
- if (this.__isPainter && this.__onlyRegion) return this
145
-
146
- this.painter.save()
147
- initText(this.painter, this.__specialConfig, x, y, deg).strokeText(text, 0, 0)
148
- this.painter.restore()
149
- return this
150
- }
151
- fullText(text: string, x: number, y: number, deg: number = 0) {
152
- if (this.__region && !this.__onlyView) this.__region.fullText(text, x, y, deg)
153
-
154
- if (this.__isPainter && this.__onlyRegion) return this
155
-
156
- this.painter.save()
157
- initText(this.painter, this.__specialConfig, x, y, deg)
158
- this.painter.fillText(text, 0, 0)
159
- this.painter.strokeText(text, 0, 0)
160
- this.painter.restore()
161
- return this
162
- }
163
-
164
- // 多行文字
165
- fillTexts(contents: string, x: number, y: number, width: number, lineHeight: number = 1.2, deg: number = 0) {
166
- let h = 0
167
- if (this.__region && !this.__onlyView) h = this.__region.fillTexts(contents, x, y, width, lineHeight)
168
-
169
- if (this.__isPainter && this.__onlyRegion) return h
170
-
171
- this.painter.save()
172
- initText(this.painter, this.__specialConfig, x, y, deg)
173
-
174
- const height = texts(this.painter, contents, width, this.__specialConfig.fontSize * lineHeight, (content, top) => {
175
- this.painter.fillText(content, 0, top)
176
- })
177
-
178
- this.painter.restore()
179
- return height
180
- }
181
- strokeTexts(contents: string, x: number, y: number, width: number, lineHeight: number = 1.2, deg: number = 0) {
182
- let h = 0
183
- if (this.__region && !this.__onlyView) h = this.__region.fillTexts(contents, x, y, width, lineHeight)
184
-
185
- if (this.__isPainter && this.__onlyRegion) return h
186
-
187
- this.painter.save()
188
- initText(this.painter, this.__specialConfig, x, y, deg)
189
-
190
- const height = texts(this.painter, contents, width, this.__specialConfig.fontSize * lineHeight, (content, top) => {
191
- this.painter.strokeText(content, 0, top)
192
- })
193
-
194
- this.painter.restore()
195
- return height
196
- }
197
- fullTexts(contents: string, x: number, y: number, width: number, lineHeight: number = 1.2, deg: number = 0) {
198
- let h = 0
199
- if (this.__region && !this.__onlyView) h = this.__region.fillTexts(contents, x, y, width, lineHeight)
200
-
201
- if (this.__isPainter && this.__onlyRegion) return h
202
-
203
- this.painter.save()
204
- initText(this.painter, this.__specialConfig, x, y, deg)
205
-
206
- const height = texts(this.painter, contents, width, this.__specialConfig.fontSize * lineHeight, (content, top) => {
207
- this.painter.fillText(content, 0, top)
208
- this.painter.strokeText(content, 0, top)
209
- })
210
-
211
- this.painter.restore()
212
- return height
213
- }
214
-
215
- // 路径
216
- beginPath() {
217
- if (this.__region && !this.__onlyView) this.__region.beginPath()
218
-
219
- if (this.__isPainter && this.__onlyRegion) return this
220
-
221
- this.painter.beginPath()
222
- return this
223
- }
224
- closePath() {
225
- if (this.__region && !this.__onlyView) this.__region.closePath()
226
-
227
- if (this.__isPainter && this.__onlyRegion) return this
228
-
229
- this.painter.closePath()
230
- return this
231
- }
232
- moveTo(x: number, y: number) {
233
- if (this.__region && !this.__onlyView) this.__region.moveTo(x, y)
234
-
235
- if (this.__isPainter && this.__onlyRegion) return this
236
-
237
- // 解决1px模糊问题,别的地方类似原因
238
- this.painter.moveTo(Math.round(x) + 0.5, Math.round(y) + 0.5)
239
- return this
240
- }
241
- lineTo(x: number, y: number) {
242
- if (this.__region && !this.__onlyView) this.__region.lineTo(x, y)
243
-
244
- if (this.__isPainter && this.__onlyRegion) return this
245
-
246
- this.painter.lineTo(Math.round(x) + 0.5, Math.round(y) + 0.5)
247
- return this
248
- }
249
- arc(x: number, y: number, r: number, beginDeg: number, deg: number) {
250
- if (this.__region && !this.__onlyView) this.__region.arc(x, y, r, beginDeg, deg)
251
-
252
- if (this.__isPainter && this.__onlyRegion) return this
253
-
254
- this.painter.arc(x, y, r, beginDeg, beginDeg + deg, deg < 0)
255
- return this
256
- }
257
- fill() {
258
- if (this.__region && !this.__onlyView) this.__region.fill()
259
-
260
- if (this.__isPainter && this.__onlyRegion) return this
261
-
262
- this.painter.fill()
263
- return this
264
- }
265
- stroke() {
266
- if (this.__region && !this.__onlyView) this.__region.stroke()
267
-
268
- if (this.__isPainter && this.__onlyRegion) return this
269
-
270
- this.painter.stroke()
271
- return this
272
- }
273
- full() {
274
- if (this.__region && !this.__onlyView) this.__region.full()
275
-
276
- if (this.__isPainter && this.__onlyRegion) return this
277
-
278
- this.painter.fill()
279
- this.painter.stroke()
280
- return this
281
- }
282
-
283
- save() {
284
- if (this.__region && !this.__onlyView) this.__region.save()
285
-
286
- if (this.__isPainter && this.__onlyRegion) return this
287
-
288
- this.painter.save()
289
- return this
290
- }
291
- restore() {
292
- if (this.__region && !this.__onlyView) this.__region.restore()
293
-
294
- if (this.__isPainter && this.__onlyRegion) return this
295
-
296
- this.painter.restore()
297
- return this
298
- }
299
- clip() {
300
- if (this.__region && !this.__onlyView) this.__region.clip()
301
-
302
- if (this.__isPainter && this.__onlyRegion) return this
303
-
304
- this.painter.clip()
305
- return this
306
- }
307
-
308
- // 路径 - 贝塞尔曲线
309
- quadraticCurveTo(cpx: number, cpy: number, x: number, y: number) {
310
- if (this.__region && !this.__onlyView) this.__region.quadraticCurveTo(cpx, cpy, x, y)
311
-
312
- if (this.__isPainter && this.__onlyRegion) return this
313
-
314
- this.painter.quadraticCurveTo(cpx, cpy, x, y)
315
- return this
316
- }
317
- bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number) {
318
- if (this.__region && !this.__onlyView) this.__region.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
319
-
320
- if (this.__isPainter && this.__onlyRegion) return this
321
-
322
- this.painter.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
323
- return this
324
- }
325
-
326
- // 擦除画面
327
- clearRect(x: number, y: number, w: number, h: number) {
328
- if (this.__region && !this.__onlyView) this.__region.clearRect(x, y, w, h)
329
-
330
- if (this.__isPainter && this.__onlyRegion) return this
331
-
332
- this.painter.clearRect(x, y, w, h)
333
- return this
334
- }
335
-
336
- // 弧
337
- fillArc(cx: number, cy: number, r1: number, r2: number, beginDeg: number, deg: number) {
338
- if (this.__region && !this.__onlyView) this.__region.fillArc(cx, cy, r1, r2, beginDeg, deg)
339
-
340
- if (this.__isPainter && this.__onlyRegion) return this
341
-
342
- initArc(this.painter, this.__specialConfig, cx, cy, r1, r2, beginDeg, deg).fill()
343
- return this
344
- }
345
- strokeArc(cx: number, cy: number, r1: number, r2: number, beginDeg: number, deg: number) {
346
- if (this.__region && !this.__onlyView) this.__region.strokeArc(cx, cy, r1, r2, beginDeg, deg)
347
-
348
- if (this.__isPainter && this.__onlyRegion) return this
349
-
350
- initArc(this.painter, this.__specialConfig, cx, cy, r1, r2, beginDeg, deg).stroke()
351
- return this
352
- }
353
- fullArc(cx: number, cy: number, r1: number, r2: number, beginDeg: number, deg: number) {
354
- if (this.__region && !this.__onlyView) this.__region.fullArc(cx, cy, r1, r2, beginDeg, deg)
355
-
356
- if (this.__isPainter && this.__onlyRegion) return this
357
-
358
- initArc(this.painter, this.__specialConfig, cx, cy, r1, r2, beginDeg, deg)
359
- this.painter.fill()
360
- this.painter.stroke()
361
- return this
362
- }
363
-
364
- // 圆形
365
- fillCircle(cx: number, cy: number, r: number) {
366
- if (this.__region && !this.__onlyView) this.__region.fillCircle(cx, cy, r)
367
-
368
- if (this.__isPainter && this.__onlyRegion) return this
369
-
370
- initCircle(this.painter, cx, cy, r).fill()
371
- return this
372
- }
373
- strokeCircle(cx: number, cy: number, r: number) {
374
- if (this.__region && !this.__onlyView) this.__region.strokeCircle(cx, cy, r)
375
-
376
- if (this.__isPainter && this.__onlyRegion) return this
377
-
378
- initCircle(this.painter, cx, cy, r).stroke()
379
- return this
380
- }
381
- fullCircle(cx: number, cy: number, r: number) {
382
- if (this.__region && !this.__onlyView) this.__region.fullCircle(cx, cy, r)
383
-
384
- if (this.__isPainter && this.__onlyRegion) return this
385
-
386
- initCircle(this.painter, cx, cy, r)
387
- this.painter.fill()
388
- this.painter.stroke()
389
- return this
390
- }
391
-
392
- // 矩形
393
- fillRect(x: number, y: number, width: number, height: number) {
394
- if (this.__region && !this.__onlyView) this.__region.fillRect(x, y, width, height)
395
-
396
- if (this.__isPainter && this.__onlyRegion) return this
397
-
398
- initRect(this.painter, x, y, width, height).fill()
399
- return this
400
- }
401
- strokeRect(x: number, y: number, width: number, height: number) {
402
- if (this.__region && !this.__onlyView) this.__region.strokeRect(x, y, width, height)
403
-
404
- if (this.__isPainter && this.__onlyRegion) return this
405
-
406
- initRect(this.painter, x, y, width, height).stroke()
407
- return this
408
- }
409
- fullRect(x: number, y: number, width: number, height: number) {
410
- if (this.__region && !this.__onlyView) this.__region.fullRect(x, y, width, height)
411
-
412
- if (this.__isPainter && this.__onlyRegion) return this
413
-
414
- initRect(this.painter, x, y, width, height)
415
- this.painter.fill()
416
- this.painter.stroke()
417
- return this
418
- }
419
-
420
- // 渲染绘制(uniapp独有)
421
- draw() {
422
-
423
- // 兼容非uniapp环境,防止误用
424
- // 2024年4月3日 于南京
425
- return Promise.resolve()
426
- }
427
-
428
- // 绘制图片
429
- drawImage(img: CanvasImageSource | string, x: number, y: number, w: number, h: number, isImage: boolean = false) {
430
- return new Promise((resolve) => {
431
- if (this.__region && !this.__onlyView) {
432
- this.__region.fillRect(x, y, w, h)
433
- }
434
-
435
- if (this.__isPainter && this.__onlyRegion) {
436
- resolve({})
437
- return
438
- }
439
-
440
- if (typeof img == 'string' && !isImage) {
441
- const imgInstance = new Image()
442
- imgInstance.onload = () => {
443
- this.painter.drawImage(imgInstance, 0, 0, imgInstance.width, imgInstance.height, x, y, w, h)
444
- resolve({})
445
- }
446
- imgInstance.src = img
447
- } else {
448
- this.painter.drawImage(img as CanvasImageSource, 0, 0, w, h, x, y, w, h)
449
- resolve({})
450
- }
451
-
452
- })
453
- }
454
-
455
- // 扩展绘制方法
456
- install(methods: {
457
- [key: string]: Function
458
- }) {
459
- for (let key in methods) {
460
-
461
- if (key in this) {
462
- throw new Error("VISLite Canvas:Method already exists and cannot be overwritten.")
463
- } else {
464
- this[key] = (...args: any) => {
465
- let value = methods[key].apply(this, args)
466
- if (value != void 0) return value
467
- return this
468
- }
469
- }
470
-
471
- }
472
- return this
473
- }
474
- }
475
-
1
+ import type { arcCapType } from '../../../types/painterConfig'
2
+ import type CanvasOptsType from '../../../types/CanvasOpts'
3
+
4
+ import { initText, initArc, initCircle, initRect } from './config'
5
+ import texts from './texts'
6
+
7
+ class Painter {
8
+
9
+ painter: CanvasRenderingContext2D
10
+ __region: Painter | null | undefined = null
11
+
12
+ __onlyRegion: boolean = false
13
+ __onlyView: boolean = false
14
+
15
+ __isPainter: boolean
16
+
17
+ // 用于记录配置
18
+ // 因为部分配置的设置比较特殊,只先记录意图
19
+ __specialConfig = {
20
+
21
+ // 文字大小
22
+ "fontSize": 16,
23
+
24
+ // 字体
25
+ "fontFamily": "sans-serif",
26
+
27
+ // 字重
28
+ "fontWeight": 400,
29
+
30
+ // 字类型
31
+ "fontStyle": "normal",
32
+
33
+ // 圆弧开始端闭合方式("butt"直线闭合、"round"圆帽闭合)
34
+ "arcStartCap": <arcCapType>'butt',
35
+
36
+ // 圆弧结束端闭合方式,和上一个类似
37
+ "arcEndCap": <arcCapType>'butt'
38
+ } as {
39
+ [key: string]: any
40
+ }
41
+
42
+ private __initConfig = {
43
+
44
+ // 填充色或图案
45
+ "fillStyle": 'black',
46
+
47
+ // 轮廓色或图案
48
+ "strokeStyle": 'black',
49
+
50
+ // 线条宽度(单位px,下同)
51
+ "lineWidth": 1,
52
+
53
+ // 文字水平对齐方式("left"左对齐、"center"居中和"right"右对齐)
54
+ "textAlign": 'left',
55
+
56
+ // 文字垂直对齐方式("middle"垂直居中、"top"上对齐和"bottom"下对齐)
57
+ "textBaseline": 'middle',
58
+
59
+ // 设置线条虚线,应该是一个数组[number,...]
60
+ "lineDash": [],
61
+
62
+ // 阴影的模糊系数,默认0,也就是无阴影
63
+ "shadowBlur": 0,
64
+
65
+ // 阴影的颜色
66
+ "shadowColor": "black"
67
+
68
+ }
69
+
70
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
71
+ constructor(canvas: HTMLCanvasElement, opts: CanvasOptsType = {}, region?: Painter, isPainter = false, scaleSize = 1) {
72
+ this.painter = canvas.getContext("2d", opts) as CanvasRenderingContext2D
73
+ this.__region = region
74
+ this.__isPainter = isPainter
75
+
76
+ // 默认配置canvas2D对象已经存在的属性
77
+ this.painter.textBaseline = 'middle'
78
+ this.painter.textAlign = 'left'
79
+ }
80
+
81
+ useConfig(key: string, value: any) {
82
+ if (this.__region && !this.__onlyView) {
83
+ if (['fillStyle', 'strokeStyle', 'shadowBlur', 'shadowColor'].indexOf(key) < 0) {
84
+ this.__region.useConfig(key, value)
85
+ }
86
+ }
87
+
88
+ if (this.__isPainter && this.__onlyRegion) return this
89
+
90
+ /**
91
+ * -----------------------------
92
+ * 特殊的设置开始
93
+ * -----------------------------
94
+ */
95
+
96
+ if (key == 'lineDash') {
97
+ if (this.painter.setLineDash) this.painter.setLineDash(value)
98
+ }
99
+
100
+ /**
101
+ * -----------------------------
102
+ * 常规的配置开始
103
+ * -----------------------------
104
+ */
105
+
106
+ // 如果已经存在默认配置中,说明只需要缓存起来即可
107
+ else if (key in this.__specialConfig) {
108
+
109
+ // 文字非整数可能存在问题,修复一下
110
+ if (key == 'fontSize') {
111
+ value = Math.round(value)
112
+ }
113
+
114
+ this.__specialConfig[key] = value
115
+ }
116
+
117
+ // 其它情况直接生效即可
118
+ else if (key in this.__initConfig) {
119
+ this.painter[key] = value
120
+ }
121
+
122
+ // 如果属性未被定义
123
+ else {
124
+ throw new Error('Illegal configuration item of painter : ' + key + " !")
125
+ }
126
+
127
+ return this
128
+ }
129
+
130
+ // 文字
131
+ fillText(text: string, x: number, y: number, deg: number = 0) {
132
+ if (this.__region && !this.__onlyView) this.__region.fillText(text, x, y, deg)
133
+
134
+ if (this.__isPainter && this.__onlyRegion) return this
135
+
136
+ this.painter.save()
137
+ initText(this.painter, this.__specialConfig, x, y, deg).fillText(text, 0, 0)
138
+ this.painter.restore()
139
+ return this
140
+ }
141
+ strokeText(text: string, x: number, y: number, deg: number = 0) {
142
+ if (this.__region && !this.__onlyView) this.__region.strokeText(text, x, y, deg)
143
+
144
+ if (this.__isPainter && this.__onlyRegion) return this
145
+
146
+ this.painter.save()
147
+ initText(this.painter, this.__specialConfig, x, y, deg).strokeText(text, 0, 0)
148
+ this.painter.restore()
149
+ return this
150
+ }
151
+ fullText(text: string, x: number, y: number, deg: number = 0) {
152
+ if (this.__region && !this.__onlyView) this.__region.fullText(text, x, y, deg)
153
+
154
+ if (this.__isPainter && this.__onlyRegion) return this
155
+
156
+ this.painter.save()
157
+ initText(this.painter, this.__specialConfig, x, y, deg)
158
+ this.painter.fillText(text, 0, 0)
159
+ this.painter.strokeText(text, 0, 0)
160
+ this.painter.restore()
161
+ return this
162
+ }
163
+
164
+ // 多行文字
165
+ fillTexts(contents: string, x: number, y: number, width: number, lineHeight: number = 1.2, deg: number = 0) {
166
+ let h = 0
167
+ if (this.__region && !this.__onlyView) h = this.__region.fillTexts(contents, x, y, width, lineHeight)
168
+
169
+ if (this.__isPainter && this.__onlyRegion) return h
170
+
171
+ this.painter.save()
172
+ initText(this.painter, this.__specialConfig, x, y, deg)
173
+
174
+ const height = texts(this.painter, contents, width, this.__specialConfig.fontSize * lineHeight, (content, top) => {
175
+ this.painter.fillText(content, 0, top)
176
+ })
177
+
178
+ this.painter.restore()
179
+ return height
180
+ }
181
+ strokeTexts(contents: string, x: number, y: number, width: number, lineHeight: number = 1.2, deg: number = 0) {
182
+ let h = 0
183
+ if (this.__region && !this.__onlyView) h = this.__region.fillTexts(contents, x, y, width, lineHeight)
184
+
185
+ if (this.__isPainter && this.__onlyRegion) return h
186
+
187
+ this.painter.save()
188
+ initText(this.painter, this.__specialConfig, x, y, deg)
189
+
190
+ const height = texts(this.painter, contents, width, this.__specialConfig.fontSize * lineHeight, (content, top) => {
191
+ this.painter.strokeText(content, 0, top)
192
+ })
193
+
194
+ this.painter.restore()
195
+ return height
196
+ }
197
+ fullTexts(contents: string, x: number, y: number, width: number, lineHeight: number = 1.2, deg: number = 0) {
198
+ let h = 0
199
+ if (this.__region && !this.__onlyView) h = this.__region.fillTexts(contents, x, y, width, lineHeight)
200
+
201
+ if (this.__isPainter && this.__onlyRegion) return h
202
+
203
+ this.painter.save()
204
+ initText(this.painter, this.__specialConfig, x, y, deg)
205
+
206
+ const height = texts(this.painter, contents, width, this.__specialConfig.fontSize * lineHeight, (content, top) => {
207
+ this.painter.fillText(content, 0, top)
208
+ this.painter.strokeText(content, 0, top)
209
+ })
210
+
211
+ this.painter.restore()
212
+ return height
213
+ }
214
+
215
+ // 路径
216
+ beginPath() {
217
+ if (this.__region && !this.__onlyView) this.__region.beginPath()
218
+
219
+ if (this.__isPainter && this.__onlyRegion) return this
220
+
221
+ this.painter.beginPath()
222
+ return this
223
+ }
224
+ closePath() {
225
+ if (this.__region && !this.__onlyView) this.__region.closePath()
226
+
227
+ if (this.__isPainter && this.__onlyRegion) return this
228
+
229
+ this.painter.closePath()
230
+ return this
231
+ }
232
+ moveTo(x: number, y: number) {
233
+ if (this.__region && !this.__onlyView) this.__region.moveTo(x, y)
234
+
235
+ if (this.__isPainter && this.__onlyRegion) return this
236
+
237
+ // 解决1px模糊问题,别的地方类似原因
238
+ this.painter.moveTo(Math.round(x) + 0.5, Math.round(y) + 0.5)
239
+ return this
240
+ }
241
+ lineTo(x: number, y: number) {
242
+ if (this.__region && !this.__onlyView) this.__region.lineTo(x, y)
243
+
244
+ if (this.__isPainter && this.__onlyRegion) return this
245
+
246
+ this.painter.lineTo(Math.round(x) + 0.5, Math.round(y) + 0.5)
247
+ return this
248
+ }
249
+ arc(x: number, y: number, r: number, beginDeg: number, deg: number) {
250
+ if (this.__region && !this.__onlyView) this.__region.arc(x, y, r, beginDeg, deg)
251
+
252
+ if (this.__isPainter && this.__onlyRegion) return this
253
+
254
+ this.painter.arc(x, y, r, beginDeg, beginDeg + deg, deg < 0)
255
+ return this
256
+ }
257
+ fill() {
258
+ if (this.__region && !this.__onlyView) this.__region.fill()
259
+
260
+ if (this.__isPainter && this.__onlyRegion) return this
261
+
262
+ this.painter.fill()
263
+ return this
264
+ }
265
+ stroke() {
266
+ if (this.__region && !this.__onlyView) this.__region.stroke()
267
+
268
+ if (this.__isPainter && this.__onlyRegion) return this
269
+
270
+ this.painter.stroke()
271
+ return this
272
+ }
273
+ full() {
274
+ if (this.__region && !this.__onlyView) this.__region.full()
275
+
276
+ if (this.__isPainter && this.__onlyRegion) return this
277
+
278
+ this.painter.fill()
279
+ this.painter.stroke()
280
+ return this
281
+ }
282
+
283
+ save() {
284
+ if (this.__region && !this.__onlyView) this.__region.save()
285
+
286
+ if (this.__isPainter && this.__onlyRegion) return this
287
+
288
+ this.painter.save()
289
+ return this
290
+ }
291
+ restore() {
292
+ if (this.__region && !this.__onlyView) this.__region.restore()
293
+
294
+ if (this.__isPainter && this.__onlyRegion) return this
295
+
296
+ this.painter.restore()
297
+ return this
298
+ }
299
+ clip() {
300
+ if (this.__region && !this.__onlyView) this.__region.clip()
301
+
302
+ if (this.__isPainter && this.__onlyRegion) return this
303
+
304
+ this.painter.clip()
305
+ return this
306
+ }
307
+
308
+ // 路径 - 贝塞尔曲线
309
+ quadraticCurveTo(cpx: number, cpy: number, x: number, y: number) {
310
+ if (this.__region && !this.__onlyView) this.__region.quadraticCurveTo(cpx, cpy, x, y)
311
+
312
+ if (this.__isPainter && this.__onlyRegion) return this
313
+
314
+ this.painter.quadraticCurveTo(cpx, cpy, x, y)
315
+ return this
316
+ }
317
+ bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number) {
318
+ if (this.__region && !this.__onlyView) this.__region.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
319
+
320
+ if (this.__isPainter && this.__onlyRegion) return this
321
+
322
+ this.painter.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
323
+ return this
324
+ }
325
+
326
+ // 擦除画面
327
+ clearRect(x: number, y: number, w: number, h: number) {
328
+ if (this.__region && !this.__onlyView) this.__region.clearRect(x, y, w, h)
329
+
330
+ if (this.__isPainter && this.__onlyRegion) return this
331
+
332
+ this.painter.clearRect(x, y, w, h)
333
+ return this
334
+ }
335
+
336
+ // 弧
337
+ fillArc(cx: number, cy: number, r1: number, r2: number, beginDeg: number, deg: number) {
338
+ if (this.__region && !this.__onlyView) this.__region.fillArc(cx, cy, r1, r2, beginDeg, deg)
339
+
340
+ if (this.__isPainter && this.__onlyRegion) return this
341
+
342
+ initArc(this.painter, this.__specialConfig, cx, cy, r1, r2, beginDeg, deg).fill()
343
+ return this
344
+ }
345
+ strokeArc(cx: number, cy: number, r1: number, r2: number, beginDeg: number, deg: number) {
346
+ if (this.__region && !this.__onlyView) this.__region.strokeArc(cx, cy, r1, r2, beginDeg, deg)
347
+
348
+ if (this.__isPainter && this.__onlyRegion) return this
349
+
350
+ initArc(this.painter, this.__specialConfig, cx, cy, r1, r2, beginDeg, deg).stroke()
351
+ return this
352
+ }
353
+ fullArc(cx: number, cy: number, r1: number, r2: number, beginDeg: number, deg: number) {
354
+ if (this.__region && !this.__onlyView) this.__region.fullArc(cx, cy, r1, r2, beginDeg, deg)
355
+
356
+ if (this.__isPainter && this.__onlyRegion) return this
357
+
358
+ initArc(this.painter, this.__specialConfig, cx, cy, r1, r2, beginDeg, deg)
359
+ this.painter.fill()
360
+ this.painter.stroke()
361
+ return this
362
+ }
363
+
364
+ // 圆形
365
+ fillCircle(cx: number, cy: number, r: number) {
366
+ if (this.__region && !this.__onlyView) this.__region.fillCircle(cx, cy, r)
367
+
368
+ if (this.__isPainter && this.__onlyRegion) return this
369
+
370
+ initCircle(this.painter, cx, cy, r).fill()
371
+ return this
372
+ }
373
+ strokeCircle(cx: number, cy: number, r: number) {
374
+ if (this.__region && !this.__onlyView) this.__region.strokeCircle(cx, cy, r)
375
+
376
+ if (this.__isPainter && this.__onlyRegion) return this
377
+
378
+ initCircle(this.painter, cx, cy, r).stroke()
379
+ return this
380
+ }
381
+ fullCircle(cx: number, cy: number, r: number) {
382
+ if (this.__region && !this.__onlyView) this.__region.fullCircle(cx, cy, r)
383
+
384
+ if (this.__isPainter && this.__onlyRegion) return this
385
+
386
+ initCircle(this.painter, cx, cy, r)
387
+ this.painter.fill()
388
+ this.painter.stroke()
389
+ return this
390
+ }
391
+
392
+ // 矩形
393
+ fillRect(x: number, y: number, width: number, height: number) {
394
+ if (this.__region && !this.__onlyView) this.__region.fillRect(x, y, width, height)
395
+
396
+ if (this.__isPainter && this.__onlyRegion) return this
397
+
398
+ initRect(this.painter, x, y, width, height).fill()
399
+ return this
400
+ }
401
+ strokeRect(x: number, y: number, width: number, height: number) {
402
+ if (this.__region && !this.__onlyView) this.__region.strokeRect(x, y, width, height)
403
+
404
+ if (this.__isPainter && this.__onlyRegion) return this
405
+
406
+ initRect(this.painter, x, y, width, height).stroke()
407
+ return this
408
+ }
409
+ fullRect(x: number, y: number, width: number, height: number) {
410
+ if (this.__region && !this.__onlyView) this.__region.fullRect(x, y, width, height)
411
+
412
+ if (this.__isPainter && this.__onlyRegion) return this
413
+
414
+ initRect(this.painter, x, y, width, height)
415
+ this.painter.fill()
416
+ this.painter.stroke()
417
+ return this
418
+ }
419
+
420
+ // 渲染绘制(uniapp独有)
421
+ draw() {
422
+
423
+ // 兼容非uniapp环境,防止误用
424
+ // 2024年4月3日 于南京
425
+ return Promise.resolve()
426
+ }
427
+
428
+ // 绘制图片
429
+ drawImage(img: CanvasImageSource | string, x: number, y: number, w: number, h: number, isImage: boolean = false) {
430
+ return new Promise((resolve) => {
431
+ if (this.__region && !this.__onlyView) {
432
+ this.__region.fillRect(x, y, w, h)
433
+ }
434
+
435
+ if (this.__isPainter && this.__onlyRegion) {
436
+ resolve({})
437
+ return
438
+ }
439
+
440
+ if (typeof img == 'string' && !isImage) {
441
+ const imgInstance = new Image()
442
+ imgInstance.onload = () => {
443
+ this.painter.drawImage(imgInstance, 0, 0, imgInstance.width, imgInstance.height, x, y, w, h)
444
+ resolve({})
445
+ }
446
+ imgInstance.src = img
447
+ } else {
448
+ this.painter.drawImage(img as CanvasImageSource, 0, 0, w, h, x, y, w, h)
449
+ resolve({})
450
+ }
451
+
452
+ })
453
+ }
454
+
455
+ // 扩展绘制方法
456
+ install(methods: {
457
+ [key: string]: Function
458
+ }): any {
459
+ for (let key in methods) {
460
+
461
+ if (key in this) {
462
+ throw new Error("VISLite Canvas:Method already exists and cannot be overwritten.")
463
+ } else {
464
+ this[key] = (...args: any) => {
465
+ let value = methods[key].apply(this, args)
466
+ if (value != void 0) return value
467
+ return this
468
+ }
469
+ }
470
+
471
+ }
472
+ return this
473
+ }
474
+ }
475
+
476
476
  export default Painter