简体   繁体   English

我需要在html画布中堆叠多个随机颜色的矩形

[英]I need to stack multiple randomly colored rectangles in html canvas

I want to have hella stacked rectangles that go to the center of the canvas and have them all randomly colored, but my code is wrong and isn't working. 我想让hella堆叠的矩形到达画布的中心并让它们全部随机着色,但是我的代码是错误的并且无法正常工作。

Here is the code i have: 这是我的代码:

var ctx = canvas.getContext("2d");
var cornerpad = 0;
var rectwidth = 790;
var rectheight = 590;

function drawrectangles() {
    ctx.beginPath;
    ctx.rect(cornerpad, cornerpad, rectwidth, rectheight);
    ctx.fillStyle = 'hsl(' + 360 * Math.random() + ', 50%, 50%)'
    ctx.fill();
    ctx.closePath;
}   

while(rectheight > 5) {
    rectheight =- 10;
    rectwidth =- 10;
    cornerpad++;
    drawrectangles();
}

I am pretty sure I made the while loop wrong since the random color works and the drawing rectangles works. 我很确定我使while循环错误,因为随机颜色工作和绘图矩形工作。 Please help and explain what I did wrong 请帮助解释我做错了什么

You had some mistakes in your code. 你的代码中有一些错误。 First of all, you have rectheight =- 10; 首先,你有rectheight =- 10; instead of rectheight -= 10 . 而不是rectheight -= 10 Your line of code is actually equivalent to rectheight = -10 , so you are just setting both of the variables to -10, not decrementing them by 10. 你的代码行实际上等于rectheight = -10 ,所以你只是将两个变量都设置为-10,而不是将它们减少10。

Then, you used fill instead of fillRect . 然后,您使用fill而不是fillRect There is a fine difference between them, you may read more here or here . 他们之间有一个很好的区别,你可以在这里这里阅读更多。 fillRect is a "stand-alone" command that draws and fills a rectangle. fillRect是一个“独立”命令,用于绘制和填充矩形。 So if you issue multiple fillRect commands with multiple fillStyle commands, each new rect will be filled with the preceeding fillstyle. 因此,如果您使用多个fillStyle命令发出多个fillRect命令,则每个新的rect将使用前面的fillstyle填充。

For a nicer effect, I recommend using strokeRect instead of rect , but that's your decision. 为了获得更好的效果,我建议使用strokeRect而不是rect ,但这是您的决定。 Also, you may want to play with the condition from the while, to actually make them appear centered. 此外,您可能希望从while中播放条件,以实际使它们显示为居中。

 var canvas = document.getElementById('canvas'); var ctx = canvas.getContext("2d"); var cornerpad = 0; var rectwidth = 790; var rectheight = 590; function drawrectangles() { ctx.beginPath(); ctx.rect(cornerpad, cornerpad, rectwidth, rectheight); ctx.fillStyle = 'hsl(' + 360 * Math.random() + ', 50%, 50%)'; ctx.fill(); ctx.stroke(); } while (rectheight > 5) { rectheight -= 10; rectwidth -= 10; cornerpad += 10; //console.log(`(h, w)=(${rectheight}, ${rectwidth})`); drawrectangles(); } 
 canvas { border: 1px dotted black; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/canvasjs/1.7.0/canvasjs.js"></script> <canvas id="canvas" width="790" height="590"> This text is displayed if your browser does not support HTML5 Canvas. </canvas> 

Edit: I added a version that draws them much nicer, check it out :) 编辑:我添加了一个更好的绘制版本,请查看:)

 var canvas = document.getElementById('canvas'); var ctx = canvas.getContext("2d"); var cornerpad = 0; var rectwidth = 600; var rectheight = 400; var middleLine = rectheight / 2; function drawrectangles() { ctx.strokeRect(cornerpad, cornerpad, rectwidth, rectheight); ctx.fillStyle = 'hsl(' + 360 * Math.random() + ', 50%, 50%)' ctx.fillRect(cornerpad, cornerpad, rectwidth, rectheight); } while (cornerpad < middleLine) { rectheight -= 20; rectwidth -= 20; cornerpad += 10; //console.log(`(h, w)=(${rectheight}, ${rectwidth})`); drawrectangles(); } 
 canvas { border: 1px dotted black; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/canvasjs/1.7.0/canvasjs.js"></script> <canvas id="canvas" width="600" height="400"> This text is displayed if your browser does not support HTML5 Canvas. </canvas> 

Cheers! 干杯!

Edit 2: Because I was too absorbed by the =- 10 thing, I forgot to point out to actually use beginPath and closePath as functions and to call them as ctx.beginPath() and ctx.closePath() . 编辑2:因为我太beginPath=- 10事情,我忘了指出实际使用beginPathclosePath作为函数并将它们称为ctx.beginPath()ctx.closePath() Thanks to Kaiido for pointing that out and for the two working additional jsfiddles. 感谢Kaiido指出了这两个工作的额外jsfiddles。

I believe your problem is laying in this expressions =- what you mean is -= . 我相信你的问题在于这个表达式=-你的意思是-= what you are doing now is setting rectheight value to -10 instead of reducing it by 10, thus your while loop just gets executed once. 你现在正在做的是将rectheight值设置为-10而不是将其减少10,因此你的while循环只执行一次。

You missed some closing brackets and had the =- around the wrong way. 你错过了一些关闭括号,并且错误地使用了= - 。 I also change the rect to fill straight away. 我也改变了矩形直接填补。

 var ctx = canvas.getContext("2d"); var cornerpad = 0; var rectwidth = 790; var rectheight = 590; function drawrectangles() { ctx.fillStyle = 'hsl(' + 360 * Math.random() + ', 50%, 50%)' ctx.fillRect(cornerpad, cornerpad, rectwidth, rectheight); } while(rectheight > 5) { rectheight -= 10; rectwidth -= 10; cornerpad++; drawrectangles(); } 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <canvas id='canvas' height=590 width=790></canvas> </body> </html> 

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

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