forked from JulianLaval/canvas-particle-network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle-network.js
274 lines (233 loc) · 8.15 KB
/
particle-network.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
// Set up ParticleNetwork appropriately for the environment.
(function (factory) {
// Establish the root object, `window` in the browser, or `global` on the server.
var root = (typeof self === 'object' && self.self === self && self) || (typeof global === 'object' && global.global === global && global);
// AMD.
if (typeof define === 'function' && define.amd) {
define(['exports'], function (exports) {
root.ParticleNetwork = factory(root, exports);
});
}
// Node.js or CommonJS.
else if (typeof module === 'object' && module.exports) {
module.exports = factory(root, {});
}
// Browser global.
else {
root.ParticleNetwork = factory(root, {});
}
}(function (root, ParticleNetwork) {
// Create Particle class
var Particle = function (parent) {
this.canvas = parent.canvas;
this.ctx = parent.ctx;
this.particleColor = parent.options.particleColor;
this.x = Math.random() * this.canvas.width;
this.y = Math.random() * this.canvas.height;
this.velocity = {
x: (Math.random() - 0.5) * parent.options.velocity,
y: (Math.random() - 0.5) * parent.options.velocity
};
};
Particle.prototype.update = function () {
// Change dir if outside map
if (this.x > this.canvas.width + 20 || this.x < -20) {
this.velocity.x = -this.velocity.x;
}
if (this.y > this.canvas.height + 20 || this.y < -20) {
this.velocity.y = -this.velocity.y;
}
// Update position
this.x += this.velocity.x;
this.y += this.velocity.y;
};
Particle.prototype.draw = function () {
// Draw particle
this.ctx.beginPath();
this.ctx.fillStyle = this.particleColor;
this.ctx.globalAlpha = 0.7;
this.ctx.arc(this.x, this.y, 1.5, 0, 2 * Math.PI);
this.ctx.fill();
};
// Create ParticleNetwork class
ParticleNetwork = function (canvas, options) {
this.canvasDiv = canvas;
this.canvasDiv.size = {
'width': this.canvasDiv.offsetWidth,
'height': this.canvasDiv.offsetHeight
};
// Set options
options = options !== undefined ? options : {};
this.options = {
particleColor: (options.particleColor !== undefined) ? options.particleColor : '#fff',
background: (options.background !== undefined) ? options.background : '#1a252f',
interactive: (options.interactive !== undefined) ? options.interactive : true,
velocity: this.setVelocity(options.speed),
density: this.setDensity(options.density)
};
this.init();
};
ParticleNetwork.prototype.init = function () {
// Create background div
this.bgDiv = document.createElement('div');
this.canvasDiv.appendChild(this.bgDiv);
this.setStyles(this.bgDiv, {
'position': 'absolute',
'top': 0,
'left': 0,
'bottom': 0,
'right': 0,
'z-index': 1
});
// Check if valid background hex color
if ((/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i).test(this.options.background)) {
this.setStyles(this.bgDiv, {
'background': this.options.background
});
}
// Else check if valid image
else if ((/\.(gif|jpg|jpeg|tiff|png)$/i).test(this.options.background)) {
this.setStyles(this.bgDiv, {
'background': 'url("' + this.options.background + '") no-repeat center',
'background-size': 'cover'
});
}
// Else throw error
else {
console.error('Please specify a valid background image or hexadecimal color');
return false;
}
// Check if valid particleColor
if (!(/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i).test(this.options.particleColor)) {
console.error('Please specify a valid particleColor hexadecimal color');
return false;
}
// Create canvas & context
this.canvas = document.createElement('canvas');
this.canvasDiv.appendChild(this.canvas);
this.ctx = this.canvas.getContext('2d');
this.canvas.width = this.canvasDiv.size.width;
this.canvas.height = this.canvasDiv.size.height;
this.setStyles(this.canvasDiv, { 'position': 'relative' });
this.setStyles(this.canvas, {
'z-index': '20',
'position': 'relative'
});
// Add resize listener to canvas
window.addEventListener('resize', function () {
// Check if div has changed size
if (this.canvasDiv.offsetWidth === this.canvasDiv.size.width && this.canvasDiv.offsetHeight === this.canvasDiv.size.height) {
return false;
}
// Scale canvas
this.canvas.width = this.canvasDiv.size.width = this.canvasDiv.offsetWidth;
this.canvas.height = this.canvasDiv.size.height = this.canvasDiv.offsetHeight;
// Set timeout to wait until end of resize event
clearTimeout(this.resetTimer);
this.resetTimer = setTimeout(function () {
// Reset particles
this.particles = [];
for (var i = 0; i < this.canvas.width * this.canvas.height / this.options.density; i++) {
this.particles.push(new Particle(this));
}
if (this.options.interactive) {
this.particles.push(this.mouseParticle);
}
// Update canvas
requestAnimationFrame(this.update.bind(this));
}.bind(this), 500);
}.bind(this));
// Initialise particles
this.particles = [];
for (var i = 0; i < this.canvas.width * this.canvas.height / this.options.density; i++) {
this.particles.push(new Particle(this));
}
if (this.options.interactive) {
// Add mouse particle if interactive
this.mouseParticle = new Particle(this);
this.mouseParticle.velocity = {
x: 0,
y: 0
};
this.particles.push(this.mouseParticle);
// Mouse event listeners
this.canvas.addEventListener('mousemove', function (e) {
this.mouseParticle.x = e.clientX - this.canvas.offsetParent.offsetLeft;
this.mouseParticle.y = e.clientY - this.canvas.offsetParent.offsetTop;
}.bind(this));
this.canvas.addEventListener('mouseup', function (e) {
this.mouseParticle.velocity = {
x: (Math.random() - 0.5) * this.options.velocity,
y: (Math.random() - 0.5) * this.options.velocity
};
this.mouseParticle = new Particle(this);
this.mouseParticle.velocity = {
x: 0,
y: 0
};
this.particles.push(this.mouseParticle);
}.bind(this));
}
// Update canvas
requestAnimationFrame(this.update.bind(this));
}
ParticleNetwork.prototype.update = function () {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.ctx.globalAlpha = 1;
// Draw particles
for (var i = 0; i < this.particles.length; i++) {
this.particles[i].update();
this.particles[i].draw();
// Draw connections
for (var j = this.particles.length - 1; j > i; j--) {
var distance = Math.sqrt(
Math.pow(this.particles[i].x - this.particles[j].x, 2)
+ Math.pow(this.particles[i].y - this.particles[j].y, 2)
);
if (distance > 120) {
continue;
}
this.ctx.beginPath();
this.ctx.strokeStyle = this.options.particleColor;
this.ctx.globalAlpha = (120 - distance) / 120;
this.ctx.lineWidth = 0.7;
this.ctx.moveTo(this.particles[i].x, this.particles[i].y);
this.ctx.lineTo(this.particles[j].x, this.particles[j].y);
this.ctx.stroke();
}
}
if (this.options.velocity !== 0) {
requestAnimationFrame(this.update.bind(this));
}
};
// Helper method to set velocity multiplier
ParticleNetwork.prototype.setVelocity = function (speed) {
if (speed === 'fast') {
return 1;
}
else if (speed === 'slow') {
return 0.33;
}
else if (speed === 'none') {
return 0;
}
return 0.66;
}
// Helper method to set density multiplier
ParticleNetwork.prototype.setDensity = function (density) {
if (density === 'high') {
return 5000;
}
else if (density === 'low') {
return 20000;
}
return !isNaN(parseInt(density, 10)) ? density : 10000;
}
// Helper method to set multiple styles
ParticleNetwork.prototype.setStyles = function (div, styles) {
for (var property in styles) {
div.style[property] = styles[property];
}
}
return ParticleNetwork;
}));