-
Notifications
You must be signed in to change notification settings - Fork 86
/
progress.js
100 lines (87 loc) · 2.76 KB
/
progress.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
/**
* Provides a fancy animation to the download icon.
*
* Copyright (C) 2014 Peter Wu <[email protected]>
*/
'use strict';
/* jshint browser:true, devel:true */
/* globals chrome */
/* exported progress */
var progress = (function () {
// number of slices in a round
var ROTATIONS = 8;
// tabId => { counter: counter, timer: timerId ]
var animations = {};
var img = new Image();
// assume that the image is loaded before being used for the canvas
img.src = 'icon_16.png';
function restoreIcon(tabId) {
if (tabId in animations) {
clearInterval(animations[tabId].timer);
delete animations[tabId];
}
// FIXME: image currently shrinks from 19x19 to 16x16
chrome.pageAction.setIcon({
path: img.src,
tabId: tabId
});
}
function getImage(counter) {
var canvas = document.createElement('canvas');
canvas.width = canvas.height = 19;
var ctx = canvas.getContext('2d');
var s = 19 / img.height;
ctx.scale(s, s);
// rotate around center point.
var h = img.height / 2;
ctx.translate(h, h);
ctx.rotate(2 * Math.PI / ROTATIONS * (counter % ROTATIONS));
ctx.translate(-h, -h);
// draw original icon
ctx.drawImage(img, 0, 0);
return ctx.getImageData(0, 0, 19, 19);
}
function drawIcon(tabId) {
var anim = animations[tabId];
chrome.pageAction.setIcon({
imageData: getImage(++anim.counter),
tabId: tabId
}, function () {
if (chrome.runtime.lastError) {
console.warn("drawIcon:", chrome.runtime.lastError);
}
});
// in case the network has issues?
if (anim.counter >= ROTATIONS * 16) {
console.log('Warning: animation took too long, cancelling.');
restoreIcon(tabId);
}
}
function setAnimatedIcon(tabId) {
if (tabId in animations) {
clearInterval(animations[tabId].timer);
}
var anim = animations[tabId] = {};
anim.counter = 0;
anim.timer = setInterval(drawIcon, 250, tabId);
drawIcon(tabId);
}
function setFinished(tabId, color) {
restoreIcon(tabId);
// TODO: distinguish between success or failure?
}
return {
downloadStarting: function (tabId) {
console.log('Download starting...');
setAnimatedIcon(tabId);
},
downloadFailed: function (tabId) {
console.log('Download failed.');
setFinished(tabId, 'red');
},
downloadStarted: function (tabId) {
console.log('Download started.');
setFinished(tabId, 'green');
}
};
})();