Making a random art generator for assignment. We are supposed to have squares randomly pop up but I can't figure out to draw a square. This is what I have so far
function drawSquare(canvas, context, color){
var x= Math.floor(Math.random()*canvas.width);
var y= Math.floor(Math.random()*canvas.height);
context.beginPath();
context.fillStyle = color;
context.fillRect (x,y, canvas.width, canvas.height)
}
window.innerWidth*0.1
- which would turn out to be some amount of pixels (varying depending on the screen width). Yes, callMath.random()
to generate a new random number between > 0 and <= 1. You do not need to useMath.floor
by the way. – DomitianMath.random()
will evaluate to a number, so you could imagine that it is just a number (between 0 and 1). So in the case of your if statementif (Math.random() < 0.4) { ... } else { ... }
it will evaluate to some number like this:if (0.54462 < 0.4 ) { ...
which then gets evaluated to eithertrue
orfalse
. With that code it will evaluate to true about 40% of the time. – Domitian