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