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

259
Visualizações
p5.js : Image added multiple times while scaling down

I have the following p5.js code:

let minScale = 1
let maxScale = 5

let targetScale = minScale
let currentScale = targetScale

let idx = 0

function setup() {
  pixelDensity(1)
  createCanvas(windowWidth, windowHeight)
  preload(IMG_PATHS, IMGS)
  frameRate(12)  
}

function draw() { 
  if (currentScale < targetScale) {
    currentScale += 0.01
  } else if (currentScale > targetScale) {
    currentScale -= 0.01
  }
  
  scale(currentScale)
    
  image(IMGS[idx], 0, 0)
  
  idx++
  
  if (idx === IMGS.length) {
    idx = 0
  }  
}

window.addEventListener('click', function() {
  targetScale = targetScale === maxScale ? minScale : maxScale
})
  1. When I click I want to zoom-in.
  2. When I click again I want to zoom out

Issue

It looks like scale() redraws the canvas without removing the previous ones. The issue is visible when zooming out. You will notice that multiple copies of the image remain on the canvas while zooming out.

Thank you in advance for your help.

You can see the issue here

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

0

Drawing functions in p5.js generally do not clear the existing content. You can deliberately clear the entire canvas using clear() or overwrite the entire thing with background(color). You can also clear selective portions of the canvas with erase()/draw some shapes/noErase().

As for zooming in and out centered on a certain point, that is a more complicated question and you should ask it separately.

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