I need to color randomly a figure

1

For the figure to change color every time it is generated again, it is suggested to create a "var color" arrangement with at least 5 colors. Then create a variable that allows you to generate a random number between 0 and the length of the array minus 1. Also, for the figure with fill to change color, instead of using " ctx.strokeStyle " you must use " ctx.fillStyle ". (JavaScript)

  var color = ["Red", "Blue", "Orange", "Yellow", "Green"];
      var randcolor = color[Math.floor(Math.random() * color.length)-1];
ctx.fillStyle = "";

I need if someone can check the syntax, and if you can tell me how I can do so that ctx.fillStyle use randcolor. Thanks

    
asked by M.Martin 01.11.2017 в 19:11
source

1 answer

0

I think that in the random you have the fault

// Mi array de colores
var color = ["Red", "Blue", "Orange", "Yellow", "Green"];
// Numero al azar para el color (saca de 0 a n-1)
// Lo que se necesito para el array !!
NumAleatorio = Math.floor((Math.random() * color.length));
// Recojo el Color del Array
ColorAleatorio = color[NumAleatorio];
ctx.fillStyle = ColorAleatorio;

// Mi array de colores
var color = ["Red", "Blue", "Orange", "Yellow", "Green"];
// Numero al azar para el color (saca de 0 a n-1)
// Lo que se necesito para el array !!
NumAleatorio = Math.floor((Math.random() * color.length));
// Recojo el Color del Array
ColorAleatorio = color[NumAleatorio];
var c = document.getElementById("MiCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = ColorAleatorio;
ctx.fillRect(20, 20, 150, 100);
<canvas id="MiCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
    </canvas>
    
answered by 01.11.2017 / 19:53
source