繁体   English   中英

RGB填充样式中未使用Javascript函数

[英]Javascript Function Not Being Used in RGB fill Style

未使用fillStyle中的randomColorValue函数。 画布只是被涂成全黑,而不是随机的配色方案。 我是一个初学者,希望这不是一个完整的菜鸟问题,也不是显而易见的问题。 谢谢你的帮助。 我将最相关的代码用斜体表示。

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; 
}

变化Math.randomMath.random() 当fillStyle具有非RGB值作为输入时,它将变为黑色,而Math.random是一个函数。

经测试,它的工作原理的jsfiddle

它确实有,但是却无声地失败了。 randomColorValue返回NaN ,因为Math.random是一个函数,您无需调用它。 您的randomColorValue应该看起来像这样:

 function randomColorValue() { let actualValue = Math.ceil(Math.random() * 255); return actualValue; } 

它会工作:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM