CanvasMarkers.js (2882B)
1 import L from "leaflet"; 2 import '~/lib/leaflet.layer.canvasMarkers'; 3 4 L.Layer.CanvasMarkers.include({ 5 _printProgressWeight: 0.1, 6 7 cloneMarker: function(marker) { 8 return { 9 latlng: {lat: marker.latlng.lat, lng: marker.latlng.lng}, 10 label: marker.label, 11 icon: marker.icon 12 }; 13 }, 14 15 cloneMarkers: function() { 16 const markers = this.rtree.all(); 17 18 const markersCopy = markers.map(this.cloneMarker); 19 return markersCopy; 20 }, 21 22 cloneForPrint: function(options) { 23 options = L.Util.extend({}, this.options, {noWrap: true}); 24 if (options.printIconsOpacity !== undefined) { 25 options.iconsOpacity = options.printIconsOpacity; 26 } 27 return new L.Layer.CanvasMarkers(this.cloneMarkers(), options); 28 }, 29 30 getTilesInfo: async function(printOptions) { 31 this.options.iconScale = printOptions.resolution / 90 * 0.75; 32 const scale = printOptions.pixelBounds.getSize().x / printOptions.destPixelSize.x; 33 const pixelExtents = { 34 tileN: printOptions.pixelBounds.getTopRight().y / scale, 35 tileS: printOptions.pixelBounds.getBottomLeft().y / scale, 36 tileE: printOptions.pixelBounds.getTopRight().x / scale, 37 tileW: printOptions.pixelBounds.getBottomLeft().x / scale 38 }; 39 const crs = L.CRS.EPSG3857; 40 if (!this._map) { 41 const dummyMap = { 42 project: crs.latLngToPoint.bind(crs), 43 unproject: crs.pointToLatLng.bind(crs), 44 }; 45 this._map = dummyMap; 46 } 47 const zoom = crs.zoom((1 / scale) * crs.scale(printOptions.zoom)); 48 const {iconUrls, markerJobs, pointsForLabels} = this.selectMarkersForDraw(pixelExtents, zoom, true); 49 await this.preloadIcons(iconUrls); 50 return { 51 iterateTilePromises: (function*() { 52 yield { 53 tilePromise: Promise.resolve({ 54 draw: (canvas) => { 55 this.resetLabels(); 56 this.drawSelectedMarkers(canvas, pixelExtents, markerJobs, pointsForLabels, zoom, 57 true); 58 }, 59 isOverlay: true, 60 overlaySolid: !this.options.printTransparent 61 } 62 ), 63 abortLoading: () => { 64 // no actions needed 65 } 66 }; 67 }).bind(this), 68 count: 1 69 }; 70 } 71 } 72 );