-
Notifications
You must be signed in to change notification settings - Fork 18
/
faviconx.js
407 lines (373 loc) · 13.9 KB
/
faviconx.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
* A tiny javascript library that makes progress bars out of the favicon placeholders
* Check nicolasbize.github.io/faviconx/ for latest updates.
*
* Author: Nicolas Bize
* Created: Oct 10th 2013
* Last Updated: Oct 16th 2014
* Version: 1.0.1
* Licence: FavIconX is licenced under MIT licence (http://opensource.org/licenses/MIT)
*/
var FavIconX = (function() {
// my private parts, do not mess with them.
var originalIcon;
var value = 0;
var animValue = 0;
var canvas = document.createElement('canvas');
var context;
var icon; // image storage
var animInterval = null;
var animCallback = null;
var oldTitle = "";
var head = null;
var isReset = null;
// default values for properties accessed through .config() method:
var shape;
var animated;
var animationSpeed; // ms to go from one value to the one from setValue
var borderColor;
var borderColor2;
var shadowColor;
var borderWidth;
var fillColor;
var fillColor2;
var updateTitle;
var titleRenderer;
function setDefaults(){
shape = 'circle';
animated = false;
animationSpeed = 2000;
animCallback = null;
borderColor = '#3A70B1';
borderColor2 = null;
shadowColor = 'rgba(255, 0, 0, 0)';
borderWidth = 1;
fillColor = '#3A70B1';
fillColor2 = null;
updateTitle = true;
value = 0;
isReset = true;
titleRenderer = function(val, title){
return '[' + val + '%] - ' + title;
};
}
// Generates the wanted shape
function generateBitmap(v){
if(shape === 'circle'){
generateCircle(v);
} else if(shape === 'doughnut'){
generateDoughnut(v);
} else if(shape === 'square'){
generateSquare(v);
}
}
// (255, 0, 0) => "#FF0000"
function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
// "#FF0000" => [255, 0, 0]
function hexToRgb(hex) {
hex = hex.indexOf("#") === 0 ? hex.substring(1) : hex;
var nb = parseInt(hex, 16);
var r = (nb >> 16) & 255;
var g = (nb >> 8) & 255;
var b = nb & 255;
return [r,g,b];
}
// because i don't speak in radians
function toRad(deg){
return deg * Math.PI / 180;
}
// gets a mid color according to current value
// col1 = 0%, col2 = 100%
function getMidColor(col1, col2){
var rgb1 = hexToRgb(col1);
var rgb2 = hexToRgb(col2);
var avg = [0, 0, 0];
var inv_v = (100 - (animValue || value)) / 100;
for(var i=0; i<3; i++){
avg[i] = rgb1[i] * inv_v + rgb2[i] * (1-inv_v);
}
return rgbToHex(Math.round(avg[0]), Math.round(avg[1]), Math.round(avg[2]));
}
// let's get a boring circle first
function generateCircle(v){
var graphValue = v || value;
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
var radius = 7 - borderWidth / 2;
var deg = graphValue * 3.6 - 90;
canvas.width = canvas.width;
context.lineWidth = borderWidth;
context.strokeStyle = borderColor2 ? getMidColor(borderColor, borderColor2) : borderColor;
context.beginPath();
context.arc(centerX, centerY, radius, 0, toRad(360), false);
context.closePath();
context.fillStyle = shadowColor;
context.fill();
context.stroke();
if(graphValue > 0){
context.fillStyle = fillColor2 ? getMidColor(fillColor, fillColor2) : fillColor;
context.beginPath();
context.moveTo(8,8);
context.arc(centerX, centerY, radius, toRad(-90), toRad(deg), false);
context.closePath();
context.fill();
}
}
// spelling doughnut just feels wrong
function generateDoughnut(v){
var graphValue = v || value;
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
var deg = graphValue * 3.6 - 90;
canvas.width = canvas.width;
if(graphValue > 0){
context.lineWidth = borderWidth;
context.strokeStyle = shadowColor;
context.beginPath();
context.arc(centerX, centerY, 6, 0, toRad(360), false);
context.arc(centerX, centerY, 6 - borderWidth, 0, toRad(360), true);
context.closePath();
context.stroke();
context.strokeStyle = fillColor2 ? getMidColor(fillColor, fillColor2) : fillColor;
context.beginPath();
context.arc(centerX, centerY, 6, toRad(-90), toRad(deg), false);
context.arc(centerX, centerY, 6 - borderWidth, toRad(deg), toRad(-90), true);
context.closePath();
context.stroke();
}
}
// something a bit more fancy
function generateSquare(v){
var graphValue = v || value;
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
var borderRadius = 2;
var x = 1;
var y = 1;
var width = 14;
var height = 14;
var deg = graphValue * 3.6 - 90;
canvas.width = canvas.width;
context.beginPath();
context.strokeStyle = borderColor2 ? getMidColor(borderColor, borderColor2) : borderColor;
context.lineWidth = borderWidth;
context.fillStyle = shadowColor;
context.moveTo(x + borderRadius, y);
context.lineTo(x + width - borderRadius, y);
context.quadraticCurveTo(x + width, y, x + width, y + borderRadius);
context.lineTo(x + width, y + height - borderRadius);
context.quadraticCurveTo(x + width, y + height, x + width - borderRadius, y + height);
context.lineTo(x + borderRadius, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - borderRadius);
context.lineTo(x, y + borderRadius);
context.quadraticCurveTo(x, y, x + borderRadius, y);
context.closePath();
context.stroke();
if(graphValue > 0){
context.clip();
context.beginPath();
context.moveTo(8,8);
context.fillStyle = 'rgba(255, 0, 0, 0)';
context.arc(centerX, centerY, 12, toRad(-90), toRad(deg), false); // we clip the rest
context.closePath();
context.fill();
context.clip();
context.beginPath();
context.fillStyle = fillColor2 ? getMidColor(fillColor, fillColor2) : fillColor;
context.moveTo(x + borderRadius, y);
context.lineTo(x + width - borderRadius, y);
context.quadraticCurveTo(x + width, y, x + width, y + borderRadius);
context.lineTo(x + width, y + height - borderRadius);
context.quadraticCurveTo(x + width, y + height, x + width - borderRadius, y + height);
context.lineTo(x + borderRadius, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - borderRadius);
context.lineTo(x, y + borderRadius);
context.quadraticCurveTo(x, y, x + borderRadius, y);
context.closePath();
context.fill();
}
}
// draw me a check mark
function generateSuccess(bgColor, fgColor){
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
canvas.width = canvas.width;
context.beginPath();
context.arc(centerX, centerY, 8, 0, 2 * Math.PI, false);
context.fillStyle = bgColor || '#53C516';
context.fill();
context.beginPath();
context.lineWidth = 2;
context.strokeStyle = fgColor || '#FFFFFF';
context.moveTo(4, 9);
context.lineTo(8, 12);
context.lineTo(12, 4);
context.stroke();
}
// how about the red cross of death?
function generateFailure(bgColor, fgColor){
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
context.beginPath();
context.arc(centerX, centerY, 8, 0, 2 * Math.PI, false);
context.fillStyle = bgColor || '#F6491F';
context.fill();
context.beginPath();
context.lineWidth = 2;
context.strokeStyle = fgColor || '#FFFFFF';
context.moveTo(5, 4);
context.lineTo(11, 12);
context.moveTo(11, 4);
context.lineTo(5, 12);
context.stroke();
}
// refresh the tab favicon
function refreshFavIcon(){
head.removeChild(icon);
icon.setAttribute('href', canvas.toDataURL("image/x-icon"));
head.appendChild(icon);
if(updateTitle){
document.title = titleRenderer.call(this, animValue || value, oldTitle);
}
}
// either we already have a favicon and we want to keep it for resets...
// or we create one that we call bob and that we care for
function getIconRef(){
head = document.getElementsByTagName('head')[0];
var els = document.getElementsByTagName('link');
if(els.length > 0){
for(var i=0; i<els.length; i++){
if(els[i].getAttribute('rel').split(' ').indexOf('icon') > -1){
originalIcon = els[i].cloneNode(true); // keep the original for reset()
return els[i];
}
}
}
// no favicon found. create one.
var newIcon = document.createElement('link');
newIcon.setAttribute('rel', 'shortcut icon');
newIcon.setAttribute('type', 'image/png');
head.appendChild(newIcon);
return newIcon;
}
// starting point to the app... couldn't hide it in the code better
function init(){
if(canvas && canvas.getContext){
icon = getIconRef();
oldTitle = document.title;
canvas.height = canvas.width = 16;
context = canvas.getContext('2d');
} else {
throw 'No support for Canvas, no chocolate for you! =(';
}
}
// animation: called again and again until we get to the right value
function incValue(inc){
animValue += inc;
if(isReset !== true){
generateBitmap(animValue);
refreshFavIcon();
if(animValue !== null && animated){
if(animValue === value){
stopAnim();
} else {
setTimeout(function(){
incValue(animValue > value ? -1 : 1);
}, animInterval);
}
}
}
}
// this, as the method name suggests, is called once the anim has stopped.
function stopAnim(){
if(animCallback){
animCallback.call(this, value);
animCallback = null;
}
}
// let's get things started.
setDefaults();
init();
// we get to the public methods now.
return {
// a helper to configure mucho settings at once
config: function(cfg){
shape = cfg.shape || shape;
isReset = false;
animated = cfg.animated || animated;
animationSpeed = cfg.animationSpeed || animationSpeed;
borderColor = cfg.borderColor || borderColor;
borderColor2 = cfg.borderColor2 || borderColor2;
borderWidth = cfg.borderWidth || borderWidth;
shadowColor = cfg.shadowColor || shadowColor;
fillColor = cfg.fillColor || fillColor;
fillColor2 = cfg.fillColor2 || fillColor2;
updateTitle = cfg.updateTitle || updateTitle;
titleRenderer = cfg.titleRenderer || titleRenderer;
animCallback = cfg.callback !== undefined ? cfg.callback : animCallback;
generateBitmap();
refreshFavIcon();
return FavIconX;
},
// setValue(value, [animated], [speed], [callback])
// value must be between 0 and 100
// when specified the animated overrides the component setting.
// when specified the speed orrides the component setting
// callback called once the animation is finished
setValue: function(v, isAnimated, animSpeed, callback){
if(v<0 || v>100) throw 'value must be between 0 and 100';
animCallback = callback !== undefined ? callback : animCallback;
animValue = value;
value = v;
isReset = false;
if(isAnimated !== undefined ? isAnimated : animated){
var steps = animValue - value;
if(animValue !== value){
animInterval = Math.abs(Math.ceil((animSpeed !== undefined ? animSpeed : animationSpeed) / steps));
incValue(animValue > value ? -1 : 1);
}
} else {
animValue = null;
generateBitmap();
refreshFavIcon();
if(animCallback){
animCallback.call(this, v);
}
}
return FavIconX;
},
// returns the current value of the widget
getValue: function(){
return animValue || value;
},
// restoring the past... or is it...
reset: function(){
setDefaults();
if(originalIcon){
head.removeChild(icon);
icon = originalIcon.cloneNode(true);
head.appendChild(icon);
}
if(updateTitle){
document.title = oldTitle;
}
return FavIconX;
},
// displays the awesome checkbox. colors can be tweaked here
complete: function(fgColor, bgColor){
isReset = false;
generateSuccess(fgColor, bgColor);
refreshFavIcon();
document.title = oldTitle;
},
// displays the even more awesome cross. colors can be tweaked here
fail: function(fgColor, bgColor){
isReset = false;
generateFailure(fgColor, bgColor);
refreshFavIcon();
document.title = oldTitle;
}
};
})();