Spaces:
Sleeping
Sleeping
File size: 10,974 Bytes
4a288a7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | // @flow
import assert from 'assert';
import DOM from '../../util/dom';
import {ease as _ease, bindAll, bezier} from '../../util/util';
import browser from '../../util/browser';
import window from '../../util/window';
import {number as interpolate} from '../../style-spec/util/interpolate';
import LngLat from '../../geo/lng_lat';
import type Map from '../map';
import type HandlerManager from '../handler_manager';
import type Point from '@mapbox/point-geometry';
// deltaY value for mouse scroll wheel identification
const wheelZoomDelta = 4.000244140625;
// These magic numbers control the rate of zoom. Trackpad events fire at a greater
// frequency than mouse scroll wheel, so reduce the zoom rate per wheel tick
const defaultZoomRate = 1 / 100;
const wheelZoomRate = 1 / 450;
// upper bound on how much we scale the map in any single render frame; this
// is used to limit zoom rate in the case of very fast scrolling
const maxScalePerFrame = 2;
/**
* The `ScrollZoomHandler` allows the user to zoom the map by scrolling.
*/
class ScrollZoomHandler {
_map: Map;
_el: HTMLElement;
_enabled: boolean;
_active: boolean;
_zooming: boolean;
_aroundCenter: boolean;
_around: Point;
_aroundPoint: Point;
_type: 'wheel' | 'trackpad' | null;
_lastValue: number;
_timeout: ?TimeoutID; // used for delayed-handling of a single wheel movement
_finishTimeout: ?TimeoutID; // used to delay final '{move,zoom}end' events
_lastWheelEvent: any;
_lastWheelEventTime: number;
_startZoom: ?number;
_targetZoom: ?number;
_delta: number;
_easing: ?((number) => number);
_prevEase: ?{start: number, duration: number, easing: (_: number) => number};
_frameId: ?boolean;
_handler: HandlerManager;
_defaultZoomRate: number;
_wheelZoomRate: number;
/**
* @private
*/
constructor(map: Map, handler: HandlerManager) {
this._map = map;
this._el = map.getCanvasContainer();
this._handler = handler;
this._delta = 0;
this._defaultZoomRate = defaultZoomRate;
this._wheelZoomRate = wheelZoomRate;
bindAll(['_onTimeout'], this);
}
/**
* Set the zoom rate of a trackpad
* @param {number} [zoomRate=1/100] The rate used to scale trackpad movement to a zoom value.
* @example
* // Speed up trackpad zoom
* map.scrollZoom.setZoomRate(1/25);
*/
setZoomRate(zoomRate: number) {
this._defaultZoomRate = zoomRate;
}
/**
* Set the zoom rate of a mouse wheel
* @param {number} [wheelZoomRate=1/450] The rate used to scale mouse wheel movement to a zoom value.
* @example
* // Slow down zoom of mouse wheel
* map.scrollZoom.setWheelZoomRate(1/600);
*/
setWheelZoomRate(wheelZoomRate: number) {
this._wheelZoomRate = wheelZoomRate;
}
/**
* Returns a Boolean indicating whether the "scroll to zoom" interaction is enabled.
*
* @returns {boolean} `true` if the "scroll to zoom" interaction is enabled.
*/
isEnabled() {
return !!this._enabled;
}
/*
* Active state is turned on and off with every scroll wheel event and is set back to false before the map
* render is called, so _active is not a good candidate for determining if a scroll zoom animation is in
* progress.
*/
isActive() {
return !!this._active || this._finishTimeout !== undefined;
}
isZooming() {
return !!this._zooming;
}
/**
* Enables the "scroll to zoom" interaction.
*
* @param {Object} [options] Options object.
* @param {string} [options.around] If "center" is passed, map will zoom around center of map
*
* @example
* map.scrollZoom.enable();
* @example
* map.scrollZoom.enable({ around: 'center' })
*/
enable(options: any) {
if (this.isEnabled()) return;
this._enabled = true;
this._aroundCenter = options && options.around === 'center';
}
/**
* Disables the "scroll to zoom" interaction.
*
* @example
* map.scrollZoom.disable();
*/
disable() {
if (!this.isEnabled()) return;
this._enabled = false;
}
wheel(e: WheelEvent) {
if (!this.isEnabled()) return;
// Remove `any` cast when https://github.com/facebook/flow/issues/4879 is fixed.
let value = e.deltaMode === (window.WheelEvent: any).DOM_DELTA_LINE ? e.deltaY * 40 : e.deltaY;
const now = browser.now(),
timeDelta = now - (this._lastWheelEventTime || 0);
this._lastWheelEventTime = now;
if (value !== 0 && (value % wheelZoomDelta) === 0) {
// This one is definitely a mouse wheel event.
this._type = 'wheel';
} else if (value !== 0 && Math.abs(value) < 4) {
// This one is definitely a trackpad event because it is so small.
this._type = 'trackpad';
} else if (timeDelta > 400) {
// This is likely a new scroll action.
this._type = null;
this._lastValue = value;
// Start a timeout in case this was a singular event, and dely it by up to 40ms.
this._timeout = setTimeout(this._onTimeout, 40, e);
} else if (!this._type) {
// This is a repeating event, but we don't know the type of event just yet.
// If the delta per time is small, we assume it's a fast trackpad; otherwise we switch into wheel mode.
this._type = (Math.abs(timeDelta * value) < 200) ? 'trackpad' : 'wheel';
// Make sure our delayed event isn't fired again, because we accumulate
// the previous event (which was less than 40ms ago) into this event.
if (this._timeout) {
clearTimeout(this._timeout);
this._timeout = null;
value += this._lastValue;
}
}
// Slow down zoom if shift key is held for more precise zooming
if (e.shiftKey && value) value = value / 4;
// Only fire the callback if we actually know what type of scrolling device the user uses.
if (this._type) {
this._lastWheelEvent = e;
this._delta -= value;
if (!this._active) {
this._start(e);
}
}
e.preventDefault();
}
_onTimeout(initialEvent: any) {
this._type = 'wheel';
this._delta -= this._lastValue;
if (!this._active) {
this._start(initialEvent);
}
}
_start(e: any) {
if (!this._delta) return;
if (this._frameId) {
this._frameId = null;
}
this._active = true;
if (!this.isZooming()) {
this._zooming = true;
}
if (this._finishTimeout) {
clearTimeout(this._finishTimeout);
delete this._finishTimeout;
}
const pos = DOM.mousePos(this._el, e);
this._around = LngLat.convert(this._aroundCenter ? this._map.getCenter() : this._map.unproject(pos));
this._aroundPoint = this._map.transform.locationPoint(this._around);
if (!this._frameId) {
this._frameId = true;
this._handler._triggerRenderFrame();
}
}
renderFrame() {
if (!this._frameId) return;
this._frameId = null;
if (!this.isActive()) return;
const tr = this._map.transform;
// if we've had scroll events since the last render frame, consume the
// accumulated delta, and update the target zoom level accordingly
if (this._delta !== 0) {
// For trackpad events and single mouse wheel ticks, use the default zoom rate
const zoomRate = (this._type === 'wheel' && Math.abs(this._delta) > wheelZoomDelta) ? this._wheelZoomRate : this._defaultZoomRate;
// Scale by sigmoid of scroll wheel delta.
let scale = maxScalePerFrame / (1 + Math.exp(-Math.abs(this._delta * zoomRate)));
if (this._delta < 0 && scale !== 0) {
scale = 1 / scale;
}
const fromScale = typeof this._targetZoom === 'number' ? tr.zoomScale(this._targetZoom) : tr.scale;
this._targetZoom = Math.min(tr.maxZoom, Math.max(tr.minZoom, tr.scaleZoom(fromScale * scale)));
// if this is a mouse wheel, refresh the starting zoom and easing
// function we're using to smooth out the zooming between wheel
// events
if (this._type === 'wheel') {
this._startZoom = tr.zoom;
this._easing = this._smoothOutEasing(200);
}
this._delta = 0;
}
const targetZoom = typeof this._targetZoom === 'number' ?
this._targetZoom : tr.zoom;
const startZoom = this._startZoom;
const easing = this._easing;
let finished = false;
let zoom;
if (this._type === 'wheel' && startZoom && easing) {
assert(easing && typeof startZoom === 'number');
const t = Math.min((browser.now() - this._lastWheelEventTime) / 200, 1);
const k = easing(t);
zoom = interpolate(startZoom, targetZoom, k);
if (t < 1) {
if (!this._frameId) {
this._frameId = true;
}
} else {
finished = true;
}
} else {
zoom = targetZoom;
finished = true;
}
this._active = true;
if (finished) {
this._active = false;
this._finishTimeout = setTimeout(() => {
this._zooming = false;
this._handler._triggerRenderFrame();
delete this._targetZoom;
delete this._finishTimeout;
}, 200);
}
return {
noInertia: true,
needsRenderFrame: !finished,
zoomDelta: zoom - tr.zoom,
around: this._aroundPoint,
originalEvent: this._lastWheelEvent
};
}
_smoothOutEasing(duration: number) {
let easing = _ease;
if (this._prevEase) {
const ease = this._prevEase,
t = (browser.now() - ease.start) / ease.duration,
speed = ease.easing(t + 0.01) - ease.easing(t),
// Quick hack to make new bezier that is continuous with last
x = 0.27 / Math.sqrt(speed * speed + 0.0001) * 0.01,
y = Math.sqrt(0.27 * 0.27 - x * x);
easing = bezier(x, y, 0.25, 1);
}
this._prevEase = {
start: browser.now(),
duration,
easing
};
return easing;
}
blur() {
this.reset();
}
reset() {
this._active = false;
}
}
export default ScrollZoomHandler;
|