vislite 0.7.0-alpha.0 → 0.7.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 (85) hide show
  1. package/CHANGELOG +8 -3
  2. package/README.md +20 -1
  3. package/lib/Buffer/index.umd.js +2 -2
  4. package/lib/Buffer/index.umd.min.js +2 -2
  5. package/lib/Canvas/index.umd.js +4 -4
  6. package/lib/Canvas/index.umd.min.js +3 -3
  7. package/lib/Cardinal/index.umd.js +2 -2
  8. package/lib/Cardinal/index.umd.min.js +2 -2
  9. package/lib/Eoap/index.umd.js +2 -2
  10. package/lib/Eoap/index.umd.min.js +2 -2
  11. package/lib/Geometry/index.umd.js +69 -8
  12. package/lib/Geometry/index.umd.min.js +3 -3
  13. package/lib/Hermite/index.umd.js +2 -2
  14. package/lib/Hermite/index.umd.min.js +2 -2
  15. package/lib/Matrix4/index.umd.js +2 -2
  16. package/lib/Matrix4/index.umd.min.js +2 -2
  17. package/lib/Mercator/index.umd.js +2 -2
  18. package/lib/Mercator/index.umd.min.js +2 -2
  19. package/lib/OralCanvas/index.es.js +3 -3
  20. package/lib/OralCanvas/index.es.min.js +3 -3
  21. package/lib/OralWebGL/index.es.js +21 -18
  22. package/lib/OralWebGL/index.es.min.js +3 -3
  23. package/lib/SVG/index.umd.js +2 -2
  24. package/lib/SVG/index.umd.min.js +2 -2
  25. package/lib/Shader/index.umd.js +3 -2
  26. package/lib/Shader/index.umd.min.js +3 -3
  27. package/lib/Texture/index.umd.js +2 -2
  28. package/lib/Texture/index.umd.min.js +2 -2
  29. package/lib/TreeLayout/index.umd.js +13 -11
  30. package/lib/TreeLayout/index.umd.min.js +3 -3
  31. package/lib/WebGL/index.umd.js +22 -19
  32. package/lib/WebGL/index.umd.min.js +3 -3
  33. package/lib/animation/index.umd.js +2 -2
  34. package/lib/animation/index.umd.min.js +2 -2
  35. package/lib/formatColor/index.umd.js +2 -2
  36. package/lib/formatColor/index.umd.min.js +2 -2
  37. package/lib/getLoopColors/index.umd.js +2 -2
  38. package/lib/getLoopColors/index.umd.min.js +2 -2
  39. package/lib/getWebGLContext/index.umd.js +4 -4
  40. package/lib/getWebGLContext/index.umd.min.js +3 -3
  41. package/lib/index.umd.js +104 -38
  42. package/lib/index.umd.min.js +3 -3
  43. package/lib/move/index.umd.js +2 -2
  44. package/lib/move/index.umd.min.js +2 -2
  45. package/lib/resizeObserver/index.umd.js +2 -2
  46. package/lib/resizeObserver/index.umd.min.js +2 -2
  47. package/lib/rotate/index.umd.js +2 -2
  48. package/lib/rotate/index.umd.min.js +2 -2
  49. package/lib/ruler/index.umd.js +4 -4
  50. package/lib/ruler/index.umd.min.js +3 -3
  51. package/lib/scale/index.umd.js +2 -2
  52. package/lib/scale/index.umd.min.js +2 -2
  53. package/lib/throttle/index.umd.js +2 -2
  54. package/lib/throttle/index.umd.min.js +2 -2
  55. package/lib/viewHandler/index.umd.js +2 -2
  56. package/lib/viewHandler/index.umd.min.js +2 -2
  57. package/package.json +1 -1
  58. package/src/Canvas.ts +70 -70
  59. package/src/Geometry.ts +126 -82
  60. package/src/Matrix4/rotate.ts +15 -15
  61. package/src/Matrix4/transform.ts +64 -64
  62. package/src/WebGL.ts +66 -66
  63. package/src/common/assemble.ts +22 -22
  64. package/src/common/canvas/index.ts +177 -177
  65. package/src/common/canvas/painter.ts +419 -417
  66. package/src/common/geometry/prism-vertical.ts +40 -40
  67. package/src/common/geometry/tool/rotateLine.ts +36 -0
  68. package/src/common/setStyle.ts +5 -5
  69. package/src/common/svg/config.ts +190 -186
  70. package/src/common/svg/tool.ts +44 -44
  71. package/src/common/tree/toInnerTree.ts +59 -57
  72. package/src/common/tree/toPlainTree.ts +132 -132
  73. package/src/common/webgl/buffer.ts +79 -79
  74. package/src/common/webgl/doDraw.ts +108 -108
  75. package/src/common/webgl/getWebGLContext.ts +28 -28
  76. package/src/common/webgl/index.ts +226 -224
  77. package/src/common/webgl/shader.ts +75 -75
  78. package/src/common/webgl/texture.ts +97 -97
  79. package/src/layout/TreeLayout.ts +188 -186
  80. package/src/resizeObserver.ts +37 -37
  81. package/src/ruler.ts +209 -209
  82. package/src/viewHandler.ts +160 -158
  83. package/types/Canvas.d.ts +308 -308
  84. package/types/Geometry.d.ts +65 -40
  85. package/types/TreeConfig.d.ts +36 -24
@@ -1,64 +1,64 @@
1
- /**
2
- * 针对任意射线(a1,b1,c1)->(a2,b2,c2)
3
- * 计算出两个变换矩阵
4
- * 分别为:任意射线变成OZ轴变换矩阵 + OZ轴变回原来的射线的变换矩阵
5
- */
6
- export default function (a1: number, b1: number, c1: number, a2: number, b2: number, c2: number) {
7
-
8
- if (typeof a1 === 'number' && typeof b1 === 'number') {
9
-
10
- // 如果设置两个点
11
- // 表示二维上围绕某个点旋转
12
- if (typeof c1 !== 'number') {
13
- c1 = 0
14
- a2 = a1
15
- b2 = b1
16
- c2 = 1
17
- }
18
- // 只设置三个点(设置不足六个点都认为只设置了三个点)
19
- // 表示围绕从原点出发的射线旋转
20
- else if (typeof a2 !== 'number' || typeof b2 !== 'number' || typeof c2 !== 'number') {
21
- a2 = a1
22
- b2 = b1
23
- c2 = c1
24
- a1 = 0
25
- b1 = 0
26
- c1 = 0
27
- }
28
-
29
- if (a1 == a2 && b1 == b2 && c1 == c2) throw new Error('It\'s not a legitimate ray!')
30
-
31
- const sqrt1 = Math.sqrt((a2 - a1) * (a2 - a1) + (b2 - b1) * (b2 - b1)),
32
- cos1 = sqrt1 != 0 ? (b2 - b1) / sqrt1 : 1,
33
- sin1 = sqrt1 != 0 ? (a2 - a1) / sqrt1 : 0,
34
-
35
- b = (a2 - a1) * sin1 + (b2 - b1) * cos1,
36
- c = c2 - c1,
37
-
38
- sqrt2 = Math.sqrt(b * b + c * c),
39
- cos2 = sqrt2 != 0 ? c / sqrt2 : 1,
40
- sin2 = sqrt2 != 0 ? b / sqrt2 : 0
41
-
42
- return [
43
-
44
- // 任意射线变成OZ轴变换矩阵
45
- [
46
- cos1, cos2 * sin1, sin1 * sin2, 0,
47
- -sin1, cos1 * cos2, cos1 * sin2, 0,
48
- 0, -sin2, cos2, 0,
49
- b1 * sin1 - a1 * cos1, c1 * sin2 - a1 * sin1 * cos2 - b1 * cos1 * cos2, -a1 * sin1 * sin2 - b1 * cos1 * sin2 - c1 * cos2, 1
50
- ],
51
-
52
- // OZ轴变回原来的射线的变换矩阵
53
- [
54
- cos1, -sin1, 0, 0,
55
- cos2 * sin1, cos2 * cos1, -sin2, 0,
56
- sin1 * sin2, cos1 * sin2, cos2, 0,
57
- a1, b1, c1, 1
58
- ]
59
-
60
- ]
61
- } else {
62
- throw new Error('a1 and b1 is required!')
63
- }
64
- }
1
+ /**
2
+ * 针对任意射线(a1,b1,c1)->(a2,b2,c2)
3
+ * 计算出两个变换矩阵
4
+ * 分别为:任意射线变成OZ轴变换矩阵 + OZ轴变回原来的射线的变换矩阵
5
+ */
6
+ export default function (a1: number, b1: number, c1?: number, a2?: number, b2?: number, c2?: number) {
7
+
8
+ if (typeof a1 === 'number' && typeof b1 === 'number') {
9
+
10
+ // 如果设置两个点
11
+ // 表示二维上围绕某个点旋转
12
+ if (typeof c1 !== 'number') {
13
+ c1 = 0
14
+ a2 = a1
15
+ b2 = b1
16
+ c2 = 1
17
+ }
18
+ // 只设置三个点(设置不足六个点都认为只设置了三个点)
19
+ // 表示围绕从原点出发的射线旋转
20
+ else if (typeof a2 !== 'number' || typeof b2 !== 'number' || typeof c2 !== 'number') {
21
+ a2 = a1
22
+ b2 = b1
23
+ c2 = c1
24
+ a1 = 0
25
+ b1 = 0
26
+ c1 = 0
27
+ }
28
+
29
+ if (a1 == a2 && b1 == b2 && c1 == c2) throw new Error('It\'s not a legitimate ray!')
30
+
31
+ const sqrt1 = Math.sqrt((a2 - a1) * (a2 - a1) + (b2 - b1) * (b2 - b1)),
32
+ cos1 = sqrt1 != 0 ? (b2 - b1) / sqrt1 : 1,
33
+ sin1 = sqrt1 != 0 ? (a2 - a1) / sqrt1 : 0,
34
+
35
+ b = (a2 - a1) * sin1 + (b2 - b1) * cos1,
36
+ c = c2 - c1,
37
+
38
+ sqrt2 = Math.sqrt(b * b + c * c),
39
+ cos2 = sqrt2 != 0 ? c / sqrt2 : 1,
40
+ sin2 = sqrt2 != 0 ? b / sqrt2 : 0
41
+
42
+ return [
43
+
44
+ // 任意射线变成OZ轴变换矩阵
45
+ [
46
+ cos1, cos2 * sin1, sin1 * sin2, 0,
47
+ -sin1, cos1 * cos2, cos1 * sin2, 0,
48
+ 0, -sin2, cos2, 0,
49
+ b1 * sin1 - a1 * cos1, c1 * sin2 - a1 * sin1 * cos2 - b1 * cos1 * cos2, -a1 * sin1 * sin2 - b1 * cos1 * sin2 - c1 * cos2, 1
50
+ ],
51
+
52
+ // OZ轴变回原来的射线的变换矩阵
53
+ [
54
+ cos1, -sin1, 0, 0,
55
+ cos2 * sin1, cos2 * cos1, -sin2, 0,
56
+ sin1 * sin2, cos1 * sin2, cos2, 0,
57
+ a1, b1, c1, 1
58
+ ]
59
+
60
+ ]
61
+ } else {
62
+ throw new Error('a1 and b1 is required!')
63
+ }
64
+ }
package/src/WebGL.ts CHANGED
@@ -1,67 +1,67 @@
1
- import type WebGLOptionType from '../types/WebGLOption'
2
- import type WebGLType from '../types/WebGL'
3
-
4
- import OralWebGL from './common/webgl/index'
5
- import mergeOption from './common/mergeOption'
6
-
7
- class WebGL extends OralWebGL implements WebGLType {
8
- constructor(el: HTMLElement | null, option: WebGLOptionType = {}) {
9
- if (!el) {
10
- throw new Error("VISLite WebGL:The mount point requires an HTMLElement type but encountered null.")
11
- }
12
-
13
- option = mergeOption(option, {
14
- region: true
15
- })
16
-
17
- const width = el.clientWidth, height = el.clientHeight
18
-
19
- let ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement
20
-
21
- const _el = el as any
22
-
23
- // 如果已经初始化过了
24
- if (_el._vislite_canvas_) {
25
- ViewCanvas = _el._vislite_canvas_[0]
26
- RegionCanvas = _el._vislite_canvas_[1]
27
- }
28
-
29
- // 否则就初始化
30
- else {
31
-
32
- if (option.region) {
33
-
34
- RegionCanvas = document.createElement('canvas')
35
- el.appendChild(RegionCanvas)
36
-
37
- RegionCanvas.style.position = 'absolute'
38
- RegionCanvas.style.zIndex = "-1"
39
- RegionCanvas.style.opacity = "0"
40
-
41
- }
42
-
43
- ViewCanvas = document.createElement('canvas')
44
- el.appendChild(ViewCanvas)
45
-
46
- _el._vislite_canvas_ = [ViewCanvas, RegionCanvas]
47
-
48
- el.setAttribute('vislite', 'WebGL')
49
- }
50
-
51
- // 设置画布大小
52
- for (const canvas of [ViewCanvas, RegionCanvas]) {
53
-
54
- if (canvas) {
55
- canvas.style.width = width + "px"
56
- canvas.setAttribute('width', width + "")
57
-
58
- canvas.style.height = height + "px"
59
- canvas.setAttribute('height', height + "")
60
- }
61
- }
62
-
63
- super(ViewCanvas, RegionCanvas)
64
- }
65
- }
66
-
1
+ import type WebGLOptionType from '../types/WebGLOption'
2
+ import type WebGLType from '../types/WebGL'
3
+
4
+ import OralWebGL from './common/webgl/index'
5
+ import mergeOption from './common/mergeOption'
6
+
7
+ class WebGL extends OralWebGL implements WebGLType {
8
+ constructor(el: HTMLElement | null, option: WebGLOptionType = {}) {
9
+ if (!el) {
10
+ throw new Error("VISLite WebGL:The mount point requires an HTMLElement type but encountered null.")
11
+ }
12
+
13
+ option = mergeOption(option, {
14
+ region: true
15
+ })
16
+
17
+ const width = el.clientWidth, height = el.clientHeight
18
+
19
+ let ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement | null = null
20
+
21
+ const _el = el as any
22
+
23
+ // 如果已经初始化过了
24
+ if (_el._vislite_canvas_) {
25
+ ViewCanvas = _el._vislite_canvas_[0]
26
+ RegionCanvas = _el._vislite_canvas_[1]
27
+ }
28
+
29
+ // 否则就初始化
30
+ else {
31
+
32
+ if (option.region) {
33
+
34
+ RegionCanvas = document.createElement('canvas')
35
+ el.appendChild(RegionCanvas)
36
+
37
+ RegionCanvas.style.position = 'absolute'
38
+ RegionCanvas.style.zIndex = "-1"
39
+ RegionCanvas.style.opacity = "0"
40
+
41
+ }
42
+
43
+ ViewCanvas = document.createElement('canvas')
44
+ el.appendChild(ViewCanvas)
45
+
46
+ _el._vislite_canvas_ = [ViewCanvas, RegionCanvas]
47
+
48
+ el.setAttribute('vislite', 'WebGL')
49
+ }
50
+
51
+ // 设置画布大小
52
+ for (const canvas of [ViewCanvas, RegionCanvas]) {
53
+
54
+ if (canvas) {
55
+ canvas.style.width = width + "px"
56
+ canvas.setAttribute('width', width + "")
57
+
58
+ canvas.style.height = height + "px"
59
+ canvas.setAttribute('height', height + "")
60
+ }
61
+ }
62
+
63
+ super(ViewCanvas, RegionCanvas)
64
+ }
65
+ }
66
+
67
67
  export default WebGL
@@ -1,23 +1,23 @@
1
- export default function (begin: number, end: number, step: number, count: number) {
2
- const val = []
3
- for (let index = 0; index < count; index++) val[index] = begin
4
-
5
- // 非常类似进制数,每次调用都+1
6
- return function () {
7
- for (let i = 0; i < count; i++) {
8
-
9
- // 如果当前位可以进1
10
- if (val[i] + step < end) {
11
- val[i] = +(val[i] + step).toFixed(7)
12
- break
13
- }
14
-
15
- // 如果当前位不可以,那当前位归0,尝试下一位
16
- else if (i < count - 1) {
17
- val[i] = begin
18
- }
19
- }
20
-
21
- return val
22
- }
1
+ export default function (begin: number, end: number, step: number, count: number) {
2
+ const val: Array<number> = []
3
+ for (let index = 0; index < count; index++) val[index] = begin
4
+
5
+ // 非常类似进制数,每次调用都+1
6
+ return function () {
7
+ for (let i = 0; i < count; i++) {
8
+
9
+ // 如果当前位可以进1
10
+ if (val[i] + step < end) {
11
+ val[i] = +(val[i] + step).toFixed(7)
12
+ break
13
+ }
14
+
15
+ // 如果当前位不可以,那当前位归0,尝试下一位
16
+ else if (i < count - 1) {
17
+ val[i] = begin
18
+ }
19
+ }
20
+
21
+ return val
22
+ }
23
23
  }
@@ -1,177 +1,177 @@
1
- import type CanvasConfigType from "../../../types/CanvasConfig"
2
- import type CanvasOptsType from '../../../types/CanvasOpts'
3
-
4
- import PainterRender from "./painter"
5
- import assemble from "../assemble"
6
- import { linearGradient, radialGradient } from "./gradient"
7
- import { initText } from './config'
8
-
9
- // 属性名向下兼容
10
- const oldAttrName = {
11
- "font-size": "fontSize",
12
- "font-family": "fontFamily",
13
- "font-weight": "fontWeight",
14
- "font-style": "fontStyle",
15
- "arc-start-cap": "arcStartCap",
16
- "arc-end-cap": "arcEndCap",
17
- }
18
-
19
- class Canvas extends PainterRender {
20
- readonly name: string = "Canvas"
21
-
22
- private __regionList = {} //区域映射表
23
-
24
- // 步长由1改为10是为了优化区域计算有时候出错问题
25
- // 2023年7月6日 于南京
26
- private __regionAssemble = assemble(0, 255, 10, 3)
27
-
28
- constructor(ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement, opts: CanvasOptsType = {}) {
29
- super(
30
- ViewCanvas,
31
- opts,
32
- RegionCanvas ? new PainterRender(RegionCanvas, {
33
- willReadFrequently: true,
34
- }) : null, true
35
- )
36
-
37
- this.setRegion("")
38
- }
39
-
40
- config(configs: CanvasConfigType) {
41
- for (const key in configs) {
42
- const _key = oldAttrName[key] || key
43
- this.useConfig(_key, configs[key])
44
- }
45
-
46
- return this
47
- }
48
-
49
- // 是否绘制的内容只需要进行区域记录
50
- onlyRegion(flag: boolean) {
51
- this.__onlyRegion = flag
52
- return this
53
- }
54
-
55
- // 设置当前绘制区域名称
56
- setRegion(regionName: string | number) {
57
- if (this.__region) {
58
- if (regionName) {
59
- if (this.__regionList[regionName] == void 0) {
60
- const tempColor = this.__regionAssemble()
61
- this.__regionList[regionName] =
62
- "rgb(" + tempColor[0] + "," + tempColor[1] + "," + tempColor[2] + ")"
63
- }
64
-
65
- this.__region.useConfig("fillStyle", this.__regionList[regionName]) &&
66
- this.__region.useConfig("strokeStyle", this.__regionList[regionName])
67
- } else {
68
- this.__region.useConfig("fillStyle", "#000000") &&
69
- this.__region.useConfig("strokeStyle", "#000000")
70
- }
71
- }
72
- return this
73
- }
74
-
75
- // 获取当前事件触发的区域名称
76
- getRegion(x: number, y: number): Promise<string> {
77
- return new Promise((resolve) => {
78
- const imgData = this.__region ? this.__region.painter.getImageData(x - 0.5, y - 0.5, 1, 1) : {
79
- data: [0, 0, 0, 0]
80
- }
81
-
82
- // 获取点击点的颜色
83
- let currentRGBA = imgData.data
84
-
85
- const doit = () => {
86
- if (this.__region) {
87
-
88
- // 查找当前点击的区域
89
- for (const key in this.__regionList) {
90
- if (
91
- "rgb(" +
92
- currentRGBA[0] +
93
- "," +
94
- currentRGBA[1] +
95
- "," +
96
- currentRGBA[2] +
97
- ")" ==
98
- this.__regionList[key]
99
- ) {
100
- resolve(key)
101
- break
102
- }
103
- }
104
- }
105
-
106
- resolve("")
107
- }
108
-
109
- // 如果有值
110
- if (currentRGBA) {
111
- doit()
112
- }
113
-
114
- // 否则就是在Promise中
115
- else {
116
- (imgData as any).then((data: Uint8ClampedArray) => {
117
- currentRGBA = data
118
- doit()
119
- })
120
- }
121
- })
122
- }
123
-
124
- textWidth(text: string) {
125
- this.painter.save()
126
-
127
- initText(this.painter, this.__specialConfig, 0, 0, 0)
128
-
129
- // 虽然我们限制了只可以输入字符串,可是不代表所有环境都可以保证,为了确保方法不失效,强转成字符串
130
- const width = this.painter.measureText(text + "").width
131
-
132
- this.painter.restore()
133
-
134
- return width
135
- }
136
-
137
- // 获取原始画笔
138
- getContext(isRegion = false) {
139
- return isRegion ? (this.__region ? this.__region.painter : null) : this.painter
140
- }
141
-
142
- // 获取画布信息
143
- getInfo() {
144
- return {
145
- width: this.painter.canvas.width,
146
- height: this.painter.canvas.height
147
- }
148
- }
149
-
150
- // 线性渐变
151
- createLinearGradient(x0: number, y0: number, x1: number, y1: number) {
152
- return linearGradient(this.painter, x0, y0, x1, y1)
153
- }
154
-
155
- // 环形渐变
156
- createRadialGradient(cx: number, cy: number, r: number) {
157
- return radialGradient(this.painter, cx, cy, r)
158
- }
159
-
160
- // 获取指定位置颜色
161
- getColor(x: number, y: number) {
162
- const currentRGBA = this.painter.getImageData(x - 0.5, y - 0.5, 1, 1).data
163
- return (
164
- "rgba(" +
165
- currentRGBA[0] +
166
- "," +
167
- currentRGBA[1] +
168
- "," +
169
- currentRGBA[2] +
170
- "," +
171
- currentRGBA[3] +
172
- ")"
173
- )
174
- }
175
- }
176
-
177
- export default Canvas
1
+ import type CanvasConfigType from "../../../types/CanvasConfig"
2
+ import type CanvasOptsType from '../../../types/CanvasOpts'
3
+
4
+ import PainterRender from "./painter"
5
+ import assemble from "../assemble"
6
+ import { linearGradient, radialGradient } from "./gradient"
7
+ import { initText } from './config'
8
+
9
+ // 属性名向下兼容
10
+ const oldAttrName = {
11
+ "font-size": "fontSize",
12
+ "font-family": "fontFamily",
13
+ "font-weight": "fontWeight",
14
+ "font-style": "fontStyle",
15
+ "arc-start-cap": "arcStartCap",
16
+ "arc-end-cap": "arcEndCap",
17
+ }
18
+
19
+ class Canvas extends PainterRender {
20
+ readonly name: string = "Canvas"
21
+
22
+ private __regionList = {} //区域映射表
23
+
24
+ // 步长由1改为10是为了优化区域计算有时候出错问题
25
+ // 2023年7月6日 于南京
26
+ private __regionAssemble = assemble(0, 255, 10, 3)
27
+
28
+ constructor(ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement | null, opts: CanvasOptsType = {}) {
29
+ super(
30
+ ViewCanvas,
31
+ opts,
32
+ RegionCanvas ? new PainterRender(RegionCanvas, {
33
+ willReadFrequently: true,
34
+ }) : undefined, true
35
+ )
36
+
37
+ this.setRegion("")
38
+ }
39
+
40
+ config(configs: CanvasConfigType) {
41
+ for (const key in configs) {
42
+ const _key = oldAttrName[key] || key
43
+ this.useConfig(_key, configs[key])
44
+ }
45
+
46
+ return this
47
+ }
48
+
49
+ // 是否绘制的内容只需要进行区域记录
50
+ onlyRegion(flag: boolean) {
51
+ this.__onlyRegion = flag
52
+ return this
53
+ }
54
+
55
+ // 设置当前绘制区域名称
56
+ setRegion(regionName: string | number) {
57
+ if (this.__region) {
58
+ if (regionName) {
59
+ if (this.__regionList[regionName] == void 0) {
60
+ const tempColor = this.__regionAssemble()
61
+ this.__regionList[regionName] =
62
+ "rgb(" + tempColor[0] + "," + tempColor[1] + "," + tempColor[2] + ")"
63
+ }
64
+
65
+ this.__region.useConfig("fillStyle", this.__regionList[regionName]) &&
66
+ this.__region.useConfig("strokeStyle", this.__regionList[regionName])
67
+ } else {
68
+ this.__region.useConfig("fillStyle", "#000000") &&
69
+ this.__region.useConfig("strokeStyle", "#000000")
70
+ }
71
+ }
72
+ return this
73
+ }
74
+
75
+ // 获取当前事件触发的区域名称
76
+ getRegion(x: number, y: number): Promise<string> {
77
+ return new Promise((resolve) => {
78
+ const imgData = this.__region ? this.__region.painter.getImageData(x - 0.5, y - 0.5, 1, 1) : {
79
+ data: [0, 0, 0, 0]
80
+ }
81
+
82
+ // 获取点击点的颜色
83
+ let currentRGBA = imgData.data
84
+
85
+ const doit = () => {
86
+ if (this.__region) {
87
+
88
+ // 查找当前点击的区域
89
+ for (const key in this.__regionList) {
90
+ if (
91
+ "rgb(" +
92
+ currentRGBA[0] +
93
+ "," +
94
+ currentRGBA[1] +
95
+ "," +
96
+ currentRGBA[2] +
97
+ ")" ==
98
+ this.__regionList[key]
99
+ ) {
100
+ resolve(key)
101
+ break
102
+ }
103
+ }
104
+ }
105
+
106
+ resolve("")
107
+ }
108
+
109
+ // 如果有值
110
+ if (currentRGBA) {
111
+ doit()
112
+ }
113
+
114
+ // 否则就是在Promise中
115
+ else {
116
+ (imgData as any).then((data: Uint8ClampedArray) => {
117
+ currentRGBA = data
118
+ doit()
119
+ })
120
+ }
121
+ })
122
+ }
123
+
124
+ textWidth(text: string) {
125
+ this.painter.save()
126
+
127
+ initText(this.painter, this.__specialConfig, 0, 0, 0)
128
+
129
+ // 虽然我们限制了只可以输入字符串,可是不代表所有环境都可以保证,为了确保方法不失效,强转成字符串
130
+ const width = this.painter.measureText(text + "").width
131
+
132
+ this.painter.restore()
133
+
134
+ return width
135
+ }
136
+
137
+ // 获取原始画笔
138
+ getContext(isRegion = false) {
139
+ return isRegion ? (this.__region ? this.__region.painter : null) : this.painter
140
+ }
141
+
142
+ // 获取画布信息
143
+ getInfo() {
144
+ return {
145
+ width: this.painter.canvas.width,
146
+ height: this.painter.canvas.height
147
+ }
148
+ }
149
+
150
+ // 线性渐变
151
+ createLinearGradient(x0: number, y0: number, x1: number, y1: number) {
152
+ return linearGradient(this.painter, x0, y0, x1, y1)
153
+ }
154
+
155
+ // 环形渐变
156
+ createRadialGradient(cx: number, cy: number, r: number) {
157
+ return radialGradient(this.painter, cx, cy, r)
158
+ }
159
+
160
+ // 获取指定位置颜色
161
+ getColor(x: number, y: number) {
162
+ const currentRGBA = this.painter.getImageData(x - 0.5, y - 0.5, 1, 1).data
163
+ return (
164
+ "rgba(" +
165
+ currentRGBA[0] +
166
+ "," +
167
+ currentRGBA[1] +
168
+ "," +
169
+ currentRGBA[2] +
170
+ "," +
171
+ currentRGBA[3] +
172
+ ")"
173
+ )
174
+ }
175
+ }
176
+
177
+ export default Canvas