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

257
Views
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 answers
Answer question

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 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!