The clearRect call is using the unscaled rect.width and rect.height.
This means it’s clearing an area smaller than the actual canvas resolution. You would need to scale those dimensions by dpr inside the clearRect function.
Why:
After ctx.scale(dpr, dpr) is called, all subsequent drawing operations, including clearRect, are affected by the scaling. To clear the entire visible (scaled) area, clearRect needs to be called with the original logical dimensions, which are canvas.width / dpr and canvas.height / dpr, not the raw rect.width and rect.height which are already implicitly scaled by the dpr in the canvas.width/height assignment.