Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

604
Visualizações
How to move an object back and forth in p5js javascript

After a few hours of work and research (I just started learning p5js and javascript) I have about 50 lines of code that creates a grid of circles and begins to move them across the canvas. Before I get into my issue, I will share the code.

var circles = [];

function setup() {
  createCanvas(500, 500);
  for (var a = 50; a < width - 300; a += 20) {
    for (var b = 50; b < height - 300; b += 20) {
      circles.push(new Circle(a, b));
    }
  }
}

function draw() {
  background(50);
  for (var b = 0; b < circles.length; b++) {
    circles[b].show();
  }
}

function Circle(x, y) {
  this.x = x;
  this.y = y;

  this.show = function() {
    fill(255);
    noStroke();
    ellipse(this.x, this.y, 10, 10);
    if (this.x < 51) {
      this.x += 1
      this.y += 1
    } 
    if (this.x < 430) {
      this.x += 1
      this.y += 1
    }
    if (this.x > 430) {
      this.x -= 1
      this.y -= 1
    }
    if (this.x < 51) {
      this.x += 1
      this.y += 1
    } 
  }
}

What I would like to do is move this grid of circles starting at (50,50) to the bottom right corner. Once it hits (width-50,height-50) I'd like the movement to reverse back to the starting point, and then back the other way. This code moves the circles to the bottom right corner successfully, them something goes wrong. The circles don't reverse their movement, rather, they get messed up. I thought the if statements would handle this but I must be missing something. I trouble shooted for about an hour and now I thought I'd ask SO. Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Add a moving direction to the object. Change the direction when the object goes out of bounds:

var circles = [];

function setup() {
    createCanvas(500, 500);
    for (var a = 50; a < width - 300; a += 20) {
        for (var b = 50; b < height - 300; b += 20) {
            circles.push(new Circle(a, b));
        }
    }
}

function draw() {
    background(50);
    for (var b = 0; b < circles.length; b++) {
        circles[b].show();
    }
}
    
function Circle(x, y) {
    this.x = x;
    this.y = y;
    this.dx = 1;
    this.dy = 1

    this.show = function() {
        fill(255);
        noStroke();
        ellipse(this.x, this.y, 10, 10);

        this.x += this.dx
        this.y += this.dy

        if (this.x < 51 || this.y < 51) {
            this.dx = 1
            this.dy = 1
        } 
        if (this.x > 430 || this.y > 430) {
            this.dx = -1
            this.dy = -1
        }
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.min.js"></script>

If you do not want to move the objects individually, you must use 1 direction of movement for all objects:

var circles = [];

function setup() {
    createCanvas(500, 500);
    for (var a = 50; a < width - 300; a += 20) {
        for (var b = 50; b < height - 300; b += 20) {
            circles.push(new Circle(a, b));
        }
    }
}

var dx = 1
var dy = 1
function draw() {
    background(50);
    mx = dx
    my = dy
    for (var b = 0; b < circles.length; b++) {
        circles[b].show(mx, my);
    }
}
    
function Circle(x, y) {
    this.x = x;
    this.y = y;

    this.show = function(mx, my) {
        fill(255);
        noStroke();
        ellipse(this.x, this.y, 10, 10);

        this.x += mx
        this.y += my

        if (this.x < 51) {
            dx = 1
            dy = 1
        } 
        if (this.x > 430) {
            dx = -1
            dy = -1
        }
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.min.js"></script>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda