forked from yasssco/ProgressX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogressx.js
More file actions
149 lines (133 loc) · 4.53 KB
/
progressx.js
File metadata and controls
149 lines (133 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* ProgressX 1.1, 2024-04-15
* https://github.com/ryadpasha/progressx
* Copyright (c) 2018 Ryad Pasha
* Licensed under the MIT License
* Modifications and optimizations by dr|z3d, 2024 */
function initProgressX(window, document) {
"use strict";
var lastTime = 0;
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function(callback) {
var currTime = Date.now();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() {callback(currTime + timeToCall);}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function(id) {clearTimeout(id);};
}
var canvas, progressTimerId, fadeTimerId, currentProgress, showing, options = {
autoRun: true,
barThickness: 3,
barColors: {
default: {
"0.0": "rgba(220, 48, 16, .8)",
"1.0": "rgba(255, 96, 0, .8)"
},
classic: {
"0.0": "rgba(48, 64, 192, .7)",
"1.0": "rgba(48, 64, 255, .7)"
},
midnight: {
"0.0": "rgba(72, 0, 72, .8)",
"1.0": "rgba(160, 0, 160, .8)"
}
}
};
function createCanvas() {
canvas = document.createElement("canvas");
canvas.id = "pageloader";
var style = canvas.style;
style.position = "fixed";
style.top = style.left = style.right = style.margin = style.padding = 0;
style.zIndex = 100001;
style.display = "none";
document.body.appendChild(canvas);
window.addEventListener("resize", repaint);
}
function repaint() {
var ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = options.barThickness;
if (options.barColors.current) {
var lineGradient = ctx.createLinearGradient(0, 0, canvas.width, 0);
Object.keys(options.barColors.current).forEach(function(stop) {
lineGradient.addColorStop(parseFloat(stop), options.barColors.current[stop]);
});
ctx.lineWidth = options.barThickness;
ctx.beginPath();
ctx.moveTo(0, options.barThickness / 2);
ctx.lineTo(Math.ceil(currentProgress * canvas.width), options.barThickness / 2);
ctx.strokeStyle = lineGradient;
ctx.stroke();
}
}
function addEvent(elem, type, handler) {
elem.addEventListener(type, handler);
}
function progressxConfig(opts) {
for (key in opts) {
if (key === "barColors") {
var colorSet;
for (colorSet in opts[key]) {options.barColors[colorSet] = opts[key][colorSet];}
} else {
if (options.hasOwnProperty(key)) {options[key] = opts[key];}
}
}
}
function progressxShow(colorSet) {
if (showing) {return;}
showing = true;
if (fadeTimerId !== null) {window.cancelAnimationFrame(fadeTimerId);}
if (!canvas) {createCanvas();}
canvas.style.opacity = 1;
canvas.style.display = "block";
progressxProgress(0);
options.barColors.current = options.barColors[colorSet] || options.barColors.default;
if (options.autoRun) {
(function loop() {
progressTimerId = window.requestAnimationFrame(loop);
progressxProgress("+" + (0.05 * Math.pow(1 - Math.sqrt(currentProgress), 2)));
})();
}
}
function progressxProgress(to) {
if (typeof to === "string") {
to = (to.indexOf("+") >= 0 || to.indexOf("-") >= 0 ? currentProgress : 0) + parseFloat(to);
}
currentProgress = to > 1 ? 1 : to;
repaint();
return currentProgress;
}
function progressxHide() {
if (!showing) {return;}
showing = false;
if (progressTimerId != null) {
window.cancelAnimationFrame(progressTimerId);
progressTimerId = null;
}
(function loop() {
if (progressxProgress("+.1") >= 1) {
canvas.style.opacity -= 0.25;
if (canvas.style.opacity <= 0.25) {
canvas.style.display = "none";
fadeTimerId = null;
return;
}
}
fadeTimerId = window.requestAnimationFrame(loop);
})();
}
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = { config: progressxConfig, show: progressxShow, progress: progressxProgress, hide: progressxHide };
} else if (typeof define === "function" && define.amd) {
define(function() {
return { config: progressxConfig, show: progressxShow, progress: progressxProgress, hide: progressxHide };
});
} else {
window.progressx = { config: progressxConfig, show: progressxShow, progress: progressxProgress, hide: progressxHide };
}
}
initProgressX(window, document);