Fix cameraForBounds not accounting for asymmetrical padding in center result
Launch Checklist
- [x] Make sure the PR title is descriptive and preferably reflects the change from the user's perspective.
- [x] Add additional detail and context in the PR description (with screenshots/videos if there are visual changes).
- [x] Manually test the debug page.
- [x] Write tests for all new functionality and make sure the CI checks pass.
- [ ] Document any changes to public APIs.
- [ ] Post benchmark scores if the change could affect performance.
- [ ] Tag
@mapbox/map-design-team@mapbox/static-apisif this PR includes style spec API or visual changes. - [ ] Tag
@mapbox/gl-nativeif this PR includes shader changes or needs a native port. - [ ] Tag
@mapbox/gl-nativeif this PR disables any test because it also needs to be disabled on their side. - [ ] Create a ticket for
gl-nativeto groom in the MAPSNAT JIRA queue if this PR includes shader changes or features not present in the native side or if it disables a test that's not disabled there.
Description
Fixes #13645
cameraForBounds was not accounting for asymmetrical padding when computing the camera center. This was a regression introduced in v3.4.0.
Root Cause
In the _extendAABB method in src/ui/camera.ts, the padding values were being averaged (symmetrized) before being applied to the AABB extension:
// Before (broken):
const halfScreenPadX = (padL + padR) * 0.5;
const halfScreenPadY = (padT + padB) * 0.5;
const top = halfScreenPadY;
const left = halfScreenPadX;
const right = halfScreenPadX;
const bottom = halfScreenPadY;
This meant both sides of the bounding box received equal padding extension, so the AABB center (which determines the resulting camera center) never shifted regardless of padding asymmetry. The zoom was correctly affected because the total padding was preserved, but the center was not.
Fix
Use the actual asymmetric padding values directly:
// After (fixed):
const top = padT;
const left = padL;
const right = padR;
const bottom = padB;
The available viewport width = tr.width - (left + right) and height = tr.height - (top + bottom) calculations remain identical since left + right = padL + padR in both cases. So the zoom calculation is unaffected. The difference is that the AABB now extends asymmetrically, shifting its center to properly account for the padding difference.
Testing
- Updated 7 existing test expectations that incorrectly expected no center shift with asymmetric padding
- Added 2 new tests:
asymmetrical left padding shifts center: verifies that left-only padding shifts the camera center and reduces zoomsymmetric padding does not shift center: verifies that equal padding on all sides does not shift the center (regression guard)
Hey, @ragini-pandey 👋 Thanks for your contribution to Mapbox GL JS!
Important: This repository does not accept direct merges. All changes go through our internal review process.
What happens next:
- A team member will review your PR here first
- If it looks good, they will import it to our internal repository for further review
- If approved, changes will be synced back here via our release process
Please respond to any review comments on this PR. For more details, see CONTRIBUTING.md.