The randomColorValue function in fillStyle is not being used. The canvas is just being painted all black instead of the random color scheme. I am a bit of a beginner and hope this isn't a complete noob question or something just plain obvious. Thanks for any help. I italicized the most relevant code.
const canvas = document.getElementById('squares');
const ctx = canvas.getContext('2d');
for (let x = 0; x <=800; x+=50){
for(let y= 0; y<=800; y+=50){
ctx.fillStyle = `rgb(${randomColorValue()},${randomColorValue()},${randomColorValue()})`;
ctx.fillRect(x, y, 50, 50);
}
}
function randomColorValue(){
let actualValue = Math.ceil (Math.random*255);
return actualValue;
}
Change Math.random
to Math.random()
. When fillStyle has non RGB-value as the input, it will turn black instead and Math.random
is a function.
Tested, it works jsfiddle
It actually does, but it's failing silently. The randomColorValue
returns a NaN
, because Math.random
is a function, and you don't call it. Your randomColorValue
should look like this:
function randomColorValue() { let actualValue = Math.ceil(Math.random() * 255); return actualValue; }
and it will work :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.