0

I am a beginner and am working on learning generative art and creative coding. this code aims at generating lines randomly and the number of iterations is random as well.

https://editor.p5js.org/rawrro/sketches/j4V6zpnMr

code:

function setup() {
  createCanvas(windowWidth-20, windowHeight-20);
  
    for (let i = random(100); i>0; i--);
   
    {
      
line(random(0,600),random(0,600),random(0,600),random(0,600))
 
    }   
}

function draw() {
  background(GRAY);
}
JMP
  • 4,417
  • 17
  • 30
  • 41
rawrro
  • 1

1 Answers1

1

First, there is a problem of syntax in the for loop, There must not be a semicolon after for(). Correct syntax:

for (let i = random(100); i>0; i--) {
    line(random(0,600),random(0,600),random(0,600),random(0,600));
}

Second, the setup() function is executed once at the start, the draw() function is executed 60 times per second. Here, you erase the lines by defining a background color in draw(). If you want the lines to be generated each frame, the for loop should be in draw() (but the animation will be too fast!)

Third, but it's not important, the random function doesn't need two arguments: random(600) will generate a random value between 0 and 600.

ManuBou
  • 46
  • 4