-
Notifications
You must be signed in to change notification settings - Fork 512
/
jquery.particleground.js
453 lines (405 loc) · 13.7 KB
/
jquery.particleground.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*!
* Particleground
*
* @author Jonathan Nicol - @mrjnicol
* @version 1.1.0
* @description Creates a canvas based particle system background
*
* Inspired by http://requestlab.fr/ and http://disruptivebydesign.com/
*/
;(function(window, document) {
"use strict";
var pluginName = 'particleground';
// http://youmightnotneedjquery.com/#deep_extend
function extend(out) {
out = out || {};
for (var i = 1; i < arguments.length; i++) {
var obj = arguments[i];
if (!obj) continue;
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === 'object')
deepExtend(out[key], obj[key]);
else
out[key] = obj[key];
}
}
}
return out;
};
var $ = window.jQuery;
function Plugin(element, options) {
var canvasSupport = !!document.createElement('canvas').getContext;
var canvas;
var ctx;
var particles = [];
var raf;
var mouseX = 0;
var mouseY = 0;
var winW;
var winH;
var desktop = !navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|BB10|mobi|tablet|opera mini|nexus 7)/i);
var orientationSupport = !!window.DeviceOrientationEvent;
var tiltX = 0;
var pointerX;
var pointerY;
var tiltY = 0;
var paused = false;
options = extend({}, window[pluginName].defaults, options);
/**
* Init
*/
function init() {
if (!canvasSupport) { return; }
//Create canvas
canvas = document.createElement('canvas');
canvas.className = 'pg-canvas';
canvas.style.display = 'block';
element.insertBefore(canvas, element.firstChild);
ctx = canvas.getContext('2d');
styleCanvas();
// Create particles
var numParticles = Math.round((canvas.width * canvas.height) / options.density);
for (var i = 0; i < numParticles; i++) {
var p = new Particle();
p.setStackPos(i);
particles.push(p);
};
window.addEventListener('resize', function() {
resizeHandler();
}, false);
document.addEventListener('mousemove', function(e) {
mouseX = e.pageX;
mouseY = e.pageY;
}, false);
if (orientationSupport && !desktop) {
window.addEventListener('deviceorientation', function () {
// Contrain tilt range to [-30,30]
tiltY = Math.min(Math.max(-event.beta, -30), 30);
tiltX = Math.min(Math.max(-event.gamma, -30), 30);
}, true);
}
draw();
hook('onInit');
}
/**
* Style the canvas
*/
function styleCanvas() {
canvas.width = element.offsetWidth;
canvas.height = element.offsetHeight;
ctx.fillStyle = options.dotColor;
ctx.strokeStyle = options.lineColor;
ctx.lineWidth = options.lineWidth;
}
/**
* Draw particles
*/
function draw() {
if (!canvasSupport) { return; }
winW = window.innerWidth;
winH = window.innerHeight;
// Wipe canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update particle positions
for (var i = 0; i < particles.length; i++) {
particles[i].updatePosition();
};
// Draw particles
for (var i = 0; i < particles.length; i++) {
particles[i].draw();
};
// Call this function next time screen is redrawn
if (!paused) {
raf = requestAnimationFrame(draw);
}
}
/**
* Add/remove particles.
*/
function resizeHandler() {
// Resize the canvas
styleCanvas();
var elWidth = element.offsetWidth;
var elHeight = element.offsetHeight;
// Remove particles that are outside the canvas
for (var i = particles.length - 1; i >= 0; i--) {
if (particles[i].position.x > elWidth || particles[i].position.y > elHeight) {
particles.splice(i, 1);
}
};
// Adjust particle density
var numParticles = Math.round((canvas.width * canvas.height) / options.density);
if (numParticles > particles.length) {
while (numParticles > particles.length) {
var p = new Particle();
particles.push(p);
}
} else if (numParticles < particles.length) {
particles.splice(numParticles);
}
// Re-index particles
for (i = particles.length - 1; i >= 0; i--) {
particles[i].setStackPos(i);
};
}
/**
* Pause particle system
*/
function pause() {
paused = true;
}
/**
* Start particle system
*/
function start() {
paused = false;
draw();
}
/**
* Particle
*/
function Particle() {
this.stackPos;
this.active = true;
this.layer = Math.ceil(Math.random() * 3);
this.parallaxOffsetX = 0;
this.parallaxOffsetY = 0;
// Initial particle position
this.position = {
x: Math.ceil(Math.random() * canvas.width),
y: Math.ceil(Math.random() * canvas.height)
}
// Random particle speed, within min and max values
this.speed = {}
switch (options.directionX) {
case 'left':
this.speed.x = +(-options.maxSpeedX + (Math.random() * options.maxSpeedX) - options.minSpeedX).toFixed(2);
break;
case 'right':
this.speed.x = +((Math.random() * options.maxSpeedX) + options.minSpeedX).toFixed(2);
break;
default:
this.speed.x = +((-options.maxSpeedX / 2) + (Math.random() * options.maxSpeedX)).toFixed(2);
this.speed.x += this.speed.x > 0 ? options.minSpeedX : -options.minSpeedX;
break;
}
switch (options.directionY) {
case 'up':
this.speed.y = +(-options.maxSpeedY + (Math.random() * options.maxSpeedY) - options.minSpeedY).toFixed(2);
break;
case 'down':
this.speed.y = +((Math.random() * options.maxSpeedY) + options.minSpeedY).toFixed(2);
break;
default:
this.speed.y = +((-options.maxSpeedY / 2) + (Math.random() * options.maxSpeedY)).toFixed(2);
this.speed.x += this.speed.y > 0 ? options.minSpeedY : -options.minSpeedY;
break;
}
}
/**
* Draw particle
*/
Particle.prototype.draw = function() {
// Draw circle
ctx.beginPath();
ctx.arc(this.position.x + this.parallaxOffsetX, this.position.y + this.parallaxOffsetY, options.particleRadius / 2, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fill();
// Draw lines
ctx.beginPath();
// Iterate over all particles which are higher in the stack than this one
for (var i = particles.length - 1; i > this.stackPos; i--) {
var p2 = particles[i];
// Pythagorus theorum to get distance between two points
var a = this.position.x - p2.position.x
var b = this.position.y - p2.position.y
var dist = Math.sqrt((a * a) + (b * b)).toFixed(2);
// If the two particles are in proximity, join them
if (dist < options.proximity) {
ctx.moveTo(this.position.x + this.parallaxOffsetX, this.position.y + this.parallaxOffsetY);
if (options.curvedLines) {
ctx.quadraticCurveTo(Math.max(p2.position.x, p2.position.x), Math.min(p2.position.y, p2.position.y), p2.position.x + p2.parallaxOffsetX, p2.position.y + p2.parallaxOffsetY);
} else {
ctx.lineTo(p2.position.x + p2.parallaxOffsetX, p2.position.y + p2.parallaxOffsetY);
}
}
}
ctx.stroke();
ctx.closePath();
}
/**
* update particle position
*/
Particle.prototype.updatePosition = function() {
if (options.parallax) {
if (orientationSupport && !desktop) {
// Map tiltX range [-30,30] to range [0,winW]
var ratioX = (winW - 0) / (30 - -30);
pointerX = (tiltX - -30) * ratioX + 0;
// Map tiltY range [-30,30] to range [0,winH]
var ratioY = (winH - 0) / (30 - -30);
pointerY = (tiltY - -30) * ratioY + 0;
} else {
pointerX = mouseX;
pointerY = mouseY;
}
// Calculate parallax offsets
this.parallaxTargX = (pointerX - (winW / 2)) / (options.parallaxMultiplier * this.layer);
this.parallaxOffsetX += (this.parallaxTargX - this.parallaxOffsetX) / 10; // Easing equation
this.parallaxTargY = (pointerY - (winH / 2)) / (options.parallaxMultiplier * this.layer);
this.parallaxOffsetY += (this.parallaxTargY - this.parallaxOffsetY) / 10; // Easing equation
}
var elWidth = element.offsetWidth;
var elHeight = element.offsetHeight;
switch (options.directionX) {
case 'left':
if (this.position.x + this.speed.x + this.parallaxOffsetX < 0) {
this.position.x = elWidth - this.parallaxOffsetX;
}
break;
case 'right':
if (this.position.x + this.speed.x + this.parallaxOffsetX > elWidth) {
this.position.x = 0 - this.parallaxOffsetX;
}
break;
default:
// If particle has reached edge of canvas, reverse its direction
if (this.position.x + this.speed.x + this.parallaxOffsetX > elWidth || this.position.x + this.speed.x + this.parallaxOffsetX < 0) {
this.speed.x = -this.speed.x;
}
break;
}
switch (options.directionY) {
case 'up':
if (this.position.y + this.speed.y + this.parallaxOffsetY < 0) {
this.position.y = elHeight - this.parallaxOffsetY;
}
break;
case 'down':
if (this.position.y + this.speed.y + this.parallaxOffsetY > elHeight) {
this.position.y = 0 - this.parallaxOffsetY;
}
break;
default:
// If particle has reached edge of canvas, reverse its direction
if (this.position.y + this.speed.y + this.parallaxOffsetY > elHeight || this.position.y + this.speed.y + this.parallaxOffsetY < 0) {
this.speed.y = -this.speed.y;
}
break;
}
// Move particle
this.position.x += this.speed.x;
this.position.y += this.speed.y;
}
/**
* Setter: particle stacking position
*/
Particle.prototype.setStackPos = function(i) {
this.stackPos = i;
}
function option (key, val) {
if (val) {
options[key] = val;
} else {
return options[key];
}
}
function destroy() {
console.log('destroy');
canvas.parentNode.removeChild(canvas);
hook('onDestroy');
if ($) {
$(element).removeData('plugin_' + pluginName);
}
}
function hook(hookName) {
if (options[hookName] !== undefined) {
options[hookName].call(element);
}
}
init();
return {
option: option,
destroy: destroy,
start: start,
pause: pause
};
}
window[pluginName] = function(elem, options) {
return new Plugin(elem, options);
};
window[pluginName].defaults = {
minSpeedX: 0.1,
maxSpeedX: 0.7,
minSpeedY: 0.1,
maxSpeedY: 0.7,
directionX: 'center', // 'center', 'left' or 'right'. 'center' = dots bounce off edges
directionY: 'center', // 'center', 'up' or 'down'. 'center' = dots bounce off edges
density: 10000, // How many particles will be generated: one particle every n pixels
dotColor: '#666666',
lineColor: '#666666',
particleRadius: 7, // Dot size
lineWidth: 1,
curvedLines: false,
proximity: 100, // How close two dots need to be before they join
parallax: true,
parallaxMultiplier: 5, // The lower the number, the more extreme the parallax effect
onInit: function() {},
onDestroy: function() {}
};
// nothing wrong with hooking into jQuery if it's there...
if ($) {
$.fn[pluginName] = function(options) {
if (typeof arguments[0] === 'string') {
var methodName = arguments[0];
var args = Array.prototype.slice.call(arguments, 1);
var returnVal;
this.each(function() {
if ($.data(this, 'plugin_' + pluginName) && typeof $.data(this, 'plugin_' + pluginName)[methodName] === 'function') {
returnVal = $.data(this, 'plugin_' + pluginName)[methodName].apply(this, args);
}
});
if (returnVal !== undefined){
return returnVal;
} else {
return this;
}
} else if (typeof options === "object" || !options) {
return this.each(function() {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin(this, options));
}
});
}
};
}
})(window, document);
/**
* requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
* @see: http://paulirish.com/2011/requestanimationframe-for-smart-animating/
* @see: http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
* @license: MIT license
*/
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|| window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());