@whitesev/utils 1.0.4 → 1.0.6
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/dist/index.amd.js +5885 -1817
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +5533 -1467
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +5531 -1467
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +5886 -1817
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +5897 -1826
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +5889 -1817
- package/dist/index.umd.js.map +1 -1
- package/dist/src/Dictionary.d.ts +82 -0
- package/dist/src/Hooks.d.ts +11 -0
- package/dist/src/Httpx.d.ts +1201 -0
- package/dist/src/LockFunction.d.ts +31 -0
- package/dist/src/Log.d.ts +96 -0
- package/dist/src/Progress.d.ts +37 -0
- package/dist/src/UtilsGMMenu.d.ts +156 -0
- package/dist/src/index.d.ts +20 -27
- package/dist/src/indexedDB.d.ts +73 -0
- package/dist/src/tryCatch.d.ts +31 -0
- package/package.json +36 -37
- package/rollup.config.js +0 -3
- package/src/Dictionary.ts +152 -0
- package/src/{Hooks/Hooks.js → Hooks.ts} +31 -17
- package/src/{Httpx/index.d.ts → Httpx.ts} +837 -46
- package/src/LockFunction.ts +62 -0
- package/src/Log.ts +281 -0
- package/src/Progress.ts +143 -0
- package/src/UtilsGMMenu.ts +681 -0
- package/src/index.ts +17 -29
- package/src/indexedDB.ts +421 -0
- package/src/tryCatch.ts +107 -0
- package/tsconfig.json +1 -1
- package/dist/src/Dictionary/Dictionary.d.ts +0 -85
- package/dist/src/Hooks/Hooks.d.ts +0 -5
- package/dist/src/Httpx/Httpx.d.ts +0 -50
- package/dist/src/LockFunction/LockFunction.d.ts +0 -16
- package/dist/src/Log/Log.d.ts +0 -66
- package/dist/src/Progress/Progress.d.ts +0 -6
- package/dist/src/UtilsGMMenu/UtilsGMMenu.d.ts +0 -119
- package/dist/src/indexedDB/indexedDB.d.ts +0 -165
- package/dist/src/tryCatch/tryCatch.d.ts +0 -31
- package/src/Dictionary/Dictionary.js +0 -157
- package/src/Dictionary/index.d.ts +0 -52
- package/src/Hooks/index.d.ts +0 -16
- package/src/Httpx/Httpx.js +0 -747
- package/src/LockFunction/LockFunction.js +0 -35
- package/src/LockFunction/index.d.ts +0 -47
- package/src/Log/Log.js +0 -256
- package/src/Log/index.d.ts +0 -91
- package/src/Progress/Progress.js +0 -98
- package/src/Progress/index.d.ts +0 -30
- package/src/UtilsGMMenu/UtilsGMMenu.js +0 -464
- package/src/UtilsGMMenu/index.d.ts +0 -224
- package/src/indexedDB/index.d.ts +0 -128
- package/src/indexedDB/indexedDB.js +0 -355
- package/src/tryCatch/index.d.ts +0 -6
- package/src/tryCatch/tryCatch.js +0 -100
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Utils } from ".";
|
|
2
|
+
|
|
3
|
+
class LockFunction<K extends (...args: any[]) => any | Promise<any> | void> {
|
|
4
|
+
#flag: boolean = false;
|
|
5
|
+
#delayTime: number = 0;
|
|
6
|
+
#callback: K;
|
|
7
|
+
#context: typeof Utils;
|
|
8
|
+
/**
|
|
9
|
+
* @param callback 需要执行的函数
|
|
10
|
+
* @param delayTime (可选)延迟xx毫秒后解锁,默认:0
|
|
11
|
+
*/
|
|
12
|
+
constructor(callback: K, delayTime?: number);
|
|
13
|
+
/**
|
|
14
|
+
* @param callback 需要执行的函数
|
|
15
|
+
* @param context (可选)函数作用域,默认:this(Utils)
|
|
16
|
+
* @param delayTime (可选)延迟xx毫秒后解锁,默认:0
|
|
17
|
+
*/
|
|
18
|
+
constructor(callback: K, context?: any, delayTime?: number);
|
|
19
|
+
constructor(callback: K, context?: any, delayTime?: number) {
|
|
20
|
+
this.#callback = callback;
|
|
21
|
+
if (typeof context === "number") {
|
|
22
|
+
this.#delayTime = context;
|
|
23
|
+
this.#context = Utils;
|
|
24
|
+
} else {
|
|
25
|
+
this.#delayTime = delayTime as number;
|
|
26
|
+
this.#context = context;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 判断是否被锁
|
|
31
|
+
*/
|
|
32
|
+
isLock() {
|
|
33
|
+
return this.#flag;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 锁
|
|
37
|
+
*/
|
|
38
|
+
lock() {
|
|
39
|
+
this.#flag = true;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* 解锁
|
|
43
|
+
*/
|
|
44
|
+
unlock() {
|
|
45
|
+
setTimeout(() => {
|
|
46
|
+
this.#flag = false;
|
|
47
|
+
}, this.#delayTime);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 执行
|
|
51
|
+
*/
|
|
52
|
+
async run(...args: any[]) {
|
|
53
|
+
if (this.isLock()) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
this.lock();
|
|
57
|
+
await this.#callback.apply(this.#context, args);
|
|
58
|
+
this.unlock();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { LockFunction };
|
package/src/Log.ts
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import type { AnyObject } from ".";
|
|
2
|
+
|
|
3
|
+
/** Utils.Log的初始化配置 */
|
|
4
|
+
declare interface UtilsLogOptions {
|
|
5
|
+
/** 是否输出Tag,false的话其它的颜色也不输出,默认为true */
|
|
6
|
+
tag: boolean;
|
|
7
|
+
/** log.success的颜色,默认#0000FF */
|
|
8
|
+
successColor: string;
|
|
9
|
+
/** log.warn的颜色,默认0 */
|
|
10
|
+
warnColor: string;
|
|
11
|
+
/** log.error的颜色,默认#FF0000 */
|
|
12
|
+
errorColor: string;
|
|
13
|
+
/** log.info的颜色,默认0 */
|
|
14
|
+
infoColor: string;
|
|
15
|
+
/** 是否开启debug模式,true会在控制台每次调用时输出调用函数的所在位置,false不会输出位置,默认false */
|
|
16
|
+
debug: boolean;
|
|
17
|
+
/** 当console输出超过logMaxCount数量自动清理控制台,默认false */
|
|
18
|
+
autoClearConsole: boolean;
|
|
19
|
+
/** console输出的最高数量,autoClearConsole开启则生效,默认999 */
|
|
20
|
+
logMaxCount: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class Log {
|
|
24
|
+
/** 前面的TAG标志 */
|
|
25
|
+
#disable: boolean = false;
|
|
26
|
+
tag: string = "";
|
|
27
|
+
#console: Console = null as any;
|
|
28
|
+
#logCount = 0;
|
|
29
|
+
#details: UtilsLogOptions = {
|
|
30
|
+
tag: true,
|
|
31
|
+
successColor: "#0000FF",
|
|
32
|
+
errorColor: "#FF0000",
|
|
33
|
+
infoColor: "0",
|
|
34
|
+
warnColor: "0",
|
|
35
|
+
debug: false,
|
|
36
|
+
autoClearConsole: false,
|
|
37
|
+
logMaxCount: 999,
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* 待恢复的函数或对象
|
|
41
|
+
*/
|
|
42
|
+
#recoveryList = [];
|
|
43
|
+
#msgColorDetails = [
|
|
44
|
+
"font-weight: bold; color: cornflowerblue",
|
|
45
|
+
"font-weight: bold; color: cornflowerblue",
|
|
46
|
+
"font-weight: bold; color: darkorange",
|
|
47
|
+
"font-weight: bold; color: cornflowerblue",
|
|
48
|
+
];
|
|
49
|
+
/**
|
|
50
|
+
* @param _GM_info_ 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}}
|
|
51
|
+
* @param console 可指定console对象为unsafeWindow下的console或者是油猴window下的console
|
|
52
|
+
*/
|
|
53
|
+
constructor(
|
|
54
|
+
_GM_info_: {
|
|
55
|
+
script: {
|
|
56
|
+
name: string;
|
|
57
|
+
};
|
|
58
|
+
} = {
|
|
59
|
+
script: {
|
|
60
|
+
name: "Utils.Log",
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
console: Console = global.console
|
|
64
|
+
) {
|
|
65
|
+
this.tag = _GM_info_.script.name;
|
|
66
|
+
this.#console = console;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 解析Error的堆栈获取实际调用者的函数名及函数所在的位置
|
|
71
|
+
* @param stack
|
|
72
|
+
*/
|
|
73
|
+
private parseErrorStack(stack: string[]) {
|
|
74
|
+
let result = {
|
|
75
|
+
name: "",
|
|
76
|
+
position: "",
|
|
77
|
+
};
|
|
78
|
+
for (let stackString of stack) {
|
|
79
|
+
stackString = stackString.trim();
|
|
80
|
+
let stackFunctionNameMatch = stackString.match(/^at[\s]+(.+?)[\s]+/i);
|
|
81
|
+
let stackFunctionNamePositionMatch = stackString.match(
|
|
82
|
+
/^at[\s]+.+[\s]+\((.+?)\)/i
|
|
83
|
+
);
|
|
84
|
+
if (stackFunctionNameMatch == null) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (stackFunctionNamePositionMatch == null) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
let stackFunctionName =
|
|
91
|
+
stackFunctionNameMatch[stackFunctionNameMatch.length - 1];
|
|
92
|
+
let stackFunctionNamePosition =
|
|
93
|
+
stackFunctionNamePositionMatch[
|
|
94
|
+
stackFunctionNamePositionMatch.length - 1
|
|
95
|
+
];
|
|
96
|
+
if (
|
|
97
|
+
stackFunctionName === "" ||
|
|
98
|
+
stackFunctionName.match(
|
|
99
|
+
new RegExp(
|
|
100
|
+
"(^Utils.Log.|.<anonymous>$|^Function.each|^NodeList.forEach|^k.fn.init.each)",
|
|
101
|
+
"g"
|
|
102
|
+
)
|
|
103
|
+
)
|
|
104
|
+
) {
|
|
105
|
+
continue;
|
|
106
|
+
} else {
|
|
107
|
+
result.name = stackFunctionName;
|
|
108
|
+
result.position = stackFunctionNamePosition;
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (result.position === "") {
|
|
113
|
+
let lastStackString = stack[stack.length - 1].trim();
|
|
114
|
+
if (lastStackString.startsWith("at chrome-extension://")) {
|
|
115
|
+
let lastStackMatch = lastStackString.match(/^at[\s]+(.+)/);
|
|
116
|
+
if (lastStackMatch) {
|
|
117
|
+
result.position = lastStackMatch[lastStackMatch.length - 1];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (result.position === "") {
|
|
122
|
+
result.position = stack[stack.length - 1].trim().replace(/^at[\s]*/g, "");
|
|
123
|
+
}
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* 检测清理控制台
|
|
128
|
+
*/
|
|
129
|
+
private checkClearConsole() {
|
|
130
|
+
this.#logCount++;
|
|
131
|
+
if (
|
|
132
|
+
this.#details.autoClearConsole &&
|
|
133
|
+
this.#logCount > this.#details.logMaxCount
|
|
134
|
+
) {
|
|
135
|
+
this.#console.clear();
|
|
136
|
+
this.#logCount = 0;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 输出内容
|
|
142
|
+
* @param msg 需要输出的内容
|
|
143
|
+
* @param color 颜色
|
|
144
|
+
* @param otherStyle 其它CSS
|
|
145
|
+
*/
|
|
146
|
+
private printContent(msg: any, color: string, otherStyle?: string) {
|
|
147
|
+
this.checkClearConsole.apply(this);
|
|
148
|
+
otherStyle = otherStyle || "";
|
|
149
|
+
let stackSplit = new Error()!.stack!.split("\n");
|
|
150
|
+
stackSplit.splice(0, 2);
|
|
151
|
+
let { name: callerName, position: callerPosition } =
|
|
152
|
+
this.parseErrorStack(stackSplit);
|
|
153
|
+
let tagName = this.tag;
|
|
154
|
+
let that = this;
|
|
155
|
+
function consoleMsg(_msg_: any) {
|
|
156
|
+
if (typeof _msg_ === "string") {
|
|
157
|
+
that.#console.log(
|
|
158
|
+
`%c[${tagName}%c-%c${callerName}%c]%c %s`,
|
|
159
|
+
...that.#msgColorDetails,
|
|
160
|
+
`color: ${color};${otherStyle}`,
|
|
161
|
+
_msg_
|
|
162
|
+
);
|
|
163
|
+
} else if (typeof _msg_ === "number") {
|
|
164
|
+
that.#console.log(
|
|
165
|
+
`%c[${tagName}%c-%c${callerName}%c]%c %d`,
|
|
166
|
+
...that.#msgColorDetails,
|
|
167
|
+
`color: ${color};${otherStyle}`,
|
|
168
|
+
_msg_
|
|
169
|
+
);
|
|
170
|
+
} else if (typeof _msg_ === "object") {
|
|
171
|
+
that.#console.log(
|
|
172
|
+
`%c[${tagName}%c-%c${callerName}%c]%c %o`,
|
|
173
|
+
...that.#msgColorDetails,
|
|
174
|
+
`color: ${color};${otherStyle}`,
|
|
175
|
+
_msg_
|
|
176
|
+
);
|
|
177
|
+
} else {
|
|
178
|
+
that.#console.log(_msg_);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (Array.isArray(msg)) {
|
|
182
|
+
msg.forEach((item) => {
|
|
183
|
+
consoleMsg(item);
|
|
184
|
+
});
|
|
185
|
+
} else {
|
|
186
|
+
consoleMsg(msg);
|
|
187
|
+
}
|
|
188
|
+
if (this.#details.debug) {
|
|
189
|
+
/* 如果开启调试模式,输出堆栈位置 */
|
|
190
|
+
this.#console.log(callerPosition);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* 控制台-普通输出
|
|
195
|
+
* @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
|
|
196
|
+
* @param color 输出的颜色
|
|
197
|
+
* @param otherStyle 其它CSS
|
|
198
|
+
*/
|
|
199
|
+
info(msg: any, color: string = this.#details.infoColor, otherStyle?: string) {
|
|
200
|
+
if (this.#disable) return;
|
|
201
|
+
this.printContent.call(this, msg, color, otherStyle);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* 控制台-警告输出
|
|
205
|
+
* @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
|
|
206
|
+
* @param color 输出的颜色
|
|
207
|
+
* @param otherStyle 其它CSS
|
|
208
|
+
*/
|
|
209
|
+
warn(
|
|
210
|
+
msg: any,
|
|
211
|
+
color = this.#details.warnColor,
|
|
212
|
+
otherStyle = "background: #FEF6D5;padding: 4px 6px 4px 0px;"
|
|
213
|
+
) {
|
|
214
|
+
if (this.#disable) return;
|
|
215
|
+
this.printContent.call(this, msg, color, otherStyle);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* 控制台-错误输出
|
|
219
|
+
* @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
|
|
220
|
+
* @param color 输出的颜色
|
|
221
|
+
* @param otherStyle 其它CSS
|
|
222
|
+
*/
|
|
223
|
+
error(msg: any, color = this.#details.errorColor, otherStyle?: string) {
|
|
224
|
+
if (this.#disable) return;
|
|
225
|
+
this.printContent.call(this, msg, color, otherStyle);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* 控制台-成功输出
|
|
229
|
+
* @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
|
|
230
|
+
* @param color 输出的颜色
|
|
231
|
+
* @param otherStyle 其它CSS
|
|
232
|
+
*/
|
|
233
|
+
success(msg: any, color = this.#details.successColor, otherStyle?: string) {
|
|
234
|
+
if (this.#disable) return;
|
|
235
|
+
this.printContent.call(this, msg, color, otherStyle);
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* 控制台-输出表格
|
|
239
|
+
* @param msg
|
|
240
|
+
* @param color 输出的颜色
|
|
241
|
+
* @param otherStyle 其它CSS
|
|
242
|
+
* @example
|
|
243
|
+
* log.table([{"名字":"example","值":"123"},{"名字":"example2","值":"345"}])
|
|
244
|
+
*/
|
|
245
|
+
table(msg: AnyObject[], color = this.#details.infoColor, otherStyle = "") {
|
|
246
|
+
if (this.#disable) return;
|
|
247
|
+
this.checkClearConsole.apply(this);
|
|
248
|
+
let stack = new Error()!.stack!.split("\n");
|
|
249
|
+
stack.splice(0, 1);
|
|
250
|
+
let errorStackParse = this.parseErrorStack(stack);
|
|
251
|
+
let stackFunctionName = errorStackParse.name;
|
|
252
|
+
let stackFunctionNamePosition = errorStackParse.position;
|
|
253
|
+
let callerName = stackFunctionName;
|
|
254
|
+
this.#console.log(
|
|
255
|
+
`%c[${this.tag}%c-%c${callerName}%c]%c`,
|
|
256
|
+
...this.#msgColorDetails,
|
|
257
|
+
`color: ${color};${otherStyle}`
|
|
258
|
+
);
|
|
259
|
+
this.#console.table(msg);
|
|
260
|
+
if (this.#details.debug) {
|
|
261
|
+
this.#console.log(stackFunctionNamePosition);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* 配置Log对象的颜色
|
|
266
|
+
* @param paramDetails 配置信息
|
|
267
|
+
*/
|
|
268
|
+
config(paramDetails: Partial<UtilsLogOptions>) {
|
|
269
|
+
this.#details = Object.assign(this.#details, paramDetails);
|
|
270
|
+
}
|
|
271
|
+
/** 禁用输出 */
|
|
272
|
+
disable() {
|
|
273
|
+
this.#disable = true;
|
|
274
|
+
}
|
|
275
|
+
/** 恢复输出 */
|
|
276
|
+
recovery() {
|
|
277
|
+
this.#disable = false;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export { Log };
|
package/src/Progress.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Utils } from ".";
|
|
2
|
+
|
|
3
|
+
declare interface ProgressParamConfig {
|
|
4
|
+
/** canvas元素节点 */
|
|
5
|
+
canvasNode: HTMLCanvasElement;
|
|
6
|
+
/** 绘制角度,默认:95 */
|
|
7
|
+
deg: number;
|
|
8
|
+
/** 进度,默认:0 */
|
|
9
|
+
progress: number;
|
|
10
|
+
/** 绘制的线宽度,默认:10 */
|
|
11
|
+
lineWidth: number;
|
|
12
|
+
/** 绘制的背景颜色,默认:#1e637c */
|
|
13
|
+
lineBgColor: string;
|
|
14
|
+
/** 绘制的线的颜色,默认:#25deff */
|
|
15
|
+
lineColor: string;
|
|
16
|
+
/** 绘制的字体颜色,默认:#000000 */
|
|
17
|
+
textColor: string;
|
|
18
|
+
/** 绘制的字体大小(px),默认:22px */
|
|
19
|
+
fontSize: number;
|
|
20
|
+
/** 绘制的圆的半径,默认:50 */
|
|
21
|
+
circleRadius: number;
|
|
22
|
+
}
|
|
23
|
+
class Progress {
|
|
24
|
+
#config: ProgressParamConfig = {
|
|
25
|
+
/**
|
|
26
|
+
* canvas元素节点
|
|
27
|
+
*/
|
|
28
|
+
canvasNode: null as any,
|
|
29
|
+
/**
|
|
30
|
+
* 绘制角度
|
|
31
|
+
*/
|
|
32
|
+
deg: 95,
|
|
33
|
+
/**
|
|
34
|
+
* 进度
|
|
35
|
+
*/
|
|
36
|
+
progress: 0,
|
|
37
|
+
/**
|
|
38
|
+
* 绘制的线宽度
|
|
39
|
+
*/
|
|
40
|
+
lineWidth: 10,
|
|
41
|
+
/**
|
|
42
|
+
* 绘制的背景颜色
|
|
43
|
+
*/
|
|
44
|
+
lineBgColor: "#1e637c",
|
|
45
|
+
/**
|
|
46
|
+
* 绘制的线的颜色
|
|
47
|
+
*/
|
|
48
|
+
lineColor: "#25deff",
|
|
49
|
+
/**
|
|
50
|
+
* 绘制的字体颜色
|
|
51
|
+
*/
|
|
52
|
+
textColor: "#000000",
|
|
53
|
+
/**
|
|
54
|
+
* 绘制的字体大小(px)
|
|
55
|
+
*/
|
|
56
|
+
fontSize: 22,
|
|
57
|
+
/**
|
|
58
|
+
* 绘制的圆的半径
|
|
59
|
+
*/
|
|
60
|
+
circleRadius: 50,
|
|
61
|
+
};
|
|
62
|
+
#ctx: CanvasRenderingContext2D = null as any;
|
|
63
|
+
#width: number = null as any;
|
|
64
|
+
#height: number = null as any;
|
|
65
|
+
/**
|
|
66
|
+
*
|
|
67
|
+
* @param paramConfig 配置信息
|
|
68
|
+
*/
|
|
69
|
+
constructor(paramConfig: ProgressParamConfig) {
|
|
70
|
+
this.#config = Utils.assign(this.#config, paramConfig);
|
|
71
|
+
if (!(this.#config.canvasNode instanceof HTMLCanvasElement)) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
"Utils.Progress 参数 canvasNode 必须是 HTMLCanvasElement"
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
this.init();
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 初始化
|
|
80
|
+
*/
|
|
81
|
+
private init() {
|
|
82
|
+
/* 获取画笔 */
|
|
83
|
+
let ctx = this.#config.canvasNode.getContext("2d");
|
|
84
|
+
if (ctx == null) {
|
|
85
|
+
throw new Error("Utils.Progress 获取画笔失败");
|
|
86
|
+
}
|
|
87
|
+
this.#ctx = ctx;
|
|
88
|
+
/* 元素宽度 */
|
|
89
|
+
this.#width = this.#config.canvasNode.width;
|
|
90
|
+
/* 元素高度 */
|
|
91
|
+
this.#height = this.#config.canvasNode.height;
|
|
92
|
+
/* 清除锯齿 */
|
|
93
|
+
if (window.devicePixelRatio) {
|
|
94
|
+
this.#config.canvasNode.style.width = this.#width + "px";
|
|
95
|
+
this.#config.canvasNode.style.height = this.#height + "px";
|
|
96
|
+
this.#config.canvasNode.height = this.#height * window.devicePixelRatio;
|
|
97
|
+
this.#config.canvasNode.width = this.#width * window.devicePixelRatio;
|
|
98
|
+
this.#ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
|
|
99
|
+
}
|
|
100
|
+
/* 设置线宽 */
|
|
101
|
+
this.#ctx.lineWidth = this.#config.lineWidth;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* 绘制
|
|
105
|
+
*/
|
|
106
|
+
draw() {
|
|
107
|
+
let degActive = (this.#config.progress * 360) / 100;
|
|
108
|
+
/* 清除画布 */
|
|
109
|
+
this.#ctx.clearRect(0, 0, this.#width, this.#height);
|
|
110
|
+
/* 开始绘制底圆 */
|
|
111
|
+
this.#ctx.beginPath();
|
|
112
|
+
this.#ctx.arc(
|
|
113
|
+
this.#width / 2,
|
|
114
|
+
this.#height / 2,
|
|
115
|
+
this.#config.circleRadius,
|
|
116
|
+
1,
|
|
117
|
+
8
|
|
118
|
+
);
|
|
119
|
+
this.#ctx.strokeStyle = this.#config.lineBgColor;
|
|
120
|
+
this.#ctx.stroke();
|
|
121
|
+
/* 开始绘制动态圆 */
|
|
122
|
+
this.#ctx.beginPath();
|
|
123
|
+
this.#ctx.arc(
|
|
124
|
+
this.#width / 2,
|
|
125
|
+
this.#height / 2,
|
|
126
|
+
this.#config.circleRadius,
|
|
127
|
+
-Math.PI / 2,
|
|
128
|
+
(degActive * Math.PI) / 180 - Math.PI / 2
|
|
129
|
+
);
|
|
130
|
+
this.#ctx.strokeStyle = this.#config.lineColor;
|
|
131
|
+
this.#ctx.stroke();
|
|
132
|
+
/* 获取百分比 */
|
|
133
|
+
let txt = parseInt(this.#config.progress.toString()) + "%";
|
|
134
|
+
this.#ctx.font = this.#config.fontSize + "px SimHei";
|
|
135
|
+
/* 获取文本宽度 */
|
|
136
|
+
let w = this.#ctx.measureText(txt).width;
|
|
137
|
+
let h = this.#config.fontSize / 2;
|
|
138
|
+
this.#ctx.fillStyle = this.#config.textColor;
|
|
139
|
+
this.#ctx.fillText(txt, this.#width / 2 - w / 2, this.#height / 2 + h / 2);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export { Progress };
|