我当前用于随机化rgb的代码已经放在了变量cr(颜色随机)中。
var cr = 'rgb('+
Math.floor(Math.random()*256)+','+
Math.floor(Math.random()*256)+','+
Math.floor(Math.random()*256)+')';
我把变量放在fillstyle画布元素中以实现颜色填充:
ctx.fillStyle = cr;
接下来的fillRect按照预期进行了随机处理。当前代码:
var c = document.getElementById('canvas');
var ctx = c.getContext('2d');
var cr = 'rgb('+
Math.floor(Math.random()*256)+','+
Math.floor(Math.random()*256)+','+
Math.floor(Math.random()*256)+')';
ctx.fillStyle = cr;
ctx.fillRect(0, 210 , 100 ,290);
问题出现在我尝试将它放入新的fillRect()中时。例如:
var c = document.getElementById('canvas');
var ctx = c.getContext('2d');
var cr = 'rgb('+
Math.floor(Math.random()*256)+','+
Math.floor(Math.random()*256)+','+
Math.floor(Math.random()*256)+')';
ctx.fillStyle = cr;
ctx.fillRect(0, 210 , 100 ,290);
ctx.fillStyle = cr;
ctx.fillRect(100, 210, 100, 290);
新的ctx.fillStyle使用相同的随机颜色。我希望新的fillRect有一个新的随机颜色(做一个酷炫的天际线效果,所以可能需要再做20个或更多)。也许,我需要将循环放入数组中,对其进行随机化,并在每个新的fillRect中使用。任何解决方案都将不胜感激 :)