samedi 28 août 2021

Get random number of colors from a list instead of 8

I am experimenting on particles and I have set a list of 8 colors that create random shape. Now I have always the 8 colors showing up in the particles but I would like to have a random number of colors (1 to 8) everytime I run the script.

this is the relevant part of the code:

let colors = [
   "dodgerBlue",
  "red",
  "#fff200",
  "limeGreen",
  "slateGrey",
  "orange",
  "hotPink", 
  " #080808"
];
const dpi = 100;

let Particle = function(x, y) {
  this.x =  getRandomInt(cw);
  this.y =  getRandomInt(ch);
  this.coord = {x:x, y:y};
  this.r =  Math.min(getRandomInt(cw / dpi), 10 );
  this.vx = (Math.random() - 0.5) * 100;
  this.vy = (Math.random() - 0.5) * 100;
  this.accX = 0;
  this.accY = 0;
  this.friction = Math.random() * 0.07 + 0.90;
  this.color = colors[Math.floor(Math.random() * 8)];
}

how can I get a random number output instead of the "8" of the last line?




Aucun commentaire:

Enregistrer un commentaire