简体   繁体   English

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

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

The randomColorValue function in fillStyle is not being used. 未使用fillStyle中的randomColorValue函数。 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() . 变化Math.randomMath.random() When fillStyle has non RGB-value as the input, it will turn black instead and Math.random is a function. 当fillStyle具有非RGB值作为输入时,它将变为黑色,而Math.random是一个函数。

Tested, it works jsfiddle 经测试,它的工作原理的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. randomColorValue返回NaN ,因为Math.random是一个函数,您无需调用它。 Your randomColorValue should look like this: 您的randomColorValue应该看起来像这样:

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

and it will work :) 它会工作:)

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

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