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
0 commit comments