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