UNPKG

react-map-gl

Version:

React components for MapLibre GL JS and Mapbox GL JS

79 lines 2.33 kB
import { deepEqual } from "./deep-equal.js"; /** * Make a copy of a transform * @param tr */ export function cloneTransform(tr) { const newTransform = tr.clone(); // Work around mapbox bug - this value is not assigned in clone(), only in resize() newTransform.pixelsToGLUnits = tr.pixelsToGLUnits; return newTransform; } /** * Copy projection from one transform to another. This only applies to mapbox-gl transforms * @param src the transform to copy projection settings from * @param dest to transform to copy projection settings to */ export function syncProjection(src, dest) { if (!src.getProjection) { return; } const srcProjection = src.getProjection(); const destProjection = dest.getProjection(); if (!deepEqual(srcProjection, destProjection)) { dest.setProjection(srcProjection); } } /** * Capture a transform's current state * @param transform * @returns descriptor of the view state */ export function transformToViewState(tr) { return { longitude: tr.center.lng, latitude: tr.center.lat, zoom: tr.zoom, pitch: tr.pitch, bearing: tr.bearing, padding: tr.padding }; } /* eslint-disable complexity */ /** * Mutate a transform to match the given view state * @param transform * @param viewState * @returns true if the transform has changed */ export function applyViewStateToTransform(tr, props) { const v = props.viewState || props; let changed = false; if ('zoom' in v) { const zoom = tr.zoom; tr.zoom = v.zoom; changed = changed || zoom !== tr.zoom; } if ('bearing' in v) { const bearing = tr.bearing; tr.bearing = v.bearing; changed = changed || bearing !== tr.bearing; } if ('pitch' in v) { const pitch = tr.pitch; tr.pitch = v.pitch; changed = changed || pitch !== tr.pitch; } if (v.padding && !tr.isPaddingEqual(v.padding)) { changed = true; tr.padding = v.padding; } if ('longitude' in v && 'latitude' in v) { const center = tr.center; // @ts-ignore tr.center = new center.constructor(v.longitude, v.latitude); changed = changed || center !== tr.center; } return changed; } //# sourceMappingURL=transform.js.map