Debian Codegraph @

vendor/assets/javascripts/favcount.js

raw
1 /*
2 * favcount.js v1.5.0
3 * http://chrishunt.co/favcount
4 * Dynamically updates the favicon with a number.
5 *
6 * Copyright 2013, Chris Hunt
7 * Released under the MIT license
8 */
9
10 (function(){
11 function Favcount(icon) {
12 this.icon = icon;
13 this.opacity = 0.4;
14 this.canvas = document.createElement('canvas');
15 this.font = "Helvetica, Arial, sans-serif";
16 }
17
18 Favcount.prototype.set = function(count) {
19 var self = this,
20 img = document.createElement('img');
21
22 if (self.canvas.getContext) {
23 img.crossOrigin = "anonymous";
24
25 img.onload = function() {
26 drawCanvas(self.canvas, self.opacity, self.font, img, normalize(count));
27 };
28
29 img.src = this.icon;
30 }
31 };
32
33 function normalize(count) {
34 count = Math.round(count);
35
36 if (isNaN(count) || count < 1) {
37 return '';
38 } else if (count < 10) {
39 return ' ' + count;
40 } else if (count > 99) {
41 return '99';
42 } else {
43 return count;
44 }
45 }
46
47 function drawCanvas(canvas, opacity, font, img, count) {
48 var head = document.getElementsByTagName('head')[0],
49 favicon = document.createElement('link'),
50 multiplier, fontSize, context, xOffset, yOffset, border, shadow;
51
52 favicon.rel = 'icon';
53
54 // Scale canvas elements based on favicon size
55 multiplier = img.width / 16;
56 fontSize = multiplier * 11;
57 xOffset = multiplier;
58 yOffset = multiplier * 11;
59 border = multiplier;
60 shadow = multiplier * 2;
61
62 canvas.height = canvas.width = img.width;
63 context = canvas.getContext('2d');
64 context.font = 'bold ' + fontSize + 'px ' + font;
65
66 // Draw faded favicon background
67 if (count) { context.globalAlpha = opacity; }
68 context.drawImage(img, 0, 0);
69 context.globalAlpha = 1.0;
70
71 // Draw white drop shadow
72 context.shadowColor = '#FFF';
73 context.shadowBlur = shadow;
74 context.shadowOffsetX = 0;
75 context.shadowOffsetY = 0;
76
77 // Draw white border
78 context.fillStyle = '#FFF';
79 context.fillText(count, xOffset, yOffset);
80 context.fillText(count, xOffset + border, yOffset);
81 context.fillText(count, xOffset, yOffset + border);
82 context.fillText(count, xOffset + border, yOffset + border);
83
84 // Draw black count
85 context.fillStyle = '#000';
86 context.fillText(count,
87 xOffset + (border / 2.0),
88 yOffset + (border / 2.0)
89 );
90
91 // Replace favicon with new favicon
92 favicon.href = canvas.toDataURL('image/png');
93 head.removeChild(document.querySelector('link[rel=icon]'));
94 head.appendChild(favicon);
95 }
96
97 this.Favcount = Favcount;
98 }).call(this);
99
100 (function(){
101 Favcount.VERSION = '1.5.0';
102 }).call(this);