Skip to content

Commit 9a8f0b2

Browse files
committed
新增加载界面工具,可以用这个工具对相关属性进行控制
1 parent 224bfb5 commit 9a8f0b2

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* @Date : 2020-12-25 20:36:22
3+
* @Author : MemoryShadow
4+
* @LastEditors : MemoryShadow
5+
* @LastEditTime : 2020-12-25 23:14:05
6+
* @Description : 创建一个正在加载的界面
7+
*/
8+
9+
function Loading_Control(ID) {
10+
// 节点对象
11+
this.loading_canvas = {};
12+
// 绘制对象
13+
this.ctx = {};
14+
// 最大显示进度(百分比)
15+
this.Schedule = 0;
16+
// 自由显示进度(自动维护的属性)
17+
this._A_show_Schedule = 0;
18+
this._A_show_Direction = 1;
19+
this._A_show_Angle = 0;
20+
// 任务控制ID
21+
this.startID = 0;
22+
// 记录要控制的目标ID
23+
this.canvasID = 0;
24+
// 记录自身在内部的编号
25+
this.canvasIndex = 0;
26+
// 记录点击标志
27+
this.ClickFlag = 0;
28+
// 初始化绘图对象
29+
this.Init_CTX = function (e) {
30+
this.ctx = this.loading_canvas.getContext("2d");
31+
this.ctx.lineWidth = 10;
32+
this.ctx.lineCap = "round";
33+
this.ctx.lineJoin = "round";
34+
this.ctx.strokeStyle = "#419fdf";
35+
this.ctx.translate(50, 50);
36+
this.ctx.rotate(-90 * Math.PI / 180);
37+
return this.ctx;
38+
}
39+
// 初始化
40+
this.Init = function (ID) {
41+
// 对输入进行检查,如果是对象就尝试转换
42+
// 初始化本对象
43+
switch (typeof ID) {
44+
case "string":
45+
this.loading_canvas = document.getElementById(ID);
46+
if (loading_canvas === null) {
47+
return undefined;
48+
}
49+
if (Loading_Control.indexOf(ID) !== undefined) {
50+
return Loading_Control.indexOf(ID);
51+
}
52+
break;
53+
case "object":
54+
// 依旧尝试进行处理,但是要求此属性必须要有ID的属性
55+
if (ID.id === "") {
56+
this.loading_canvas = document.getElementById(ID.id);
57+
if (Loading_Control.indexOf(ID.id) !== undefined) {
58+
return Loading_Control.indexOf(ID.id);
59+
}
60+
} else {
61+
return undefined;
62+
}
63+
break;
64+
default:
65+
return undefined;
66+
}
67+
68+
this.loading_canvas.setAttribute("width", "100px");
69+
this.loading_canvas.setAttribute("height", "100px");
70+
this.Init_CTX();
71+
// 注册本对象
72+
this.canvasID = ID;
73+
this.canvasIndex = Loading_Control.ObjList.length;
74+
Loading_Control.ObjList.push(this);
75+
return this;
76+
}
77+
// 设置最大进度
78+
this.setSchedule = function (Percentage) {
79+
this.Schedule = Percentage;
80+
}
81+
// 将百分比数值转换为开源用于显示的数值
82+
this.PercentageValue2ShowValue = function (params) {
83+
switch (params) {
84+
case 100:
85+
return 2;
86+
break;
87+
case 0:
88+
return 0;
89+
break;
90+
default:
91+
return (100 - params) / 100 * 2;
92+
break;
93+
}
94+
}
95+
// 渲染
96+
this.Refresh = function (e) {
97+
this._A_show_Schedule = parseInt(this._A_show_Schedule) + this._A_show_Direction;
98+
// 大于360°就重置
99+
this._A_show_Angle = this._A_show_Angle > 360 ? parseInt(0) : parseInt(this._A_show_Angle) + 1;
100+
// 只要新数据不低于0并且不高于this.Schedule就继续
101+
if ((this._A_show_Schedule >= 0) && (this._A_show_Schedule <= this.Schedule)) {
102+
// 如果符合,就继续渲染
103+
this.ctx.clearRect(-50, -50, 100, 100);
104+
this.ctx.beginPath();
105+
this.ctx.rotate((-90 + this._A_show_Angle) * Math.PI / 180);
106+
this.ctx.arc(0, 0, 40, 0, this.PercentageValue2ShowValue(this._A_show_Schedule) * Math.PI, true);
107+
this.ctx.stroke();
108+
} else {
109+
// 否则反向this._A_show_Direction
110+
this._A_show_Direction = -this._A_show_Direction;
111+
}
112+
}
113+
// 启动
114+
this.start = function (e) {
115+
this.ClickFlag = 1;
116+
this.startID = setInterval(function (ID) {
117+
Loading_Control.indexOf(ID).Refresh();
118+
}, 50, this.canvasIndex);
119+
}
120+
// 停止
121+
this.stop = function (e) {
122+
this.ClickFlag = -1;
123+
clearInterval(this.startID);
124+
this.startID = 0;
125+
}
126+
// 设置事件接收器
127+
this.onClick = function (value) {
128+
// 如果是false或者0, 就设数据为0,并且在移除监听器后退出
129+
if (value === false) {
130+
this.ClickFlag = 0;
131+
this.loading_canvas.onclick = null;
132+
return true;
133+
} else {
134+
// 就检测值,如果此时为负数就执行start,如果此时为正数,就执行stop
135+
// 重新设置监听器
136+
this.loading_canvas.onclick = function onclick(event) {
137+
Loading_Control.indexOf(this.id).onClick();
138+
};
139+
this.ClickFlag < 0 ? this.start() : this.stop();
140+
}
141+
}
142+
// 执行代码
143+
return this.Init(ID);
144+
}
145+
146+
Loading_Control.ObjList = [];
147+
// 获取指定的索引
148+
Loading_Control.indexOf = function (index) {
149+
switch (typeof index) {
150+
case "number":
151+
return Loading_Control.ObjList[index];
152+
case "string":
153+
// 索引指定ID的内容
154+
for (var key in Loading_Control.ObjList) {
155+
var Loading_ControlObj = Loading_Control.ObjList[key];
156+
if (Loading_ControlObj.canvasID === index) {
157+
// 如果符合就直接返回
158+
return Loading_ControlObj;
159+
}
160+
// 如果没有,就初始化内容并返回
161+
return new Loading_Control(index);
162+
}
163+
break;
164+
default:
165+
return undefined;
166+
}
167+
}

0 commit comments

Comments
 (0)