簡體   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