forked from rpsthecoder/favicon-canvas-loader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
34 lines (33 loc) · 1.08 KB
/
script.js
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
onload = function() {
cv = document.querySelector('#cvl'),
ctx = cv.getContext('2d');
if (!!ctx) {
s = 1.5 * Math.PI, // starting position[angle] for circle drawing
tc = pct = 0,
btn = document.querySelector('#lbtn'),
lnk = document.querySelector('link[rel*="icon"]');
ctx.lineWidth = 2;
ctx.strokeStyle = 'fuchsia';
if(btn.disabled) btn.removeAttribute('disabled'); // enable btn on page refresh
btn.addEventListener('click', function() {
tc = setInterval(updateLoader, 60);
this.textContent = 'Loading';
this.style.backgroundColor = '#999';
this.setAttribute('disabled','');
});
}
};
function updateLoader() {
ctx.clearRect(0, 0, 16, 16);
ctx.beginPath();
ctx.arc(8, 8, 6, s, (pct * 2 * Math.PI / 100 + s));
ctx.stroke();
lnk.href= cv.toDataURL('image/png'); // update favicon
if (pct === 100) {
clearInterval(tc);
btn.textContent = 'Loaded';
btn.style.backgroundColor = 'limegreen';
return;
}
pct++;
}