Fix DOMElement origin drift under camera zoom#7330
Open
moufmouf wants to merge 1 commit into
Open
Conversation
A scene-level DOMElement with a non-zero origin (the default is 0.5, 0.5) drifted away from its world position when the camera zoom was not 1, by `originX * width * (1 - zoom)` horizontally and the equivalent vertically. The same DOMElement nested in a Container did not drift. DOMElementCSSRenderer always bakes the origin offset into the CSS matrix via `camMatrix.translate(-dx, -dy)`. The parentMatrix branch correctly leaves `transform-origin` at 0% 0%, so the offset is applied once. The scene-level (else) branch additionally set `transform-origin: (100 * origin)%`, applying the offset a second time; the two only cancel when the camera zoom is 1. Make the scene-level path match the container path: bake the scale-aware offset into the matrix and keep `transform-origin` at 0% 0%. Scaled and rotated scene-level elements now stay correctly anchored at any zoom too. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR:
Fixes #7329.
Describe the changes
A scene-level
DOMElementwith a non-zero origin (the default is0.5, 0.5) driftedaway from its world position when
camera.zoom !== 1, byoriginX * width * (1 - zoom)horizontally and the equivalent vertically. The same
DOMElementnested in aContainerdid not drift — the two render paths were inconsistent.
Cause: in
DOMElementCSSRenderer,camMatrix.translate(-dx, -dy)bakes the originoffset into the CSS matrix in both branches. The
parentMatrixbranch correctly leavestransform-originat0% 0%(offset applied once). The scene-levelelsebranch alsoset
transform-origin: (100 * origin)%, applying the offset a second time. The two onlycancel when the camera-matrix scale is
1, so under camera zoom the residualoriginX * width * (1 - zoom)shows up as drift.Fix: make the scene-level path match the (correct) container path — keep
transform-originat0% 0%and bake the scale-aware offset into the matrix. ThescaleX/scaleYfactor theparentMatrixbranch already applied is correct for thescene-level branch too, so it's hoisted up rather than duplicated:
(
tx/tynow stay'0%', sotransform-originis0% 0%on both paths.)The container-nested element is unchanged (no drift before or after), and scaled / rotated
scene-level elements now also stay correctly anchored at any zoom because the baked offset
is scale-aware.
A minimal repro is available here and can be copy/pasted:
https://phaser.io/sandbox/f0eb3f05
Disclaimer: this PR was mostly written by Claude Code but manually checked to be working (so AI assisted but hopefully not AI slop :) )