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.
- package/CHANGELOG +212 -209
- package/README.md +136 -144
- package/lib/Buffer/index.umd.js +36 -36
- package/lib/Buffer/index.umd.min.js +2 -2
- package/lib/Canvas/index.umd.js +691 -691
- package/lib/Canvas/index.umd.min.js +2 -2
- package/lib/Cardinal/index.umd.js +91 -91
- package/lib/Cardinal/index.umd.min.js +2 -2
- package/lib/Eoap/index.umd.js +34 -34
- package/lib/Eoap/index.umd.min.js +2 -2
- package/lib/Geometry/index.umd.js +286 -286
- package/lib/Geometry/index.umd.min.js +2 -2
- package/lib/Hermite/index.umd.js +36 -36
- package/lib/Hermite/index.umd.min.js +2 -2
- package/lib/Matrix4/index.umd.js +125 -125
- package/lib/Matrix4/index.umd.min.js +2 -2
- package/lib/Mercator/index.umd.js +20 -20
- package/lib/Mercator/index.umd.min.js +2 -2
- package/lib/OralCanvas/index.es.js +634 -634
- package/lib/OralCanvas/index.es.min.js +2 -2
- package/lib/OralWebGL/index.es.js +552 -552
- package/lib/OralWebGL/index.es.min.js +2 -2
- package/lib/SVG/index.umd.js +471 -471
- package/lib/SVG/index.umd.min.js +2 -2
- package/lib/Shader/index.umd.js +61 -61
- package/lib/Shader/index.umd.min.js +2 -2
- package/lib/Texture/index.umd.js +54 -54
- package/lib/Texture/index.umd.min.js +2 -2
- package/lib/TreeLayout/index.umd.js +368 -368
- package/lib/TreeLayout/index.umd.min.js +2 -2
- package/lib/WebGL/index.umd.js +599 -599
- package/lib/WebGL/index.umd.min.js +2 -2
- package/lib/animation/index.umd.js +73 -73
- package/lib/animation/index.umd.min.js +2 -2
- package/lib/formatColor/index.umd.js +40 -40
- package/lib/formatColor/index.umd.min.js +2 -2
- package/lib/getLoopColors/index.umd.js +36 -36
- package/lib/getLoopColors/index.umd.min.js +2 -2
- package/lib/getWebGLContext/index.umd.js +63 -63
- package/lib/getWebGLContext/index.umd.min.js +2 -2
- package/lib/index.umd.js +2955 -2955
- package/lib/index.umd.min.js +2 -2
- package/lib/move/index.umd.js +8 -8
- package/lib/move/index.umd.min.js +2 -2
- package/lib/resizeObserver/index.umd.js +30 -30
- package/lib/resizeObserver/index.umd.min.js +2 -2
- package/lib/rotate/index.umd.js +8 -8
- package/lib/rotate/index.umd.min.js +2 -2
- package/lib/ruler/index.umd.js +138 -138
- package/lib/ruler/index.umd.min.js +2 -2
- package/lib/scale/index.umd.js +7 -7
- package/lib/scale/index.umd.min.js +2 -2
- package/lib/throttle/index.umd.js +49 -49
- package/lib/throttle/index.umd.min.js +2 -2
- package/lib/viewHandler/index.umd.js +120 -120
- package/lib/viewHandler/index.umd.min.js +2 -2
- package/miniprogram/ui-canvas/drawImage.js +34 -0
- package/miniprogram/ui-canvas/index.js +10 -2
- package/package/getWebGLContext/index.ts +40 -40
- package/package.json +70 -70
- package/src/Canvas.ts +72 -72
- package/src/Geometry.ts +210 -210
- package/src/WebGL.ts +66 -66
- package/src/common/canvas/index.ts +183 -183
- package/src/common/canvas/painter.ts +446 -446
- package/src/common/geometry/prism-horizontal.ts +50 -50
- package/src/common/geometry/prism-vertical.ts +55 -55
- package/src/common/geometry/tool/circle.ts +17 -17
- package/src/common/geometry/tool/rotateLine.ts +48 -48
- package/src/common/geometry/tool/triangles.ts +23 -23
- package/src/common/webgl/getWebGLContext.ts +45 -45
- package/src/common/webgl/index.ts +227 -227
- package/types/Canvas.d.ts +318 -318
- package/types/Geometry.d.ts +132 -132
- package/types/WebGLOption.d.ts +14 -14
- package/types/getWebGLContext.d.ts +4 -4
- package/types/ui-canvas.d.ts +4 -4
- package/uni-app/drawImage.js +17 -17
- package/uni-app/ui-canvas.vue +290 -290
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 | 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, option.mode)
|
|
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, option.mode)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
67
|
export default WebGL
|
|
@@ -1,183 +1,183 @@
|
|
|
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
|
-
private __scaleSize
|
|
24
|
-
|
|
25
|
-
// 步长由1改为10是为了优化区域计算有时候出错问题
|
|
26
|
-
// 2023年7月6日 于南京
|
|
27
|
-
private __regionAssemble = assemble(0, 255, 10, 3)
|
|
28
|
-
|
|
29
|
-
// 添加scaleSize参数是为了适配画布缩放后的处理
|
|
30
|
-
// 2024年1月17日 于南京
|
|
31
|
-
constructor(ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement | null, opts: CanvasOptsType = {}, scaleSize = 1) {
|
|
32
|
-
super(
|
|
33
|
-
ViewCanvas,
|
|
34
|
-
opts,
|
|
35
|
-
RegionCanvas ? new PainterRender(RegionCanvas, {
|
|
36
|
-
willReadFrequently: true,
|
|
37
|
-
}) : undefined, true, scaleSize
|
|
38
|
-
)
|
|
39
|
-
|
|
40
|
-
this.__scaleSize = scaleSize
|
|
41
|
-
this.setRegion("")
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
config(configs: CanvasConfigType) {
|
|
45
|
-
for (const key in configs) {
|
|
46
|
-
const _key = oldAttrName[key] || key
|
|
47
|
-
this.useConfig(_key, configs[key])
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return this
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// 是否绘制的内容只需要进行区域记录
|
|
54
|
-
onlyRegion(flag: boolean) {
|
|
55
|
-
this.__onlyRegion = flag
|
|
56
|
-
return this
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// 设置当前绘制区域名称
|
|
60
|
-
setRegion(regionName: string | number) {
|
|
61
|
-
if (this.__region) {
|
|
62
|
-
if (regionName) {
|
|
63
|
-
if (this.__regionList[regionName] == void 0) {
|
|
64
|
-
const tempColor = this.__regionAssemble()
|
|
65
|
-
this.__regionList[regionName] =
|
|
66
|
-
"rgb(" + tempColor[0] + "," + tempColor[1] + "," + tempColor[2] + ")"
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
this.__region.useConfig("fillStyle", this.__regionList[regionName]) &&
|
|
70
|
-
this.__region.useConfig("strokeStyle", this.__regionList[regionName])
|
|
71
|
-
} else {
|
|
72
|
-
this.__region.useConfig("fillStyle", "#000000") &&
|
|
73
|
-
this.__region.useConfig("strokeStyle", "#000000")
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return this
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// 获取当前事件触发的区域名称
|
|
80
|
-
getRegion(x: number, y: number): Promise<string> {
|
|
81
|
-
return new Promise((resolve) => {
|
|
82
|
-
const imgData = this.__region ? this.__region.painter.getImageData(x - 0.5, y - 0.5, 1, 1) : {
|
|
83
|
-
data: [0, 0, 0, 0]
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// 获取点击点的颜色
|
|
87
|
-
let currentRGBA = imgData.data
|
|
88
|
-
|
|
89
|
-
const doit = () => {
|
|
90
|
-
if (this.__region) {
|
|
91
|
-
|
|
92
|
-
// 查找当前点击的区域
|
|
93
|
-
for (const key in this.__regionList) {
|
|
94
|
-
if (
|
|
95
|
-
"rgb(" +
|
|
96
|
-
currentRGBA[0] +
|
|
97
|
-
"," +
|
|
98
|
-
currentRGBA[1] +
|
|
99
|
-
"," +
|
|
100
|
-
currentRGBA[2] +
|
|
101
|
-
")" ==
|
|
102
|
-
this.__regionList[key]
|
|
103
|
-
) {
|
|
104
|
-
resolve(key)
|
|
105
|
-
break
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
resolve("")
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// 如果有值
|
|
114
|
-
if (currentRGBA) {
|
|
115
|
-
doit()
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// 否则就是在Promise中
|
|
119
|
-
else {
|
|
120
|
-
(imgData as any).then((data: Uint8ClampedArray) => {
|
|
121
|
-
currentRGBA = data
|
|
122
|
-
doit()
|
|
123
|
-
})
|
|
124
|
-
}
|
|
125
|
-
})
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
textWidth(text: string) {
|
|
129
|
-
this.painter.save()
|
|
130
|
-
|
|
131
|
-
initText(this.painter, this.__specialConfig, 0, 0, 0)
|
|
132
|
-
|
|
133
|
-
// 虽然我们限制了只可以输入字符串,可是不代表所有环境都可以保证,为了确保方法不失效,强转成字符串
|
|
134
|
-
const width = this.painter.measureText(text + "").width
|
|
135
|
-
|
|
136
|
-
this.painter.restore()
|
|
137
|
-
|
|
138
|
-
return width
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// 获取原始画笔
|
|
142
|
-
getContext(isRegion = false) {
|
|
143
|
-
return isRegion ? (this.__region ? this.__region.painter : null) : this.painter
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// 获取画布信息
|
|
147
|
-
getInfo() {
|
|
148
|
-
return {
|
|
149
|
-
width: this.painter.canvas.width / this.__scaleSize,
|
|
150
|
-
height: this.painter.canvas.height / this.__scaleSize
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// 线性渐变
|
|
155
|
-
createLinearGradient(x0: number, y0: number, x1: number, y1: number) {
|
|
156
|
-
return linearGradient(this.painter, x0, y0, x1, y1)
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// 环形渐变
|
|
160
|
-
createRadialGradient(cx: number, cy: number, r: number) {
|
|
161
|
-
return radialGradient(this.painter, cx, cy, r)
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// 获取指定位置颜色
|
|
165
|
-
getColor(x: number, y: number) {
|
|
166
|
-
x *= this.__scaleSize
|
|
167
|
-
y *= this.__scaleSize
|
|
168
|
-
const currentRGBA = this.painter.getImageData(x - 0.5, y - 0.5, 1, 1).data
|
|
169
|
-
return (
|
|
170
|
-
"rgba(" +
|
|
171
|
-
currentRGBA[0] +
|
|
172
|
-
"," +
|
|
173
|
-
currentRGBA[1] +
|
|
174
|
-
"," +
|
|
175
|
-
currentRGBA[2] +
|
|
176
|
-
"," +
|
|
177
|
-
currentRGBA[3] +
|
|
178
|
-
")"
|
|
179
|
-
)
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
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
|
+
private __scaleSize
|
|
24
|
+
|
|
25
|
+
// 步长由1改为10是为了优化区域计算有时候出错问题
|
|
26
|
+
// 2023年7月6日 于南京
|
|
27
|
+
private __regionAssemble = assemble(0, 255, 10, 3)
|
|
28
|
+
|
|
29
|
+
// 添加scaleSize参数是为了适配画布缩放后的处理
|
|
30
|
+
// 2024年1月17日 于南京
|
|
31
|
+
constructor(ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement | null, opts: CanvasOptsType = {}, scaleSize = 1) {
|
|
32
|
+
super(
|
|
33
|
+
ViewCanvas,
|
|
34
|
+
opts,
|
|
35
|
+
RegionCanvas ? new PainterRender(RegionCanvas, {
|
|
36
|
+
willReadFrequently: true,
|
|
37
|
+
}) : undefined, true, scaleSize
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
this.__scaleSize = scaleSize
|
|
41
|
+
this.setRegion("")
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
config(configs: CanvasConfigType) {
|
|
45
|
+
for (const key in configs) {
|
|
46
|
+
const _key = oldAttrName[key] || key
|
|
47
|
+
this.useConfig(_key, configs[key])
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return this
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 是否绘制的内容只需要进行区域记录
|
|
54
|
+
onlyRegion(flag: boolean) {
|
|
55
|
+
this.__onlyRegion = flag
|
|
56
|
+
return this
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 设置当前绘制区域名称
|
|
60
|
+
setRegion(regionName: string | number) {
|
|
61
|
+
if (this.__region) {
|
|
62
|
+
if (regionName) {
|
|
63
|
+
if (this.__regionList[regionName] == void 0) {
|
|
64
|
+
const tempColor = this.__regionAssemble()
|
|
65
|
+
this.__regionList[regionName] =
|
|
66
|
+
"rgb(" + tempColor[0] + "," + tempColor[1] + "," + tempColor[2] + ")"
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
this.__region.useConfig("fillStyle", this.__regionList[regionName]) &&
|
|
70
|
+
this.__region.useConfig("strokeStyle", this.__regionList[regionName])
|
|
71
|
+
} else {
|
|
72
|
+
this.__region.useConfig("fillStyle", "#000000") &&
|
|
73
|
+
this.__region.useConfig("strokeStyle", "#000000")
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return this
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 获取当前事件触发的区域名称
|
|
80
|
+
getRegion(x: number, y: number): Promise<string> {
|
|
81
|
+
return new Promise((resolve) => {
|
|
82
|
+
const imgData = this.__region ? this.__region.painter.getImageData(x - 0.5, y - 0.5, 1, 1) : {
|
|
83
|
+
data: [0, 0, 0, 0]
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 获取点击点的颜色
|
|
87
|
+
let currentRGBA = imgData.data
|
|
88
|
+
|
|
89
|
+
const doit = () => {
|
|
90
|
+
if (this.__region) {
|
|
91
|
+
|
|
92
|
+
// 查找当前点击的区域
|
|
93
|
+
for (const key in this.__regionList) {
|
|
94
|
+
if (
|
|
95
|
+
"rgb(" +
|
|
96
|
+
currentRGBA[0] +
|
|
97
|
+
"," +
|
|
98
|
+
currentRGBA[1] +
|
|
99
|
+
"," +
|
|
100
|
+
currentRGBA[2] +
|
|
101
|
+
")" ==
|
|
102
|
+
this.__regionList[key]
|
|
103
|
+
) {
|
|
104
|
+
resolve(key)
|
|
105
|
+
break
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
resolve("")
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 如果有值
|
|
114
|
+
if (currentRGBA) {
|
|
115
|
+
doit()
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// 否则就是在Promise中
|
|
119
|
+
else {
|
|
120
|
+
(imgData as any).then((data: Uint8ClampedArray) => {
|
|
121
|
+
currentRGBA = data
|
|
122
|
+
doit()
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
textWidth(text: string) {
|
|
129
|
+
this.painter.save()
|
|
130
|
+
|
|
131
|
+
initText(this.painter, this.__specialConfig, 0, 0, 0)
|
|
132
|
+
|
|
133
|
+
// 虽然我们限制了只可以输入字符串,可是不代表所有环境都可以保证,为了确保方法不失效,强转成字符串
|
|
134
|
+
const width = this.painter.measureText(text + "").width
|
|
135
|
+
|
|
136
|
+
this.painter.restore()
|
|
137
|
+
|
|
138
|
+
return width
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 获取原始画笔
|
|
142
|
+
getContext(isRegion = false) {
|
|
143
|
+
return isRegion ? (this.__region ? this.__region.painter : null) : this.painter
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 获取画布信息
|
|
147
|
+
getInfo() {
|
|
148
|
+
return {
|
|
149
|
+
width: this.painter.canvas.width / this.__scaleSize,
|
|
150
|
+
height: this.painter.canvas.height / this.__scaleSize
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// 线性渐变
|
|
155
|
+
createLinearGradient(x0: number, y0: number, x1: number, y1: number) {
|
|
156
|
+
return linearGradient(this.painter, x0, y0, x1, y1)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// 环形渐变
|
|
160
|
+
createRadialGradient(cx: number, cy: number, r: number) {
|
|
161
|
+
return radialGradient(this.painter, cx, cy, r)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// 获取指定位置颜色
|
|
165
|
+
getColor(x: number, y: number) {
|
|
166
|
+
x *= this.__scaleSize
|
|
167
|
+
y *= this.__scaleSize
|
|
168
|
+
const currentRGBA = this.painter.getImageData(x - 0.5, y - 0.5, 1, 1).data
|
|
169
|
+
return (
|
|
170
|
+
"rgba(" +
|
|
171
|
+
currentRGBA[0] +
|
|
172
|
+
"," +
|
|
173
|
+
currentRGBA[1] +
|
|
174
|
+
"," +
|
|
175
|
+
currentRGBA[2] +
|
|
176
|
+
"," +
|
|
177
|
+
currentRGBA[3] +
|
|
178
|
+
")"
|
|
179
|
+
)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export default Canvas
|