fix(image): account for destination pixel density in p5.Image copy and blend - #9168
Pcmhacker-piro wants to merge 4 commits into
Conversation
|
Hi @ksen0, could you please take a look at this PR when you have a moment? It fixes a high-DPI scaling issue where |
There was a problem hiding this comment.
I suspect this change might also affect mask(). It already passes imgScaleFactor * this.width and imgScaleFactor * this.height to copy(). Since _copyHelper() now scales the destination dimensions again, could this cause double scaling when the image’s pixel density is greater than 1?
mask() was pre-multiplying destination coords by imgScaleFactor before passing them to copy(). Since _copyHelper() now scales destination coords by the destination pixel density internally, this caused double-scaling when pixelDensity > 1. Fix: pass logical (unscaled) this.width / this.height from mask() and let _copyHelper() handle the scaling. Added unit test verifying mask() works correctly at pixelDensity 2.
|
Great catch @perminder-17! You were right — Fix: Updated Added: A unit test for All tests pass (8 passed, 7 todo). Lint is clean (0 errors). |
Resolves #9169
Overview
Fixes a high-DPI scaling bug where calling
copy()andblend()on ap5.ImagewithpixelDensity > 1(e.g. on Retina or high-DPI displays) failed to scale the destination coordinates (dx, dy, dw, dh) to the physical dimensions of the backing canvas.Cause of the Bug
In
p5.Image.prototype._copyHelper:s = srcImage.canvas.width / srcImage.width.dstImage.drawingContextis a 2D canvas context without an automatic DPI scale transform.drawImage()was called with unscaled logical destination coordinatesdx, dy, dw, dh.pixelDensity = 2), the copied image covered only 1/4th of the intended destination area in the top-left corner, leaving the rest untouched.p5.Image.prototype.blend()internally delegates tocopy(), it was similarly affected.Changes:
src/image/p5.Image.js, computed destination pixel density scale factorconst d = dstImage.canvas.width / dstImage.width.d * dx, d * dy, d * dw, d * dhintodstImage.drawingContext.drawImage().test/unit/image/p5.Image.jsforp5.Image.prototype.copyandp5.Image.prototype.blendverifying correct high-DPI destination rendering.PR Checklist
npm run lintpasses