samedi 2 décembre 2017

HTML5 Canvas - Why the color of the rectangle always returns black?

I want to draw a canvas rectangle that changes color every load. Here is my code:

window.onload = function() {
var can = 
document.getElementById("lol");
var ctx = can.getContext("2d");
var colors = ["rgba(255,0,0,1)", "rgba(0,255,0,1)", "rgba(0,0,255,1)"];

ctx.fillStyle = colors[Math.random() * 3];
ctx.fillRect(40,40,30,25);
}

Yet, every time when i open a web page, The color should changed to either red, blue or green but the color is persistently black.

Why is this happens? What is wrong in my code?




Aucun commentaire:

Enregistrer un commentaire