Skip to content
This repository was archived by the owner on May 26, 2020. It is now read-only.

Commit 770acb3

Browse files
author
Marc Brüderlin
committed
Merge branch 'release-2.2.0'
2 parents ac7e2df + 924bb58 commit 770acb3

6 files changed

Lines changed: 5109 additions & 63 deletions

File tree

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ particles.js is a lightweight, dependency-free and responsive javascript plugin
1616
There are several ways to install particles.js:
1717
- [Download the latest version](https://github.com/marcbruederlin/particles.js/archive/master.zip)
1818
- Install with npm: `npm install particlesjs --save`
19-
- Use the CDN: `https://cdnjs.cloudflare.com/ajax/libs/particlesjs/2.1.0/particles.min.js`
19+
- Use the CDN: `https://cdnjs.cloudflare.com/ajax/libs/particlesjs/2.2.0/particles.min.js`
2020

2121
## Usage
2222
Include the minified JS in your HTML (right before the closing body tag).
@@ -69,9 +69,9 @@ Option | Type | Default | Description
6969
`maxParticles` | integer | `100` | *Optional:* Maximum amount of particles
7070
`sizeVariations` | integer | `3` | *Optional:* Amount of size variations
7171
`speed` | integer | `0.5` | *Optional:* Movement speed of the particles
72-
`color` | string | `#000000` | *Optional:* Color of particles and connecting lines
72+
`color` | string or string[] | `#000000` | *Optional:* Color(s) of the particles and connecting lines
7373
`minDistance` | integer | `120` | *Optional:* Distance in `px` for connecting lines
74-
`connectParticles` | boolean | `false` | *Optional:* `true`/`false` if connecting lines should be drawn
74+
`connectParticles` | boolean | `false` | *Optional:* `true`/`false` if connecting lines should be drawn or not
7575
`responsive` | array | `null` | *Optional:* Array of objects containing breakpoints and options
7676

7777
Example how to use the [responsive option](https://marcbruederlin.github.io/particles.js/#responsive-option).
@@ -87,6 +87,9 @@ Example how to use the [public methods](https://marcbruederlin.github.io/particl
8787
## Browser Support
8888
IE9+ and all modern browsers.
8989

90+
## Examples
91+
See [various examples](https://marcbruederlin.github.io/particles.js/#examples) how you can use particles.js.
92+
9093
## Build
9194
To compile the distribution files by yourself, make sure that you have node.js and gulp installed, then:
9295
- Clone the repository: `https://github.com/marcbruederlin/particles.js.git`

dist/particles.js

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* A lightweight, dependency-free and responsive javascript plugin for particle backgrounds.
33
*
44
* @author Marc Bruederlin <hello@marcbruederlin.com>
5-
* @version 2.1.0
5+
* @version 2.2.0
66
* @license MIT
77
* @see https://github.com/marcbruederlin/particles.js
88
*/
@@ -72,7 +72,6 @@ var Particles = (function(window, document) {
7272
var _ = this;
7373

7474
_.options = _._extend(_.defaults, settings);
75-
_.options.color = ((settings.color) ? _._hex2rgb(settings.color) : _._hex2rgb(_.defaults.color));
7675
_.originalSettings = JSON.parse(JSON.stringify(_.options));
7776

7877
_._animate = _._animate.bind(_);
@@ -83,6 +82,7 @@ var Particles = (function(window, document) {
8382
_._checkResponsive();
8483
_._initializeStorage();
8584
_._animate();
85+
8686
return _;
8787
};
8888

@@ -267,7 +267,7 @@ var Particles = (function(window, document) {
267267
};
268268

269269
/**
270-
* Animates particles by calling _animate if there exists no _animation
270+
* Restarts the particles animation by calling _animate.
271271
*
272272
* @public
273273
*/
@@ -280,7 +280,7 @@ var Particles = (function(window, document) {
280280
};
281281

282282
/**
283-
* Pauses/stops animation
283+
* Pauses/stops the particle animation.
284284
*
285285
* @public
286286
*/
@@ -320,7 +320,6 @@ var Particles = (function(window, document) {
320320

321321
_.context.clearRect(0, 0, element.width, element.height);
322322
_.context.beginPath();
323-
_.context.fillStyle = 'rgb(' + _.options.color.r + ', ' + _.options.color.g + ', ' + _.options.color.b + ')';
324323

325324
for(var i = storage.length; i--;) {
326325
var particle = storage[i];
@@ -331,7 +330,6 @@ var Particles = (function(window, document) {
331330

332331
particle._updateCoordinates(parentWidth, parentHeight);
333332
}
334-
_.context.fill();
335333

336334
if (_.options.connectParticles) {
337335
storage.sort(particleCompareFunc);
@@ -340,21 +338,17 @@ var Particles = (function(window, document) {
340338
};
341339

342340
/**
343-
* Updates the edges
341+
* Updates the edges.
344342
*
345343
* @private
346344
*/
347345
Plugin.prototype._updateEdges = function() {
348346
var _ = this,
349347
minDistance = _.options.minDistance,
350-
color = _.options.color,
351348
sqrt = Math.sqrt,
352349
abs = Math.abs,
353-
354350
storage = _.storage,
355-
storageLength = storage.length,
356-
357-
strokeStyleColor = 'rgba(' + color.r + ',' + color.g + ',' + color.b + ',';
351+
storageLength = storage.length;
358352

359353
for(var i = 0; i < storageLength; i++) {
360354
var p1 = storage[i];
@@ -370,30 +364,37 @@ var Particles = (function(window, document) {
370364
}
371365

372366
if (distance <= minDistance) {
373-
_._drawEdge(p1, p2, strokeStyleColor + (1.2 - distance/minDistance) + ')');
367+
_._drawEdge(p1, p2, (1.2 - distance/minDistance));
374368
}
375369
}
376370
}
377371
};
378372

379373
/**
380-
* Draws an edge between two points
374+
* Draws an edge between two points.
381375
*
382376
* @private
383377
* @param {Particle} p1
384378
* @param {Particle} p2
385-
* @param strokeStyle
379+
* @param {number} opacity
386380
*/
387-
Plugin.prototype._drawEdge = function(p1, p2, strokeStyle) {
381+
Plugin.prototype._drawEdge = function(p1, p2, opacity) {
388382
var _ = this,
389-
context = _.context;
390-
391-
context.beginPath();
392-
context.strokeStyle = strokeStyle;
393-
context.moveTo(p1.x, p1.y);
394-
context.lineTo(p2.x, p2.y);
395-
context.stroke();
396-
context.closePath();
383+
gradient = _.context.createLinearGradient(p1.x, p1.y, p2.x, p2.y);
384+
385+
var color1 = this._hex2rgb(p1.color);
386+
var color2 = this._hex2rgb(p2.color);
387+
388+
gradient.addColorStop(0, 'rgba(' + color1.r + ',' + color1.g + ',' + color1.b + ',' + opacity);
389+
gradient.addColorStop(1, 'rgba(' + color2.r + ',' + color2.g + ',' + color2.b + ',' + opacity);
390+
391+
_.context.beginPath();
392+
_.context.strokeStyle = gradient;
393+
_.context.moveTo(p1.x, p1.y);
394+
_.context.lineTo(p2.x, p2.y);
395+
_.context.stroke();
396+
_.context.fill();
397+
_.context.closePath();
397398
};
398399

399400
/**
@@ -434,12 +435,12 @@ var Particles = (function(window, document) {
434435
* @constructor
435436
* @param {object} context
436437
* @param {object} options
437-
*
438438
*/
439439
Particle = function(context, options) {
440440
var _ = this,
441441
random = Math.random,
442-
speed = options.speed;
442+
speed = options.speed,
443+
color = (options.color instanceof Array) ? options.color[Math.floor(Math.random() * options.color.length)] : options.color;
443444

444445
_.context = context;
445446
_.options = options;
@@ -456,6 +457,7 @@ var Particles = (function(window, document) {
456457
_.vx = random() * speed * 2 - speed;
457458
_.vy = random() * speed * 2 - speed;
458459
_.radius = random() * random() * options.sizeVariations;
460+
_.color = color;
459461

460462
_._draw();
461463
};
@@ -471,12 +473,15 @@ var Particles = (function(window, document) {
471473
_.context.save();
472474
_.context.translate(_.x, _.y);
473475
_.context.moveTo(0, 0);
476+
_.context.beginPath();
474477
_.context.arc(0, 0, _.radius, 0, Math.PI * 2, false);
478+
_.context.fillStyle = _.color;
479+
_.context.fill();
475480
_.context.restore();
476481
};
477482

478483
/**
479-
* This updates the particles coordinates
484+
* This updates the particles coordinates.
480485
*
481486
* @private
482487
* @param parentWidth

dist/particles.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)