0
  1. Hello I was making a javascript net art and I needed to make that rgb stroke random can anyone help me i'm kinda new in javascript.

    if(this.bbox.collision(Player.bbox)){ strokeColor(255,0,0); }

2 Answers2

1

Use Math.random to get a random value from 0-255 for each of the three values.

let r = Math.floor(Math.random()*255);
let g = Math.floor(Math.random()*255);
let b = Math.floor(Math.random()*255);

Then you can call this by doing this:

strokeColor(r, g, b);

SatvikVejendla
  • 424
  • 5
  • 14
0
<div id="changer">Contents Here</div>
<br>
<button onclick="setRandomRgbColor()">Change Color</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
function getRandomRgbColor() {
   var color = 'rgb(';    
   color += Math.floor(Math.random() * 256) + ',';
   color += Math.floor(Math.random() * 256) + ',';
   color += Math.floor(Math.random() * 256) + ')';
   return color;
}


function setRandomRgbColor() {
   //$("#changer").css("border", ' 2px solid '+ getRandomRgbColor());
   //or  
   document.getElementById("changer").style.border = "2px solid "+getRandomRgbColor();
}
Kallol Ray
  • 79
  • 1
  • 5