Skip to content

Commit

Permalink
anti-meridian crossing for easeTo; do not wrap if maxBounds set (#4602)
Browse files Browse the repository at this point in the history
- Enable anti-meridian crossing for `easeTo`-based methods for
consistency with `flyTo`.
- Fix #2521 by disabling anti-meridian crossing if `maxBounds` is set.
  • Loading branch information
mourner authored Apr 18, 2017
1 parent c0dcee7 commit 0c3daa9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/ui/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,8 @@ class Camera extends Evented {
const pointAtOffset = tr.centerPoint.add(Point.convert(options.offset));
const locationAtOffset = tr.pointLocation(pointAtOffset);
const center = LngLat.convert(options.center || locationAtOffset);
this._normalizeCenter(center);

const from = tr.project(locationAtOffset);
const delta = tr.project(center).sub(from);
const finalScale = tr.zoomScale(zoom - startZoom);
Expand Down Expand Up @@ -695,16 +697,7 @@ class Camera extends Evented {
const pointAtOffset = tr.centerPoint.add(Point.convert(options.offset));
const locationAtOffset = tr.pointLocation(pointAtOffset);
const center = LngLat.convert(options.center || locationAtOffset);

// If a path crossing the antimeridian would be shorter, extend the final coordinate so that
// interpolating between the two endpoints will cross it.
if (tr.renderWorldCopies && Math.abs(tr.center.lng) + Math.abs(center.lng) > 180) {
if (tr.center.lng > 0 && center.lng < 0) {
center.lng += 360;
} else if (tr.center.lng < 0 && center.lng > 0) {
center.lng -= 360;
}
}
this._normalizeCenter(center);

const from = tr.project(locationAtOffset);
const delta = tr.project(center).sub(from);
Expand Down Expand Up @@ -868,6 +861,18 @@ class Camera extends Evented {
return bearing;
}

// If a path crossing the antimeridian would be shorter, extend the final coordinate so that
// interpolating between the two endpoints will cross it.
_normalizeCenter(center) {
const tr = this.transform;
if (!tr.renderWorldCopies || tr.lngRange) return;

const delta = center.lng - tr.center.lng;
center.lng +=
delta > 180 ? -360 :
delta < -180 ? 360 : 0;
}

// only used on mouse-wheel zoom to smooth out animation
_smoothOutEasing(duration) {
let easing = util.ease;
Expand Down
38 changes: 38 additions & 0 deletions test/unit/ui/camera.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,44 @@ test('camera', (t) => {
});
});

t.test('pans eastward across the antimeridian', (t) => {
const camera = createCamera();
camera.setCenter([170, 0]);
let crossedAntimeridian;

camera.on('move', () => {
if (camera.getCenter().lng > 170) {
crossedAntimeridian = true;
}
});

camera.on('moveend', () => {
t.ok(crossedAntimeridian);
t.end();
});

camera.easeTo({ center: [-170, 0], duration: 10 });
});

t.test('pans westward across the antimeridian', (t) => {
const camera = createCamera();
camera.setCenter([-170, 0]);
let crossedAntimeridian;

camera.on('move', () => {
if (camera.getCenter().lng < -170) {
crossedAntimeridian = true;
}
});

camera.on('moveend', () => {
t.ok(crossedAntimeridian);
t.end();
});

camera.easeTo({ center: [170, 0], duration: 10 });
});

t.end();
});

Expand Down

0 comments on commit 0c3daa9

Please sign in to comment.