-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbouncingIcon.js
More file actions
149 lines (126 loc) · 3.53 KB
/
Copy pathbouncingIcon.js
File metadata and controls
149 lines (126 loc) · 3.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
/*
Bouncing Icon - v1.0
Makes any image bounce, useful to indicate data loading.
Inspired by the KDE loading animation.
https://github.com/koas/bouncingIcon
Usage:
To start bouncing call BI.start(imageUrl, position).
- imageUrl: required. URL of the image that will bounce.
- position: optional. Indicates where will the image bounce. Default value is
"followCursor", with this value the image will follow the mouse
pointer, as in KDE. You can also use "topLeft", "topRight",
"bottomLeft" and "bottomRight" to keep the image in any of the
four corners of the screen.
To stop bouncing, call BI.stop().
*/
var BI =
{
instance: null,
div: null,
icon: null,
iconX: 0,
iconY: 0,
bounceData: [[16, 16, 0, 0], [17, 15, 0, 1], [18, 14, -1, 2],
[19, 13, -1, 3], [20, 12, -2, 4], [21, 11, -3, 5],
[20, 12, -2, 4], [19, 13, -1, 3], [18, 14, -1, 2],
[17, 15, 0, 1], [16, 16, 0, 0], [15, 17, 0, -4],
[14, 18, 0, -8], [13, 19, 1, -12], [12, 20, 1, -16]],
bounceIndex: 0,
bounceDir: 1,
bounceIntervalId: 0,
start: function(imageUrl, position)
{
instance = this;
this.stop();
this.div = document.createElement("div");
this.div.style.position = "absolute";
this.div.style.zIndex = 100000;
this.icon = document.createElement("img");
this.icon.src = imageUrl;
this.icon.style.position = "absolute";
this.icon.width = 32;
this.icon.height = 32;
this.div.appendChild(this.icon);
document.body.appendChild(this.div);
this.bounceIndex = 0;
this.bounceDir = 1;
this.bounceIntervalId = setInterval(this.bouncing, 50);
switch (position)
{
case "topLeft":
this.div.style.position = "fixed";
this.div.style.left = "10px";
this.div.style.top = "30px";
break;
case "topRight":
this.div.style.position = "fixed";
this.div.style.right = "40px";
this.div.style.top = "30px";
break;
case "bottomLeft":
this.div.style.position = "fixed";
this.div.style.left = "10px";
this.div.style.bottom = "40px";
break;
case "bottomRight":
this.div.style.position = "fixed";
this.div.style.right = "40px";
this.div.style.bottom = "40px";
break;
default:
window.addEventListener("mousemove", this.moving);
break;
}
},
stop: function()
{
window.removeEventListener("mousemove", this.moving);
if (this.div !== null)
{
document.body.removeChild(this.div);
this.div = null;
clearInterval(this.bounceIntervalId);
}
},
moving: function(e)
{
var posX = 0;
var posY = 0;
if (!e)
e = window.event;
if (e.pageX || e.pageY)
{
posX = e.pageX;
posY = e.pageY;
}
else if (e.clientX || e.clientY)
{
posX = e.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
posY = e.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}
instance.div.style.left = (posX + 12) + "px";
instance.div.style.top = (posY + 12) + "px";
},
bouncing: function()
{
var data = instance.bounceData[instance.bounceIndex];
instance.icon.style.width = data[0] * 2 + 'px';
instance.icon.style.height = data[1] * 2 + 'px';
instance.icon.style.left = instance.iconX + data[2] * 2 +'px';
instance.icon.style.top = instance.iconY + data[3] * 2 + 'px';
instance.bounceIndex += instance.bounceDir;
if (instance.bounceDir == 1 && instance.bounceIndex == 15)
{
instance.bounceIndex = 14;
instance.bounceDir =- 1;
}
if (instance.bounceDir == -1 && instance.bounceIndex == 5)
{
instance.bounceIndex = 6;
instance.bounceDir = 1;
}
}
};
module.exports = BI;