Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda