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

85
Visualizações
How to push one object infront of another p5js javascript

I have been stuck on this for hours and am unsure on what to do.

Here is my code:

var circles = [];
var circles2 = [];

function setup() {
  createCanvas(500, 500);
  //there's a "b" for every "a"
  for (var a = 50; a < width - 300; a += 20) {
    for (var b = 50; b < height - 300; b += 20) {
      //add the circles to the array at x = a and y = b
      circles.push(new Circle(a, b));
      circles2.push(new Conn(a, b));
    }
  }
  for (var a = 250; a < width - 50; a += 20) {
    for (var b = 250; b < height - 50; b += 20) {
      //add the circles to the array at x = a and y = b

    }
  }
  console.log(circles.length);
}

function draw() {
  background(50);
  for (var b = 0; b < circles.length; b++) {
    circles2[b].show();
    circles[b].show();
    //console.log("shown");
  }
  for (var b = 0; b < circles2.length; b++) {
    //circles2[b].show();
    //console.log("shown");
  }
}

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

  this.show = function() {
    fill(255);
    noStroke();
    ellipse(this.x, this.y, 10, 10);
    //console.log("showing");
    push();
    fill(255);
    noStroke();
    translate(250, 250);
    ellipse(this.x, this.y, 10, 10);
    pop();
  }
}

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

  this.show = function() {
    noStroke();
    let h = 0;
    for (let i = 0; i < 251; i++) {
      fill(h, 90, 120);
      h = (h + 1) % 500;
      noStroke();
      push()
      ellipse(this.x + i, this.y + i, 10, 10);
      noStroke();
      noLoop();
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>

It generates a grid of squares that is supposed to connect to a different grid of squares through gradient lines. However, only the bottom row and right most column of circles on the bottom right circle grid show up for some reason. How can I make it that all circles on the bottom right circle grid are in front of the gradient lines? I've tried push, pop, and everything else. I'm really confused as to why only some of the circles are showing up, but the others are stuck behind these gradients.

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

0

Unless you use p5.js in WEBGL mode then there is no object stacking order or z-index or z-buffer. Shapes that you draw always overlap whatever was drawn before them. So in order to make a shape appear on top of another you need to draw the shape underneath first, followed by the shape on top. I believe I was able to achieve this by rearranging your code a little:

var circles = [];
var connections = [];

function setup() {
  createCanvas(500, 500);
  //there's a "b" for every "a"
  for (var a = 50; a < width - 300; a += 20) {
    for (var b = 50; b < height - 300; b += 20) {
      //add the circles to the array at x = a and y = b
      circles.push(new Circle(a, b));
      connections.push(new Conn(a, b));
    }
  }
  for (var a = 250; a < width - 50; a += 20) {
    for (var b = 250; b < height - 50; b += 20) {
      //add the circles to the array at x = a and y = b

    }
  }
  console.log(circles.length);
}

function draw() {
  background(50);
  // Show connections first
  for (var b = 0; b < connections.length; b++) {
    connections[b].show();
  }
  
  // Show circles second so that they appear on top of the connections
  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);
    //console.log("showing");
    push();
    fill(255);
    noStroke();
    translate(250, 250);
    ellipse(this.x, this.y, 10, 10);
    pop();
  }
}

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

  this.show = function() {
    noStroke();
    let h = 0;
    for (let i = 0; i < 251; i++) {
      fill(h, 90, 120);
      h = (h + 1) % 500;
      noStroke();
      push()
      ellipse(this.x + i, this.y + i, 10, 10);
      noStroke();
      noLoop();
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.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