Mi objetivo es que cada cuadro obtenga un color diferente desde una lista de valores HEX en un array. No lo he logrado aún, he llegado hasta que cada vez que se actualice la página, se generan nuevos colores, pero seguiré buscando la manera correcta. Sugerencias? Gracias!
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Canvas JS</title>
</head>
<body>
<canvas width="800" height="600"> </canvas>
<script>
// obtener color random ------->
const colorList = [
"#ff48c4",
"#2bd1fc",
"#f3ea5f",
"#c04df9",
"#ff3f3",
"yellow",
"magenta",
"tomato",
];
var randomColor1 = colorList[Math.floor(Math.random() * colorList.length)];
var randomColor2 = colorList[Math.floor(Math.random() * colorList.length)];
var randomColor3 = colorList[Math.floor(Math.random() * colorList.length)];
// funcion dibujar cuadro
function drawSquare(x, y, randomColor, lineWidth, borderColor) {
var screen = document.querySelector("canvas");
var brush = screen.getContext("2d");
// grosor de línea borde
brush.lineWidth = lineWidth;
// color y tamaño de borde y relleno
brush.fillStyle = randomColor;
brush.fillRect(x, y, 50, 50);
brush.strokeStyle = borderColor;
brush.strokeRect(x, y, 50, 50);
}
var x = 0;
while(x < 800) {
drawSquare(x, 20, randomColor1, 2, "white");
drawSquare(x, 70, randomColor2, 2, "white");
drawSquare(x, 120, randomColor3, 2, "white");
x = x + 50;
};
</script>
</body>
</html>