ctx.clearRect(0, 0, canvas.width, canvas.height); if (increasing) { rectScale += 0.02; if (rectScale >= 2) { increasing = false; } } else { rectScale -= 0.02; if (rectScale <= 1) { increasing = true; } } const scaledWidth = rectWidth * rectScale; const scaledHeight = rectHeight * rectScale; rectX = (canvas.width - scaledWidth) / 2; rectY = (canvas.height - scaledHeight) / 2; ctx.fillStyle = 'blue'; ctx.fillRect(rectX, rectY, scaledWidth, scaledHeight); requestAnimationFrame(animate); }