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
})
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
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.