Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
how can i rewrite this code to make an object fall from top of canvas and when hits bottom it resets at top

I am trying to implement a group of objects that will fall from the top of the canvas in random spots, and then when they hit the bottom of the canvas they respawn at the top of the canvas. Ideally, I want to change this code in the future to use sprites that will fall from the top of the canvas and explode when they hit the bottom of the canvas but then respawn again at the top.

I have the bellow code that works when the mouse is pressed but I would like to rewrite this code so the event happens automatically without the mouse needed to be pressed to make the objects fall.

see code below.

var x, y;
var particle = [];

function setup() {
  createCanvas(720, 400);
  // Starts in the middle
  x = width / 2;
  y = height;
}

function draw() {
  background(0);
  if (mouseIsPressed){
  stroke(50);
  fill(100);
  ellipse(x, y, 24, 24);
  }
  x = x + 0;
  // Moving up at a constant speed
  y = y + 2;
  
  // Reset to the bottom
  if (y >= height) {
    y = 0;
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use the mousePressed() to add new particles to the list. The particles are added at the current mouse position (mouseX, mouseY). Move and draw the particles in a loop:

var particles = [];

function setup() {
    createCanvas(720, 400);
}

function mousePressed() {
    particles.push([mouseX, mouseY]);
}

function draw() {
    background(0);
    stroke(50);
    for (let i=0; i < particles.length; i++) {
        particles[i][1] += 2;
        if (particles[i][1] > height) {
            particles[i][1] = 0;
        }
        fill(100);
        ellipse(particles[i][0], particles[i][1], 24, 24);
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>

Alternatively, you can spawn the particles with a time interval. Get the number of milliseconds (thousandths of a second) since starting the sketch with millis(). The y-coordinate of a new particle is 0 and the x-coordinate is random():

var particles = [];

function setup() {
    createCanvas(720, 400);
}

function mousePressed() {
    particles.push([mouseX, mouseY]);
}

function draw() {
    let max_no_of_particles = 40;
    let expected_no_of_praticles = min(max_no_of_particles, millis() / 100);
    if (particles.length < expected_no_of_praticles) {
        particles.push([random(12, width-12), 0]);
    }

    background(0);
    stroke(50);
    for (let i=0; i < particles.length; i++) {
        particles[i][1] += 2;
        if (particles[i][1] > height) {
            particles[i][1] = 0;
        }
        fill(100);
        ellipse(particles[i][0], particles[i][1], 24, 24);
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!