Skip to content

Fix cameraForBounds not accounting for asymmetrical padding in center result#13647

Open
ragini-pandey wants to merge 1 commit intomapbox:mainfrom
ragini-pandey:fix/camera-for-bounds-asymmetric-padding
Open

Fix cameraForBounds not accounting for asymmetrical padding in center result#13647
ragini-pandey wants to merge 1 commit intomapbox:mainfrom
ragini-pandey:fix/camera-for-bounds-asymmetric-padding

Conversation

@ragini-pandey
Copy link
Copy Markdown

Launch Checklist

  • Make sure the PR title is descriptive and preferably reflects the change from the user's perspective.
  • Add additional detail and context in the PR description (with screenshots/videos if there are visual changes).
  • Manually test the debug page.
  • 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-apis if this PR includes style spec API or visual changes.
  • Tag @mapbox/gl-native if this PR includes shader changes or needs a native port.
  • Tag @mapbox/gl-native if this PR disables any test because it also needs to be disabled on their side.
  • Create a ticket for gl-native to 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 zoom
    • symmetric padding does not shift center: verifies that equal padding on all sides does not shift the center (regression guard)

cameraForBounds was not accounting for asymmetrical padding when computing
the camera center. The _extendAABB method was averaging left/right and
top/bottom padding values, making the AABB extension symmetric. This meant
the AABB center never shifted to account for padding asymmetry, so the
resulting camera center was the same regardless of padding distribution.

The fix uses the actual padding values (left, right, top, bottom) directly
instead of averaging them. The available viewport width/height calculation
remains the same (left + right = padL + padR), so zoom is unaffected for
equal total padding. The AABB center now shifts to account for the
difference between left and right (or top and bottom) padding.

Fixes mapbox#13645
@ragini-pandey ragini-pandey requested a review from a team as a code owner March 25, 2026 20:07
@ragini-pandey ragini-pandey requested review from dcoj and removed request for a team March 25, 2026 20:07
@github-actions
Copy link
Copy Markdown

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:

  1. A team member will review your PR here first
  2. If it looks good, they will import it to our internal repository for further review
  3. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cameraForBounds is not accounting for an asymmetrical padding in the camera center result (regression in v3.3.0 to v3.4.0)

1 participant