-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.firefly.js
206 lines (173 loc) · 8.28 KB
/
jquery.firefly.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
/*
* FireFly v1.1.1 - jQuery plugin
*
* An updated version of Dharmveer Motyar's firefly plugin.
* Creates an effect of fireflys flying around your page.
*
* @example $.firefly()
*
* // to stop and start
* $.firefly.stop()
* $.firefly.start()
*
* Copyright © 2011 Matt 'Dirty Monkey' Stevens, http://www.dirtymonkey.co.uk
* License: http://www.opensource.org/licenses/mit-license.php
*/
(function($){
/*
* default settings
*/
var defaults = {
// base64 encoded to cut down requests,
// doesn't work in IE <= 7.0 and I'm not bothered to be honest
images: [ // spark one
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGMAAABjCAYAAACPO76VAAAA'+
'BGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAA'+
'D6SURBVHja7Nc7CsJAAEVRM/7ARqws3f+q3IBVrETC+AIpUthPBs6FB2nDYfIZaq07baMB'+
'BgzBgCEYMAQDhmDAEAwYgiEYMAQDhmDAEAwYggFDMGAIhmDAEAwYggFDMGAIBgzBgCEYgg'+
'FDMGA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9BXxSykRmLpqj1QlgCAYMwYAh'+
'GDAEA4ZgwBAMwYAhGDAEA4ZgwBAMGIIBQzAEA4ZgwBAMGIIBQzBgCAYMwRAMGIIBQzBgCA'+
'YMwXgXta3WYIyv147a/heM+PBPbO2BOGsXDHmmYAgGDMGAIRiCAUMwYAgGDMGAIRgwBAOG'+
'E8AQDBiCAUMwYAgGDMGAIRiCAUMwYAgGDMGAIRgwBAOGYAgGDMGYvVuAAQD5BwwNjgAKGw'+
'AAAABJRU5ErkJggg%3D%3D'
],
total: 50, // anything over a few hundred is gonna crawl
boundary: 100, // avoid the edge of the window
fast: 5, // fastest spark (a lower number is faster)
slow: 1, // slowest spark (a higher number is slower)
limit: 3600, // stop after this many seconds (one hour)
}
// shortcuts
var ff, ffs, x, y, old_x, old_y, start, timer_handle, delay_handle, sparks = []
// init
// initializes and starts everything
$.firefly = function(settings){
ff = $.firefly
ffs = $.extend({}, defaults, settings)
ff.calibrate() // get size of window
ff.make() // load the spark images
ff.start() // start moving them
ff.check_time() // start timer
}
// create
// creates every spark and inserts it into the DOM
$.firefly.make = function(){
for (i = 0; i < ffs.total; i++)
sparks[i] = ff.create(ffs.images[ff.random(0,ffs.images.length-1)])
}
// start
// records start time, watches window resize and begins movement
$.firefly.start = function(){
start = parseInt((new Date).getTime()/1000)
$(window).bind('resize.firefly', function(){
ff.delay(ff.resized, 250)
})
for (i = 0; i < sparks.length; i++) ff.move(sparks[i])
}
// reset
// stop and remove all fireflies, then create and start them again
$.firefly.reset = function(){
for (i = 0; i < ffs.total; i++)
sparks[i].remove()
$.firefly()
}
// delay
// utility function to prevent the resize event from being fired multiple times
$.firefly.delay = function(callback, milliseconds){
clearTimeout(delay_handle)
delay_handle = setTimeout(callback, milliseconds);
}
// resized
// event handler to deal with window resizing
$.firefly.resized = function(e){
ff.calibrate();
(x < old_x || y < old_y) && ff.reset()
}
// stop
// freezes a spark in its current position
$.firefly.stop = function(){
$(window).unbind('resize.firefly')
for (i = 0; i < sparks.length; i++) $(sparks[i]).stop(true)
}
// remove
// removes a spark from the DOM
$.firefly.remove = function() {
for (i = 0; i < ffs.total; i++) $('img[src="'+ffs.images[i]+'"]').remove()
}
// create
// inserts a spark into the DOM at a random position
$.firefly.create = function(i){
var spark = $('<img>').attr({'src':i}).hide()
$("body").append(spark)
spark.css({'position': 'absolute',
'z-index': ff.random(0, 20),
'top': ff.random(ffs.boundary, y - ffs.boundary),
'left': ff.random(ffs.boundary, x - ffs.boundary)
})
return spark.show()
}
// move
// animate to a random position, within the boundaries
$.firefly.move = function(image){
image.animate({top: ff.random(ffs.boundary,y-ffs.boundary), // x coordinate
left: ff.random(ffs.boundary,x-ffs.boundary)}, // y coordinate
ff.random(ffs.slow,ffs.fast) * 1000, // speed
function(){ff.move(image)}) // callback
}
// check_time
// check every second whether or not we have passed the time limit
$.firefly.check_time = function(){
if (parseInt((new Date).getTime()/1000) > (start+ffs.limit)) {
ff.stop()
} else {
window.setTimeout('$.firefly.check_time()', 1000)
}
}
// calibrate
// get the dimensions of the window
$.firefly.calibrate = function(){
old_x = x
old_y = y
x = $(window).width()
y = $(window).height()
}
// random
// return a random number between a and b
$.firefly.random = function(a,b){
return Math.floor(Math.random()*(b-a+1)+a)
}
})(jQuery)