vislite 1.2.0-alpha.2 → 1.2.0-alpha.3
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 +257 -254
- package/lib/Buffer/index.es.js +1 -1
- package/lib/Buffer/index.es.min.js +1 -1
- package/lib/Canvas/index.es.js +13 -4
- package/lib/Canvas/index.es.min.js +2 -2
- package/lib/Cardinal/index.es.js +1 -1
- package/lib/Cardinal/index.es.min.js +1 -1
- package/lib/Eoap/index.es.js +1 -1
- package/lib/Eoap/index.es.min.js +1 -1
- package/lib/Hermite/index.es.js +1 -1
- package/lib/Hermite/index.es.min.js +1 -1
- package/lib/Matrix4/index.es.js +1 -1
- package/lib/Matrix4/index.es.min.js +1 -1
- package/lib/Mercator/index.es.js +1 -1
- package/lib/Mercator/index.es.min.js +1 -1
- package/lib/RawCanvas/index.es.js +12 -3
- package/lib/RawCanvas/index.es.min.js +2 -2
- package/lib/SVG/index.es.js +1 -1
- package/lib/SVG/index.es.min.js +1 -1
- package/lib/Shader/index.es.js +1 -1
- package/lib/Shader/index.es.min.js +1 -1
- package/lib/Texture/index.es.js +1 -1
- package/lib/Texture/index.es.min.js +1 -1
- package/lib/TreeLayout/index.es.js +1 -1
- package/lib/TreeLayout/index.es.min.js +1 -1
- package/lib/animation/index.es.js +1 -1
- package/lib/animation/index.es.min.js +1 -1
- package/lib/assemble/index.es.js +1 -1
- package/lib/assemble/index.es.min.js +1 -1
- package/lib/getLoopColors/index.es.js +1 -1
- package/lib/getLoopColors/index.es.min.js +1 -1
- package/lib/getWebGLContext/index.es.js +1 -1
- package/lib/getWebGLContext/index.es.min.js +1 -1
- package/lib/index.umd.js +13 -4
- package/lib/index.umd.min.js +2 -2
- package/lib/initOption/index.es.js +1 -1
- package/lib/initOption/index.es.min.js +1 -1
- package/lib/mergeOption/index.es.js +1 -1
- package/lib/mergeOption/index.es.min.js +1 -1
- package/lib/move/index.es.js +1 -1
- package/lib/move/index.es.min.js +1 -1
- package/lib/rotate/index.es.js +1 -1
- package/lib/rotate/index.es.min.js +1 -1
- package/lib/ruler/index.es.js +1 -1
- package/lib/ruler/index.es.min.js +1 -1
- package/lib/scale/index.es.js +1 -1
- package/lib/scale/index.es.min.js +1 -1
- package/lib/throttle/index.es.js +1 -1
- package/lib/throttle/index.es.min.js +1 -1
- package/miniprogram/ui-canvas/index.js +115 -115
- package/package.json +1 -1
- package/src/Canvas.ts +84 -84
- package/src/common/canvas/gradient.ts +32 -24
- package/src/common/canvas/index.ts +182 -177
- package/types/Canvas.d.ts +10 -1
- package/types/index.d.ts +160 -158
- package/types/uni-canvas.d.ts +4 -0
package/src/Canvas.ts
CHANGED
|
@@ -1,85 +1,85 @@
|
|
|
1
|
-
import type CanvasType from '../types/Canvas'
|
|
2
|
-
import type CanvasOptionType from '../types/CanvasOption'
|
|
3
|
-
|
|
4
|
-
import OralCanvas from './common/canvas/index'
|
|
5
|
-
import { initOption } from './common/option'
|
|
6
|
-
|
|
7
|
-
class Canvas extends OralCanvas implements CanvasType {
|
|
8
|
-
private __canvas: HTMLCanvasElement
|
|
9
|
-
private __el: HTMLElement
|
|
10
|
-
constructor(el: HTMLElement | null, option: CanvasOptionType = {}, width: number = 0, height: number = 0) {
|
|
11
|
-
if (!el) {
|
|
12
|
-
throw new Error("VISLite Canvas:The mount point requires an HTMLElement type but encountered null.")
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
option = initOption(option, {
|
|
16
|
-
region: true,
|
|
17
|
-
willReadFrequently: false
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
width = width || el.clientWidth
|
|
21
|
-
height = height || el.clientHeight
|
|
22
|
-
|
|
23
|
-
let ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement | null = null
|
|
24
|
-
|
|
25
|
-
const _el = el as any
|
|
26
|
-
|
|
27
|
-
// 如果已经初始化过了
|
|
28
|
-
if (_el._vislite_canvas_) {
|
|
29
|
-
ViewCanvas = _el._vislite_canvas_[0]
|
|
30
|
-
RegionCanvas = _el._vislite_canvas_[1]
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// 否则就初始化
|
|
34
|
-
else {
|
|
35
|
-
ViewCanvas = document.createElement('canvas')
|
|
36
|
-
el.appendChild(ViewCanvas)
|
|
37
|
-
|
|
38
|
-
if (option.region) {
|
|
39
|
-
RegionCanvas = document.createElement('canvas')
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
_el._vislite_canvas_ = [ViewCanvas, RegionCanvas]
|
|
43
|
-
|
|
44
|
-
el.setAttribute('vislite', 'Canvas')
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// 设置画布大小
|
|
48
|
-
const canvasArray = [RegionCanvas, ViewCanvas]
|
|
49
|
-
for (let index = 0; index < canvasArray.length; index++) {
|
|
50
|
-
const canvas = canvasArray[index]
|
|
51
|
-
if (canvas) {
|
|
52
|
-
canvas.style.width = width + "px"
|
|
53
|
-
canvas.setAttribute('width', (index * width + width) + "")
|
|
54
|
-
|
|
55
|
-
canvas.style.height = height + "px"
|
|
56
|
-
canvas.setAttribute('height', (index * height + height) + "")
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
super(ViewCanvas, RegionCanvas, {
|
|
61
|
-
willReadFrequently: option.willReadFrequently,
|
|
62
|
-
}, 2)
|
|
63
|
-
|
|
64
|
-
this.__canvas = ViewCanvas
|
|
65
|
-
this.__el = el
|
|
66
|
-
this.painter.scale(2, 2)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
toDataURL(): Promise<string> {
|
|
70
|
-
return new Promise(resolve => {
|
|
71
|
-
resolve(this.__canvas.toDataURL())
|
|
72
|
-
})
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
bind(eventName: string, callback: (regionName: string) => void) {
|
|
76
|
-
this.__el.addEventListener(eventName, (event: any) => {
|
|
77
|
-
this.getRegion(event.offsetX, event.offsetY).then(regionName => {
|
|
78
|
-
callback(regionName)
|
|
79
|
-
})
|
|
80
|
-
})
|
|
81
|
-
return this
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
1
|
+
import type CanvasType from '../types/Canvas'
|
|
2
|
+
import type CanvasOptionType from '../types/CanvasOption'
|
|
3
|
+
|
|
4
|
+
import OralCanvas from './common/canvas/index'
|
|
5
|
+
import { initOption } from './common/option'
|
|
6
|
+
|
|
7
|
+
class Canvas extends OralCanvas implements CanvasType {
|
|
8
|
+
private __canvas: HTMLCanvasElement
|
|
9
|
+
private __el: HTMLElement
|
|
10
|
+
constructor(el: HTMLElement | null, option: CanvasOptionType = {}, width: number = 0, height: number = 0) {
|
|
11
|
+
if (!el) {
|
|
12
|
+
throw new Error("VISLite Canvas:The mount point requires an HTMLElement type but encountered null.")
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
option = initOption(option, {
|
|
16
|
+
region: true,
|
|
17
|
+
willReadFrequently: false
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
width = width || el.clientWidth
|
|
21
|
+
height = height || el.clientHeight
|
|
22
|
+
|
|
23
|
+
let ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement | null = null
|
|
24
|
+
|
|
25
|
+
const _el = el as any
|
|
26
|
+
|
|
27
|
+
// 如果已经初始化过了
|
|
28
|
+
if (_el._vislite_canvas_) {
|
|
29
|
+
ViewCanvas = _el._vislite_canvas_[0]
|
|
30
|
+
RegionCanvas = _el._vislite_canvas_[1]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 否则就初始化
|
|
34
|
+
else {
|
|
35
|
+
ViewCanvas = document.createElement('canvas')
|
|
36
|
+
el.appendChild(ViewCanvas)
|
|
37
|
+
|
|
38
|
+
if (option.region) {
|
|
39
|
+
RegionCanvas = document.createElement('canvas')
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
_el._vislite_canvas_ = [ViewCanvas, RegionCanvas]
|
|
43
|
+
|
|
44
|
+
el.setAttribute('vislite', 'Canvas')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 设置画布大小
|
|
48
|
+
const canvasArray = [RegionCanvas, ViewCanvas]
|
|
49
|
+
for (let index = 0; index < canvasArray.length; index++) {
|
|
50
|
+
const canvas = canvasArray[index]
|
|
51
|
+
if (canvas) {
|
|
52
|
+
canvas.style.width = width + "px"
|
|
53
|
+
canvas.setAttribute('width', (index * width + width) + "")
|
|
54
|
+
|
|
55
|
+
canvas.style.height = height + "px"
|
|
56
|
+
canvas.setAttribute('height', (index * height + height) + "")
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
super(ViewCanvas, RegionCanvas, {
|
|
61
|
+
willReadFrequently: option.willReadFrequently,
|
|
62
|
+
}, 2)
|
|
63
|
+
|
|
64
|
+
this.__canvas = ViewCanvas
|
|
65
|
+
this.__el = el
|
|
66
|
+
this.painter.scale(2, 2)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
toDataURL(): Promise<string> {
|
|
70
|
+
return new Promise(resolve => {
|
|
71
|
+
resolve(this.__canvas.toDataURL())
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
bind(eventName: string, callback: (regionName: string, x: number, y: number) => void) {
|
|
76
|
+
this.__el.addEventListener(eventName, (event: any) => {
|
|
77
|
+
this.getRegion(event.offsetX, event.offsetY).then(regionName => {
|
|
78
|
+
callback(regionName, event.offsetX, event.offsetY)
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
return this
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
85
|
export default Canvas
|
|
@@ -1,24 +1,32 @@
|
|
|
1
|
-
let enhanceGradient = function (gradient: CanvasGradient) {
|
|
2
|
-
const enhanceGradient = {
|
|
3
|
-
"value": function () {
|
|
4
|
-
return gradient
|
|
5
|
-
},
|
|
6
|
-
"setColor": function (stop: number, color: string) {
|
|
7
|
-
gradient.addColorStop(stop, color)
|
|
8
|
-
return enhanceGradient
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
return enhanceGradient
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// 线性渐变
|
|
15
|
-
export const linearGradient = function (painter: CanvasRenderingContext2D, x0: number, y0: number, x1: number, y1: number) {
|
|
16
|
-
const gradient = painter.createLinearGradient(x0, y0, x1, y1)
|
|
17
|
-
return enhanceGradient(gradient)
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// 环形渐变
|
|
21
|
-
export const radialGradient = function (painter: CanvasRenderingContext2D, cx: number, cy: number, r: number) {
|
|
22
|
-
const gradient = painter.createRadialGradient(cx, cy, 0, cx, cy, r)
|
|
23
|
-
return enhanceGradient(gradient)
|
|
24
|
-
}
|
|
1
|
+
let enhanceGradient = function (gradient: CanvasGradient, calcStop?: Function) {
|
|
2
|
+
const enhanceGradient = {
|
|
3
|
+
"value": function () {
|
|
4
|
+
return gradient
|
|
5
|
+
},
|
|
6
|
+
"setColor": function (stop: number, color: string) {
|
|
7
|
+
gradient.addColorStop(calcStop ? calcStop(stop) : stop, color)
|
|
8
|
+
return enhanceGradient
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
return enhanceGradient
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// 线性渐变
|
|
15
|
+
export const linearGradient = function (painter: CanvasRenderingContext2D, x0: number, y0: number, x1: number, y1: number) {
|
|
16
|
+
const gradient = painter.createLinearGradient(x0, y0, x1, y1)
|
|
17
|
+
return enhanceGradient(gradient)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 环形渐变
|
|
21
|
+
export const radialGradient = function (painter: CanvasRenderingContext2D, cx: number, cy: number, r: number) {
|
|
22
|
+
const gradient = painter.createRadialGradient(cx, cy, 0, cx, cy, r)
|
|
23
|
+
return enhanceGradient(gradient)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 角度渐变
|
|
27
|
+
export const conicGradient = function (painter: CanvasRenderingContext2D, cx: number, cy: number, startDeg: number, deg?: number) {
|
|
28
|
+
const gradient = painter.createConicGradient(startDeg, cx, cy)
|
|
29
|
+
return enhanceGradient(gradient, deg ? (stop: number) => {
|
|
30
|
+
return deg / (Math.PI * 2) * stop
|
|
31
|
+
} : undefined)
|
|
32
|
+
}
|
|
@@ -1,177 +1,182 @@
|
|
|
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
|
-
class Canvas extends PainterRender {
|
|
10
|
-
readonly name: string = "Canvas"
|
|
11
|
-
|
|
12
|
-
private __regionList = {} //区域映射表
|
|
13
|
-
private __scaleSize
|
|
14
|
-
|
|
15
|
-
// 步长由1改为10是为了优化区域计算有时候出错问题
|
|
16
|
-
// 2023年7月6日 于南京
|
|
17
|
-
private __regionAssemble = assemble(0, 255, 10, 3)
|
|
18
|
-
|
|
19
|
-
// 添加scaleSize参数是为了适配画布缩放后的处理
|
|
20
|
-
// 2024年1月17日 于南京
|
|
21
|
-
constructor(ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement | null, opts: CanvasOptsType = {}, scaleSize = 1) {
|
|
22
|
-
super(
|
|
23
|
-
ViewCanvas,
|
|
24
|
-
opts,
|
|
25
|
-
RegionCanvas ? new PainterRender(RegionCanvas, {
|
|
26
|
-
willReadFrequently: true,
|
|
27
|
-
}) : undefined, true, scaleSize
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
this.__scaleSize = scaleSize
|
|
31
|
-
this.setRegion("")
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
config(configs: CanvasConfigType) {
|
|
35
|
-
for (const key in configs) {
|
|
36
|
-
this.useConfig(key, configs[key])
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return this
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// 是否绘制的内容只需要进行区域记录
|
|
43
|
-
onlyRegion(flag: boolean) {
|
|
44
|
-
this.__onlyRegion = flag
|
|
45
|
-
return this
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
onlyView(flag: boolean) {
|
|
49
|
-
this.__onlyView = flag
|
|
50
|
-
return this
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// 设置当前绘制区域名称
|
|
54
|
-
setRegion(regionName: string | number) {
|
|
55
|
-
if (this.__region) {
|
|
56
|
-
if (regionName) {
|
|
57
|
-
if (this.__regionList[regionName] == void 0) {
|
|
58
|
-
const tempColor = this.__regionAssemble()
|
|
59
|
-
this.__regionList[regionName] =
|
|
60
|
-
"rgb(" + tempColor[0] + "," + tempColor[1] + "," + tempColor[2] + ")"
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
this.__region.useConfig("fillStyle", this.__regionList[regionName]) &&
|
|
64
|
-
this.__region.useConfig("strokeStyle", this.__regionList[regionName])
|
|
65
|
-
} else {
|
|
66
|
-
this.__region.useConfig("fillStyle", "#000000") &&
|
|
67
|
-
this.__region.useConfig("strokeStyle", "#000000")
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return this
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// 获取当前事件触发的区域名称
|
|
74
|
-
getRegion(x: number, y: number): Promise<string> {
|
|
75
|
-
return new Promise((resolve) => {
|
|
76
|
-
const imgData = this.__region ? this.__region.painter.getImageData(x - 0.5, y - 0.5, 1, 1) : {
|
|
77
|
-
data: [0, 0, 0, 0]
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// 获取点击点的颜色
|
|
81
|
-
let currentRGBA = imgData.data
|
|
82
|
-
|
|
83
|
-
const doit = () => {
|
|
84
|
-
if (this.__region) {
|
|
85
|
-
|
|
86
|
-
// 查找当前点击的区域
|
|
87
|
-
for (const key in this.__regionList) {
|
|
88
|
-
if (
|
|
89
|
-
"rgb(" +
|
|
90
|
-
currentRGBA[0] +
|
|
91
|
-
"," +
|
|
92
|
-
currentRGBA[1] +
|
|
93
|
-
"," +
|
|
94
|
-
currentRGBA[2] +
|
|
95
|
-
")" ==
|
|
96
|
-
this.__regionList[key]
|
|
97
|
-
) {
|
|
98
|
-
resolve(key)
|
|
99
|
-
break
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
resolve("")
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// 如果有值
|
|
108
|
-
if (currentRGBA) {
|
|
109
|
-
doit()
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// 否则就是在Promise中
|
|
113
|
-
else {
|
|
114
|
-
(imgData as any).then((data: Uint8ClampedArray) => {
|
|
115
|
-
currentRGBA = data
|
|
116
|
-
doit()
|
|
117
|
-
})
|
|
118
|
-
}
|
|
119
|
-
})
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
textWidth(text: string) {
|
|
123
|
-
this.painter.save()
|
|
124
|
-
|
|
125
|
-
initText(this.painter, this.__specialConfig, 0, 0, 0)
|
|
126
|
-
|
|
127
|
-
// 虽然我们限制了只可以输入字符串,可是不代表所有环境都可以保证,为了确保方法不失效,强转成字符串
|
|
128
|
-
const width = this.painter.measureText(text + "").width
|
|
129
|
-
|
|
130
|
-
this.painter.restore()
|
|
131
|
-
|
|
132
|
-
return width
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// 获取原始画笔
|
|
136
|
-
getContext(isRegion = false) {
|
|
137
|
-
return isRegion ? (this.__region ? this.__region.painter : null) : this.painter
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// 获取画布信息
|
|
141
|
-
getInfo() {
|
|
142
|
-
return {
|
|
143
|
-
width: this.painter.canvas.width / this.__scaleSize,
|
|
144
|
-
height: this.painter.canvas.height / this.__scaleSize
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// 线性渐变
|
|
149
|
-
createLinearGradient(x0: number, y0: number, x1: number, y1: number) {
|
|
150
|
-
return linearGradient(this.painter, x0, y0, x1, y1)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// 环形渐变
|
|
154
|
-
createRadialGradient(cx: number, cy: number, r: number) {
|
|
155
|
-
return radialGradient(this.painter, cx, cy, r)
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
//
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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, conicGradient } from "./gradient"
|
|
7
|
+
import { initText } from './config'
|
|
8
|
+
|
|
9
|
+
class Canvas extends PainterRender {
|
|
10
|
+
readonly name: string = "Canvas"
|
|
11
|
+
|
|
12
|
+
private __regionList = {} //区域映射表
|
|
13
|
+
private __scaleSize
|
|
14
|
+
|
|
15
|
+
// 步长由1改为10是为了优化区域计算有时候出错问题
|
|
16
|
+
// 2023年7月6日 于南京
|
|
17
|
+
private __regionAssemble = assemble(0, 255, 10, 3)
|
|
18
|
+
|
|
19
|
+
// 添加scaleSize参数是为了适配画布缩放后的处理
|
|
20
|
+
// 2024年1月17日 于南京
|
|
21
|
+
constructor(ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement | null, opts: CanvasOptsType = {}, scaleSize = 1) {
|
|
22
|
+
super(
|
|
23
|
+
ViewCanvas,
|
|
24
|
+
opts,
|
|
25
|
+
RegionCanvas ? new PainterRender(RegionCanvas, {
|
|
26
|
+
willReadFrequently: true,
|
|
27
|
+
}) : undefined, true, scaleSize
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
this.__scaleSize = scaleSize
|
|
31
|
+
this.setRegion("")
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
config(configs: CanvasConfigType) {
|
|
35
|
+
for (const key in configs) {
|
|
36
|
+
this.useConfig(key, configs[key])
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return this
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 是否绘制的内容只需要进行区域记录
|
|
43
|
+
onlyRegion(flag: boolean) {
|
|
44
|
+
this.__onlyRegion = flag
|
|
45
|
+
return this
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
onlyView(flag: boolean) {
|
|
49
|
+
this.__onlyView = flag
|
|
50
|
+
return this
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 设置当前绘制区域名称
|
|
54
|
+
setRegion(regionName: string | number) {
|
|
55
|
+
if (this.__region) {
|
|
56
|
+
if (regionName) {
|
|
57
|
+
if (this.__regionList[regionName] == void 0) {
|
|
58
|
+
const tempColor = this.__regionAssemble()
|
|
59
|
+
this.__regionList[regionName] =
|
|
60
|
+
"rgb(" + tempColor[0] + "," + tempColor[1] + "," + tempColor[2] + ")"
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
this.__region.useConfig("fillStyle", this.__regionList[regionName]) &&
|
|
64
|
+
this.__region.useConfig("strokeStyle", this.__regionList[regionName])
|
|
65
|
+
} else {
|
|
66
|
+
this.__region.useConfig("fillStyle", "#000000") &&
|
|
67
|
+
this.__region.useConfig("strokeStyle", "#000000")
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return this
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 获取当前事件触发的区域名称
|
|
74
|
+
getRegion(x: number, y: number): Promise<string> {
|
|
75
|
+
return new Promise((resolve) => {
|
|
76
|
+
const imgData = this.__region ? this.__region.painter.getImageData(x - 0.5, y - 0.5, 1, 1) : {
|
|
77
|
+
data: [0, 0, 0, 0]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 获取点击点的颜色
|
|
81
|
+
let currentRGBA = imgData.data
|
|
82
|
+
|
|
83
|
+
const doit = () => {
|
|
84
|
+
if (this.__region) {
|
|
85
|
+
|
|
86
|
+
// 查找当前点击的区域
|
|
87
|
+
for (const key in this.__regionList) {
|
|
88
|
+
if (
|
|
89
|
+
"rgb(" +
|
|
90
|
+
currentRGBA[0] +
|
|
91
|
+
"," +
|
|
92
|
+
currentRGBA[1] +
|
|
93
|
+
"," +
|
|
94
|
+
currentRGBA[2] +
|
|
95
|
+
")" ==
|
|
96
|
+
this.__regionList[key]
|
|
97
|
+
) {
|
|
98
|
+
resolve(key)
|
|
99
|
+
break
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
resolve("")
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 如果有值
|
|
108
|
+
if (currentRGBA) {
|
|
109
|
+
doit()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 否则就是在Promise中
|
|
113
|
+
else {
|
|
114
|
+
(imgData as any).then((data: Uint8ClampedArray) => {
|
|
115
|
+
currentRGBA = data
|
|
116
|
+
doit()
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
textWidth(text: string) {
|
|
123
|
+
this.painter.save()
|
|
124
|
+
|
|
125
|
+
initText(this.painter, this.__specialConfig, 0, 0, 0)
|
|
126
|
+
|
|
127
|
+
// 虽然我们限制了只可以输入字符串,可是不代表所有环境都可以保证,为了确保方法不失效,强转成字符串
|
|
128
|
+
const width = this.painter.measureText(text + "").width
|
|
129
|
+
|
|
130
|
+
this.painter.restore()
|
|
131
|
+
|
|
132
|
+
return width
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// 获取原始画笔
|
|
136
|
+
getContext(isRegion = false) {
|
|
137
|
+
return isRegion ? (this.__region ? this.__region.painter : null) : this.painter
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// 获取画布信息
|
|
141
|
+
getInfo() {
|
|
142
|
+
return {
|
|
143
|
+
width: this.painter.canvas.width / this.__scaleSize,
|
|
144
|
+
height: this.painter.canvas.height / this.__scaleSize
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// 线性渐变
|
|
149
|
+
createLinearGradient(x0: number, y0: number, x1: number, y1: number) {
|
|
150
|
+
return linearGradient(this.painter, x0, y0, x1, y1)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// 环形渐变
|
|
154
|
+
createRadialGradient(cx: number, cy: number, r: number) {
|
|
155
|
+
return radialGradient(this.painter, cx, cy, r)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 角度渐变
|
|
159
|
+
createConicGradient(cx: number, cy: number, beginDeg: number, deg?: number) {
|
|
160
|
+
return conicGradient(this.painter, cx, cy, beginDeg, deg)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// 获取指定位置颜色
|
|
164
|
+
getColor(x: number, y: number) {
|
|
165
|
+
x *= this.__scaleSize
|
|
166
|
+
y *= this.__scaleSize
|
|
167
|
+
const currentRGBA = this.painter.getImageData(x - 0.5, y - 0.5, 1, 1).data
|
|
168
|
+
return (
|
|
169
|
+
"rgba(" +
|
|
170
|
+
currentRGBA[0] +
|
|
171
|
+
"," +
|
|
172
|
+
currentRGBA[1] +
|
|
173
|
+
"," +
|
|
174
|
+
currentRGBA[2] +
|
|
175
|
+
"," +
|
|
176
|
+
currentRGBA[3] +
|
|
177
|
+
")"
|
|
178
|
+
)
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export default Canvas
|
package/types/Canvas.d.ts
CHANGED
|
@@ -297,6 +297,15 @@ export default interface CanvasType {
|
|
|
297
297
|
*/
|
|
298
298
|
createRadialGradient(cx: number, cy: number, r: number): GradientType<CanvasGradient>
|
|
299
299
|
|
|
300
|
+
/**
|
|
301
|
+
* 创建角度渐变
|
|
302
|
+
* @param cx 中心
|
|
303
|
+
* @param cy
|
|
304
|
+
* @param beginDeg 开始弧度
|
|
305
|
+
* @param deg 跨越弧度,可选,默认2PI
|
|
306
|
+
*/
|
|
307
|
+
createConicGradient(cx: number, cy: number, beginDeg: number, deg?: number): GradientType<CanvasGradient>
|
|
308
|
+
|
|
300
309
|
/**
|
|
301
310
|
* 获取指定位置颜色
|
|
302
311
|
* @param x
|
|
@@ -326,5 +335,5 @@ export default interface CanvasType {
|
|
|
326
335
|
* @param eventName 事件名称
|
|
327
336
|
* @param callback 回调函数
|
|
328
337
|
*/
|
|
329
|
-
bind(eventName: string, callback: (regionName: string) => void): this
|
|
338
|
+
bind(eventName: string, callback: (regionName: string, x: number, y: number) => void): this
|
|
330
339
|
}
|