Skip to content

Commit ebee9f7

Browse files
feat(camera): Make jumpTo, easeTo, and flyTo respect the zoomSnap map option by snapping the zoom level to the nearest valid increment (maplibre#7338)
* respect snapZoom for zoom setting functions * changelog
1 parent f01cb9d commit ebee9f7

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### 🐞 Bug fixes
66
- _...Add new stuff here..._
77
- Make `fitBounds` and `fitScreenCoordinates` respect the `zoomSnap` map option by snapping the zoom level down to keep bounds fully visible ([#7332](https://github.com/maplibre/maplibre-gl-js/issues/7332) (by [@CommanderStorm](https://github.com/CommanderStorm))
8+
- Make `jumpTo`, `easeTo`, and `flyTo` respect the `zoomSnap` map option by snapping the zoom level to the nearest valid increment ([#7333](https://github.com/maplibre/maplibre-gl-js/issues/7333) (by [@CommanderStorm](https://github.com/CommanderStorm))
89

910
## 5.21.1
1011

src/ui/camera.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,18 @@ describe('jumpTo', () => {
393393
camera.jumpTo({center: [1, 2]});
394394
expect(!camera.isEasing()).toBeTruthy();
395395
});
396+
397+
test('respects zoomSnap', () => {
398+
const camera = createCamera({zoomSnap: 1});
399+
camera.jumpTo({zoom: 2.3});
400+
expect(camera.getZoom()).toBe(2);
401+
});
402+
403+
test('zoomSnap=0 does not snap zoom', () => {
404+
const camera = createCamera({zoomSnap: 0});
405+
camera.jumpTo({zoom: 2.3});
406+
expect(camera.getZoom()).toBe(2.3);
407+
});
396408
});
397409

398410
describe('setCenter', () => {
@@ -466,6 +478,12 @@ describe('setZoom', () => {
466478
camera.setZoom(5);
467479
expect(!camera.isEasing()).toBeTruthy();
468480
});
481+
482+
test('respects zoomSnap', () => {
483+
const camera = createCamera({zoomSnap: 0.5});
484+
camera.setZoom(2.7);
485+
expect(camera.getZoom()).toBe(2.5);
486+
});
469487
});
470488

471489
describe('setBearing', () => {
@@ -1303,6 +1321,18 @@ describe('easeTo', () => {
13031321

13041322
expect(camera.getBearing()).toEqual(97);
13051323
});
1324+
1325+
test('respects zoomSnap', () => {
1326+
const camera = createCamera({zoomSnap: 1});
1327+
camera.easeTo({zoom: 2.7, duration: 0});
1328+
expect(camera.getZoom()).toBe(3);
1329+
});
1330+
1331+
test('zoomSnap=0 does not snap zoom', () => {
1332+
const camera = createCamera({zoomSnap: 0});
1333+
camera.easeTo({zoom: 2.7, duration: 0});
1334+
expect(camera.getZoom()).toBe(2.7);
1335+
});
13061336
});
13071337

13081338
describe('flyTo', () => {
@@ -2117,6 +2147,18 @@ describe('flyTo', () => {
21172147
camera._finalizeElevation();
21182148
expect(camera._elevationFreeze).toBeFalsy();
21192149
});
2150+
2151+
test('respects zoomSnap', () => {
2152+
const camera = createCamera({zoomSnap: 1});
2153+
camera.flyTo({zoom: 2.7, animate: false});
2154+
expect(camera.getZoom()).toBe(3);
2155+
});
2156+
2157+
test('zoomSnap=0 does not snap zoom', () => {
2158+
const camera = createCamera({zoomSnap: 0});
2159+
camera.flyTo({zoom: 2.7, animate: false});
2160+
expect(camera.getZoom()).toBe(2.7);
2161+
});
21202162
});
21212163

21222164
describe('isEasing', () => {

src/ui/camera.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,10 @@ export abstract class Camera extends Evented {
948948
jumpTo(options: JumpToOptions, eventData?: any): this {
949949
this.stop();
950950

951+
if ('zoom' in options && this._zoomSnap) {
952+
options.zoom = evaluateZoomSnap(options.zoom, this._zoomSnap);
953+
}
954+
951955
const tr = this._getTransformForUpdate();
952956
let bearingChanged = false,
953957
pitchChanged = false;
@@ -1120,6 +1124,10 @@ export abstract class Camera extends Evented {
11201124
easing: defaultEasing
11211125
}, options);
11221126

1127+
if ('zoom' in options && this._zoomSnap) {
1128+
options.zoom = evaluateZoomSnap(options.zoom, this._zoomSnap);
1129+
}
1130+
11231131
if (options.animate === false || (!options.essential && browser.prefersReducedMotion)) {
11241132
options.duration = 0;
11251133
}
@@ -1436,6 +1444,10 @@ export abstract class Camera extends Evented {
14361444
easing: defaultEasing
14371445
}, options);
14381446

1447+
if ('zoom' in options && this._zoomSnap) {
1448+
options.zoom = evaluateZoomSnap(options.zoom, this._zoomSnap);
1449+
}
1450+
14391451
const tr = this._getTransformForUpdate(),
14401452
startBearing = tr.bearing,
14411453
startPitch = tr.pitch,

0 commit comments

Comments
 (0)