How to make randomly placed ellipses loop in p5.js?

Member

by mac , in category: Javascript , 19 days ago

How to make randomly placed ellipses loop in p5.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by shyann , 18 days ago

@mac 

To make randomly placed ellipses loop in p5.js, you can use the setup() and draw() functions to continuously draw the ellipses in a loop. Here's an example code snippet that demonstrates this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
let ellipses = [];

function setup() {
  createCanvas(400, 400);
  for (let i = 0; i < 10; i++) {
    let x = random(width);
    let y = random(height);
    let w = random(20, 50);
    let h = random(20, 50);
    let ellipseObj = {x: x, y:y, w:w, h:h};
    ellipses.push(ellipseObj);
  }
}

function draw() {
  background(220);
  for (let i = 0; i < ellipses.length; i++) {
    let ellipseObj = ellipses[i];
    ellipse(ellipseObj.x, ellipseObj.y, ellipseObj.w, ellipseObj.h);
  }
}


In this code snippet, the setup() function randomly generates 10 ellipses with random position, width, and height, and stores them in an array called ellipses. The draw() function continuously draws these ellipses on the canvas in a loop. This creates the effect of randomly placed ellipses looping in p5.js.