leaflet.maskhalo
Version:
A Leaflet plugin for creating halo effects around GeoJSON features with masking capabilities
3 lines (2 loc) • 1.68 kB
JavaScript
import*as e from"leaflet";import{bboxPolygon as o,featureCollection as t,feature as a,difference as s}from"@turf/turf";const l=o([-180,-85.05112877980659,180,85.05112877980659]);class r extends e.FeatureGroup{_options;_halo=null;_mask=null;constructor(e,o){super(),this._options={mask:{color:"transparent",weight:0,fillColor:"#000",fillOpacity:.3,...o?.mask},halo:{color:"#165DFF",weight:2,fillColor:"transparent",fillOpacity:0,...o?.halo}},this._createLayers(e)}_createLayers(o){let r=t([]);if(Array.isArray(o))for(const e of o)"Feature"==e.type?["Polygon","MultiPolygon"].includes(e.geometry.type)&&r.features.push(e):["Polygon","MultiPolygon"].includes(e.type)&&r.features.push(a(e));else switch(o.type){case"FeatureCollection":const e=o.features.filter(e=>["Polygon","MultiPolygon"].includes(e.geometry.type));r=t(e);break;case"Feature":["Polygon","MultiPolygon"].includes(o.geometry.type)&&(r=t([o]));break;case"Polygon":case"MultiPolygon":r=t([a(o)])}if(r.features.length<1)return void console.warn("【leaflet.maskhalo】 No valid polygon data to show.");this._halo=e.geoJSON(r,{style:{...this._options.halo}});const i=s(t([l,...r.features]));i?(this._mask=e.geoJSON(i,{style:{...this._options.mask}}),this.addLayer(this._mask),this.addLayer(this._halo)):console.warn("【leaflet.maskhalo】 Mask computation failed. Please check geometry data. ")}setHalo(e){return this.clearLayers(),this._createLayers(e),this}}function i(e,o){return new r(e,o)}e.Map.addInitHook(function(){this.maskHalo=(e,o)=>new r(e,o)}),"undefined"!=typeof window&&window.L&&(window.L.MaskHalo=r,window.L.maskHalo=i);export{r as MaskHalo,i as maskHalo};
//# sourceMappingURL=leaflet.maskhalo.esm.js.map