Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ScrollZoom handler setting tr.zoom = NaN #6924

Merged
merged 2 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/ui/handler/scroll_zoom.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow

import assert from 'assert';
import DOM from '../../util/dom';

import { ease as _ease, bindAll, bezier } from '../../util/util';
Expand Down Expand Up @@ -43,11 +44,11 @@ class ScrollZoomHandler {
_lastWheelEvent: any;
_lastWheelEventTime: number;

_startZoom: number;
_targetZoom: number;
_startZoom: ?number;
_targetZoom: ?number;
_delta: number;
_easing: (number) => number;
_prevEase: {start: number, duration: number, easing: (number) => number};
_easing: ?((number) => number);
_prevEase: ?{start: number, duration: number, easing: (number) => number};

_frameId: ?TaskID;

Expand Down Expand Up @@ -228,11 +229,18 @@ class ScrollZoomHandler {
this._delta = 0;
}

const targetZoom = typeof this._targetZoom === 'number' ?
this._targetZoom : tr.zoom;
const startZoom = this._startZoom;
const easing = this._easing;

let finished = false;
if (this._type === 'wheel') {
if (this._type === 'wheel' && startZoom && easing) {
assert(easing && typeof startZoom === 'number');

const t = Math.min((browser.now() - this._lastWheelEventTime) / 200, 1);
const k = this._easing(t);
tr.zoom = interpolate(this._startZoom, this._targetZoom, k);
const k = easing(t);
tr.zoom = interpolate(startZoom, targetZoom, k);
if (t < 1) {
if (!this._frameId) {
this._frameId = this._map._requestRenderFrame(this._onScrollFrame);
Expand All @@ -241,7 +249,7 @@ class ScrollZoomHandler {
finished = true;
}
} else {
tr.zoom = this._targetZoom;
tr.zoom = targetZoom;
finished = true;
}

Expand Down
1 change: 1 addition & 0 deletions test/node_modules/mapbox-gl-js-test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion test/unit/ui/handler/scroll_zoom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,25 @@ test('ScrollZoomHandler', (t) => {
t.end();
});

test('does not zoom if preventDefault is called on the wheel event', (t) => {
t.test('Gracefully handle wheel events that cancel each other out before the first scroll frame', (t) => {
// See also https://github.com/mapbox/mapbox-gl-js/issues/6782
const map = createMap(t);
map._renderTaskQueue.run();

simulate.wheel(map.getCanvas(), {type: 'wheel', deltaY: -1});
simulate.wheel(map.getCanvas(), {type: 'wheel', deltaY: -1});
now += 1;
simulate.wheel(map.getCanvas(), {type: 'wheel', deltaY: 2});

map._renderTaskQueue.run();

now += 400;
map._renderTaskQueue.run();

t.end();
});

t.test('does not zoom if preventDefault is called on the wheel event', (t) => {
const map = createMap(t);

map.on('wheel', e => e.preventDefault());
Expand Down