Skip to content

Commit 759da9d

Browse files
author
Molly Lloyd
committed
update padding approach, add error warnings 🙅
1 parent 110a9c6 commit 759da9d

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/ui/camera.js

+28-4
Original file line numberDiff line numberDiff line change
@@ -342,20 +342,44 @@ class Camera extends Evented {
342342
};
343343
}
344344

345+
if (!util.deepEqual(Object.keys(options.padding).sort((a, b)=> {
346+
if (a < b) return -1;
347+
if (a > b) return 1;
348+
return 0;
349+
}), ["bottom", "left", "right", "top"])) {
350+
util.warnOnce("options.padding must be a positive number, or an Object with keys 'bottom', 'left', 'right', 'top'");
351+
console.log(Object.keys(options.padding).sort((a, b)=> {
352+
if (a < b) return -1;
353+
if (a > b) return 1;
354+
return 0;
355+
}));
356+
return;
357+
358+
}
359+
345360
bounds = LngLatBounds.convert(bounds);
346361

347-
const offset = Point.convert(options.offset),
362+
// we separate the passed padding option into two parts, the part that does not affect the map's center
363+
// (lateral and vertical padding), and the part that does (paddingOffset). We add the padding offset
364+
// to the options `offset` object where it can alter the map's center in the subsequent calls to
365+
// `easeTo` and `flyTo`.
366+
const paddingOffset = [options.padding.left - options.padding.right, options.padding.top - options.padding.bottom],
367+
lateralPadding = Math.min(options.padding.right, options.padding.left),
368+
verticalPadding = Math.min(options.padding.top, options.padding.bottom);
369+
options.offset = [options.offset[0] + paddingOffset[0], options.offset[1] + paddingOffset[1]];
348370

349-
paddingOffset = [Math.min(options.padding.right, options.padding.left), Math.min(options.padding.top, options.padding.bottom)],
350-
verticalPadding = Math.abs(options.padding.top - options.padding.bottom),
351-
lateralPadding = Math.abs(options.padding.right - options.padding.left),
371+
const offset = Point.convert(options.offset),
352372
tr = this.transform,
353373
nw = tr.project(bounds.getNorthWest()),
354374
se = tr.project(bounds.getSouthEast()),
355375
size = se.sub(nw),
356376
scaleX = (tr.width - lateralPadding * 2 - Math.abs(offset.x) * 2) / size.x,
357377
scaleY = (tr.height - verticalPadding * 2 - Math.abs(offset.y) * 2) / size.y;
358378

379+
if (scaleY < 0 || scaleX < 0) {
380+
util.warnOnce('Map cannot fit within canvas with the given bounds, padding, and/or offset.');
381+
return;
382+
}
359383

360384
options.center = tr.unproject(nw.add(se).div(2));
361385
options.zoom = Math.min(tr.scaleZoom(tr.scale * Math.min(scaleX, scaleY)), options.maxZoom);

test/unit/ui/camera.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1153,16 +1153,16 @@ test('camera', (t) => {
11531153

11541154
camera.fitBounds(bb, { padding: 15, duration:0 });
11551155
t.deepEqual(fixedLngLat(camera.getCenter()), { lng: -100.5, lat: 34.717077774 }, 'pans to coordinates based on fitBounds with padding option as number applied');
1156-
t.equal(fixedNum(camera.getZoom(), 3), 2.064);
1156+
t.equal(fixedNum(camera.getZoom(), 3), 2.382);
11571157
t.end();
11581158
});
11591159

11601160
t.test('padding object', (t) => {
11611161
const camera = createCamera();
11621162
const bb = [[-133, 16], [-68, 50]];
11631163

1164-
camera.fitBounds(bb, { padding: {north: 10, east: 75, south: 50, west: 25}, duration:0 });
1165-
t.deepEqual(fixedLngLat(camera.getCenter()), { lng: -82.921875, lat: 22.403743206 }, 'pans to coordinates based on fitBounds with padding option as object applied');
1164+
camera.fitBounds(bb, { padding: {top: 10, right: 75, bottom: 50, left: 25}, duration:0 });
1165+
t.deepEqual(fixedLngLat(camera.getCenter()), { lng: -91.522099448, lat: 28.608945184 }, 'pans to coordinates based on fitBounds with padding option as object applied');
11661166
t.end();
11671167
});
11681168

0 commit comments

Comments
 (0)