Skip to content

Commit c39f2d9

Browse files
committed
Add array selection for color_random
Added ability for Particles.js to accept an array as a value for color_random. If the value is an array it will randomly select an element from the array for the fillStyle. The values must be HEX values at the moment.
1 parent 80239f6 commit c39f2d9

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

particles.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,17 @@ function launchParticlesJS(tag_id, params){
263263
if (pJS.retina) this.radius *= pJS.canvas.pxratio;
264264

265265
/* color */
266-
if(pJS.particles.color_random){
266+
if(pJS.particles.color_random === true){
267267
this.color = {
268268
r: (Math.floor(Math.random() * (255 - 0 + 1)) + 0),
269269
g: (Math.floor(Math.random() * (255 - 0 + 1)) + 0),
270270
b: (Math.floor(Math.random() * (255 - 0 + 1)) + 0)
271271
}
272-
}else{
272+
}
273+
else if( pJS.particles.color_random instanceof Array){
274+
this.color = pJS.particles.color_random[Math.floor(Math.random() * pJS.particles.color_random.length)];
275+
}
276+
else{
273277
this.color = color;
274278
}
275279

@@ -291,7 +295,7 @@ function launchParticlesJS(tag_id, params){
291295

292296
pJS.fn.particle.prototype.draw = function() {
293297

294-
pJS.canvas.ctx.fillStyle = 'rgba('+this.color.r+','+this.color.g+','+this.color.b+','+this.opacity+')';
298+
pJS.canvas.ctx.fillStyle = this.color.toString().charAt(0) === '#' ? this.color : 'rgba('+this.color.r+','+this.color.g+','+this.color.b+','+this.opacity+')';
295299
pJS.canvas.ctx.beginPath();
296300

297301
switch(pJS.particles.shape){
@@ -658,4 +662,4 @@ window.particlesJS = function(tag_id, params){
658662
launchParticlesJS(tag_id, params);
659663
}
660664

661-
};
665+
};

0 commit comments

Comments
 (0)