fitBound end up in error of NaN after call
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 12.4k
- Forks
- 2.4k
- PR merge metrics
- No merged PRs in 30d
Description
mapbox-gl-js version: 1.13.1
browser: Tested in Chrome and Firefox
Steps to Trigger Behavior
- Set a map element with a size
x(example: 400px) - Call
fitBoundsmethod passing CameraOptions with padding beingx / 2(example: 200px)
Expected Behavior
Map should fit to provided boundaries
Actual Behavior
At this moment when it happens to map element width to be x and the padding (right and left) to be x / 2 we end up with the Point object generated in _cameraForBoxAndBearing to be a set of NaN of x and y, this happens due to the fact we multiply 0 with Infinity and the method just throw an error.
-
https://github.com/mapbox/mapbox-gl-js/blob/main/src/ui/camera.js#L579
Here we havescaleXbeing assigned to 0 astr.widthbeingx(e.g: 400px) andedgePadding.left or .rightbeing 0 andeOptions.padding.left or rightbeingx / 2(e.g: 200px) -
https://github.com/mapbox/mapbox-gl-js/blob/main/src/ui/camera.js#L588
zoomis assigned to-Infinityas intr. scaleZoomit doesMath.log(scale) / Math.LN2and log of 0 if-Infinity -
https://github.com/mapbox/mapbox-gl-js/blob/main/src/ui/camera.js#L597
offsetAtFinalZoomis assigned to{ x: NaN, y: NaN }as we havetr.zoomScale(zoom)returning 0 as result toMath.pow(2, -Infinity)and we gety/0which turns intoInfinityand finally we get.multreturningNaNfrom0 * Infinity

Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/ui/camera.js at _cameraForBoxAndBearing, following the fitBounds path and the scale, zoom, and offsetAtFinalZoom calculations described in the report. Reproduce the case with a 400px map and 200px left and right padding; done means fitBounds no longer produces NaN coordinates or throws when the available width is zero.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100