@mapbox/react-map-gl
Version:
A React wrapper for MapboxGL-js and overlay API.
95 lines (79 loc) • 3.15 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import WebMercatorViewport from 'viewport-mercator-project';
import assert from '../assert';
import TransitionInterpolator from './transition-interpolator';
import { isValid, getEndValueByShortestPath } from './transition-utils';
import { lerp } from '../math-utils';
const VIEWPORT_TRANSITION_PROPS = ['longitude', 'latitude', 'zoom', 'bearing', 'pitch'];
/**
* Performs linear interpolation of two viewports.
*/
export default class LinearInterpolator extends TransitionInterpolator {
/**
* @param opts {Object}
* - opts.transitionProps {Array}] - list of props to apply linear transition to.
* - opts.around {Array} - a screen point to zoom/rotate around
*/
constructor() {
let opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
super();
_defineProperty(this, "around", void 0);
if (Array.isArray(opts)) {
// backward compatibility
opts = {
transitionProps: opts
};
}
this.propNames = opts.transitionProps || VIEWPORT_TRANSITION_PROPS;
if (opts.around) {
this.around = opts.around;
}
}
initializeProps(startProps, endProps) {
const startViewportProps = {};
const endViewportProps = {};
if (this.around) {
// anchor point in origin screen coordinates
startViewportProps.around = this.around; // anchor point in spherical coordinates
const aroundLngLat = new WebMercatorViewport(startProps).unproject(this.around);
Object.assign(endViewportProps, endProps, {
// anchor point in destination screen coordinates
around: new WebMercatorViewport(endProps).project(aroundLngLat),
aroundLngLat
});
}
for (const key of this.propNames) {
const startValue = startProps[key];
const endValue = endProps[key];
assert(isValid(startValue) && isValid(endValue), "".concat(key, " must be supplied for transition"));
startViewportProps[key] = startValue;
endViewportProps[key] = getEndValueByShortestPath(key, startValue, endValue);
}
return {
start: startViewportProps,
end: endViewportProps
};
}
interpolateProps(startProps, endProps, t) {
const viewport = {};
for (const key of this.propNames) {
viewport[key] = lerp(startProps[key], endProps[key], t);
}
if (endProps.around) {
// zoom around provided point
const _getMapCenterByLngLat = new WebMercatorViewport(Object.assign({}, endProps, viewport)).getMapCenterByLngLatPosition({
lngLat: endProps.aroundLngLat,
// anchor point in current screen coordinates
pos: lerp(startProps.around, endProps.around, t)
}),
_getMapCenterByLngLat2 = _slicedToArray(_getMapCenterByLngLat, 2),
longitude = _getMapCenterByLngLat2[0],
latitude = _getMapCenterByLngLat2[1];
viewport.longitude = longitude;
viewport.latitude = latitude;
}
return viewport;
}
}
//# sourceMappingURL=linear-interpolator.js.map