1(function (root, factory) { 2 if (typeof define === 'function' && define.amd) { 3 // AMD 4 define(['leaflet', 'maplibre-gl'], factory); 5 } else if (typeof exports === 'object') { 6 // Node, CommonJS-like 7 module.exports = factory(require('leaflet'), require('maplibre-gl')); 8 } else { 9 // Browser globals (root is window) 10 root.returnExports = factory(root.L, root.maplibregl); 11 } 12}(typeof globalThis !== 'undefined' ? globalThis : this || self, function (L, maplibregl) { 13 L.MaplibreGL = L.Layer.extend({ 14 options: { 15 updateInterval: 32, 16 // How much to extend the overlay view (relative to map size) 17 // e.g. 0.1 would be 10% of map view in each direction 18 padding: 0.1, 19 // whether or not to register the mouse and keyboard 20 // events on the maplibre overlay 21 interactive: false, 22 // set the tilepane as the default pane to draw gl tiles 23 pane: 'tilePane' 24 }, 25 26 initialize: function (options) { 27 L.setOptions(this, options); 28 29 // setup throttling the update event when panning 30 this._throttledUpdate = L.Util.throttle(this._update, this.options.updateInterval, this); 31 }, 32 33 onAdd: function (map) { 34 if (!this._container) { 35 this._initContainer(); 36 } 37 38 var paneName = this.getPaneName(); 39 map.getPane(paneName).appendChild(this._container); 40 41 this._initGL(); 42 43 this._offset = this._map.containerPointToLayerPoint([0, 0]); 44 45 // work around https://github.com/mapbox/mapbox-gl-leaflet/issues/47 46 if (map.options.zoomAnimation) { 47 L.DomEvent.on(map._proxy, L.DomUtil.TRANSITION_END, this._transitionEnd, this); 48 } 49 }, 50 51 onRemove: function (map) { 52 if (this._map._proxy && this._map.options.zoomAnimation) { 53 L.DomEvent.off(this._map._proxy, L.DomUtil.TRANSITION_END, this._transitionEnd, this); 54 } 55 var paneName = this.getPaneName(); 56 map.getPane(paneName).removeChild(this._container); 57 58 this._glMap.remove(); 59 this._glMap = null; 60 }, 61 62 getEvents: function () { 63 return { 64 move: this._throttledUpdate, // sensibly throttle updating while panning 65 zoomanim: this._animateZoom, // applys the zoom animation to the <canvas> 66 zoom: this._pinchZoom, // animate every zoom event for smoother pinch-zooming 67 zoomstart: this._zoomStart, // flag starting a zoom to disable panning 68 zoomend: this._zoomEnd, 69 resize: this._resize 70 }; 71 }, 72 73 getMaplibreMap: function () { 74 return this._glMap; 75 }, 76 77 getCanvas: function () { 78 return this._glMap.getCanvas(); 79 }, 80 81 getSize: function () { 82 return this._map.getSize().multiplyBy(1 + this.options.padding * 2); 83 }, 84 85 getBounds: function () { 86 var halfSize = this.getSize().multiplyBy(0.5); 87 var center = this._map.latLngToContainerPoint(this._map.getCenter()); 88 return L.latLngBounds( 89 this._map.containerPointToLatLng(center.subtract(halfSize)), 90 this._map.containerPointToLatLng(center.add(halfSize)) 91 ); 92 }, 93 94 getContainer: function () { 95 return this._container; 96 }, 97 98 // returns the pane name set in options if it is a valid pane, defaults to tilePane 99 getPaneName: function () { 100 return this._map.getPane(this.options.pane) ? this.options.pane : 'tilePane'; 101 }, 102 103 _roundPoint: function(p) { 104 return {x: Math.round(p.x), y: Math.round(p.y)}; 105 }, 106 107 _initContainer: function () { 108 var container = this._container = L.DomUtil.create('div', 'leaflet-gl-layer'); 109 110 var size = this.getSize(); 111 var offset = this._map.getSize().multiplyBy(this.options.padding); 112 container.style.width = size.x + 'px'; 113 container.style.height = size.y + 'px'; 114 115 var topLeft = this._map.containerPointToLayerPoint([0, 0]).subtract(offset); 116 117 L.DomUtil.setPosition(container, this._roundPoint(topLeft)); 118 }, 119 120 _initGL: function () { 121 var center = this._map.getCenter(); 122 123 var options = L.extend({}, this.options, { 124 container: this._container, 125 center: [center.lng, center.lat], 126 zoom: this._map.getZoom() - 1, 127 attributionControl: false 128 }); 129 130 this._glMap = new maplibregl.Map(options); 131 132 // allow GL base map to pan beyond min/max latitudes 133 this._glMap.transform.latRange = null; 134 this._glMap.transform.maxValidLatitude = Infinity; 135 136 this._transformGL(this._glMap); 137 138 if (this._glMap._canvas.canvas) { 139 // older versions of mapbox-gl surfaced the canvas differently 140 this._glMap._actualCanvas = this._glMap._canvas.canvas; 141 } else { 142 this._glMap._actualCanvas = this._glMap._canvas; 143 } 144 145 // treat child <canvas> element like L.ImageOverlay 146 var canvas = this._glMap._actualCanvas; 147 L.DomUtil.addClass(canvas, 'leaflet-image-layer'); 148 L.DomUtil.addClass(canvas, 'leaflet-zoom-animated'); 149 if (this.options.interactive) { 150 L.DomUtil.addClass(canvas, 'leaflet-interactive'); 151 } 152 if (this.options.className) { 153 L.DomUtil.addClass(canvas, this.options.className); 154 } 155 }, 156 157 _update: function (e) { 158 if (!this._map) { 159 return; 160 } 161 // update the offset so we can correct for it later when we zoom 162 this._offset = this._map.containerPointToLayerPoint([0, 0]); 163 164 if (this._zooming) { 165 return; 166 } 167
vendor: 4,272 bytes, lines 168-289
168 var size = this.getSize(), 169 container = this._container, 170 gl = this._glMap, 171 offset = this._map.getSize().multiplyBy(this.options.padding), 172 topLeft = this._map.containerPointToLayerPoint([0, 0]).subtract(offset); 173 174 L.DomUtil.setPosition(container, this._roundPoint(topLeft)); 175 176 this._transformGL(gl); 177 178 if (gl.transform.width !== size.x || gl.transform.height !== size.y) { 179 container.style.width = size.x + 'px'; 180 container.style.height = size.y + 'px'; 181 if (gl._resize !== null && gl._resize !== undefined){ 182 gl._resize(); 183 } else { 184 gl.resize(); 185 } 186 } else { 187 // older versions of mapbox-gl surfaced update publicly 188 if (gl._update !== null && gl._update !== undefined){ 189 gl._update(); 190 } else { 191 gl.update(); 192 } 193 } 194 }, 195 196 _transformGL: function (gl) { 197 var center = this._map.getCenter(); 198 199 // gl.setView([center.lat, center.lng], this._map.getZoom() - 1, 0); 200 // calling setView directly causes sync issues because it uses requestAnimFrame 201 202 var tr = gl.transform; 203 tr.center = maplibregl.LngLat.convert([center.lng, center.lat]); 204 tr.zoom = this._map.getZoom() - 1; 205 }, 206 207 // update the map constantly during a pinch zoom 208 _pinchZoom: function (e) { 209 this._glMap.jumpTo({ 210 zoom: this._map.getZoom() - 1, 211 center: this._map.getCenter() 212 }); 213 }, 214 215 // borrowed from L.ImageOverlay 216 // https://github.com/Leaflet/Leaflet/blob/master/src/layer/ImageOverlay.js#L139-L144 217 _animateZoom: function (e) { 218 var scale = this._map.getZoomScale(e.zoom); 219 var padding = this._map.getSize().multiplyBy(this.options.padding * scale); 220 var viewHalf = this.getSize()._divideBy(2); 221 // corrections for padding (scaled), adapted from 222 // https://github.com/Leaflet/Leaflet/blob/master/src/map/Map.js#L1490-L1508 223 var topLeft = this._map.project(e.center, e.zoom) 224 ._subtract(viewHalf) 225 ._add(this._map._getMapPanePos() 226 .add(padding))._round(); 227 var offset = this._map.project(this._map.getBounds().getNorthWest(), e.zoom) 228 ._subtract(topLeft); 229 230 L.DomUtil.setTransform( 231 this._glMap._actualCanvas, 232 offset.subtract(this._offset), 233 scale 234 ); 235 }, 236 237 _zoomStart: function (e) { 238 this._zooming = true; 239 }, 240 241 _zoomEnd: function () { 242 var scale = this._map.getZoomScale(this._map.getZoom()); 243 244 L.DomUtil.setTransform( 245 this._glMap._actualCanvas, 246 // https://github.com/mapbox/mapbox-gl-leaflet/pull/130 247 null, 248 scale 249 ); 250 251 this._zooming = false; 252 253 this._update(); 254 }, 255 256 _transitionEnd: function (e) { 257 L.Util.requestAnimFrame(function () { 258 var zoom = this._map.getZoom(); 259 var center = this._map.getCenter(); 260 var offset = this._map.latLngToContainerPoint( 261 this._map.getBounds().getNorthWest() 262 ); 263 264 // reset the scale and offset 265 L.DomUtil.setTransform(this._glMap._actualCanvas, offset, 1); 266 267 // enable panning once the gl map is ready again 268 this._glMap.once('moveend', L.Util.bind(function () { 269 this._zoomEnd(); 270 }, this)); 271 272 // update the map position 273 this._glMap.jumpTo({ 274 center: center, 275 zoom: zoom - 1 276 }); 277 }, this); 278 }, 279 280 _resize: function (e) { 281 this._transitionEnd(e); 282 } 283 }); 284 285 L.maplibreGL = function (options) { 286 return new L.MaplibreGL(options); 287 }; 288 289}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.