leaflet.maskhalo
Version:
A Leaflet plugin for creating halo effects around GeoJSON features with masking capabilities
3 lines (2 loc) • 2.27 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("leaflet"),require("@turf/turf")):"function"==typeof define&&define.amd?define(["exports","leaflet","@turf/turf"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).L=e.L||{},e.L,e.turf)}(this,function(e,t,o){"use strict";function a(e){var t=Object.create(null);return e&&Object.keys(e).forEach(function(o){if("default"!==o){var a=Object.getOwnPropertyDescriptor(e,o);Object.defineProperty(t,o,a.get?a:{enumerable:!0,get:function(){return e[o]}})}}),t.default=e,Object.freeze(t)}var l=a(t);const r=o.bboxPolygon([-180,-85.05112877980659,180,85.05112877980659]);class n extends l.FeatureGroup{_options;_halo=null;_mask=null;constructor(e,t){super(),this._options={mask:{color:"transparent",weight:0,fillColor:"#000",fillOpacity:.3,...t?.mask},halo:{color:"#165DFF",weight:2,fillColor:"transparent",fillOpacity:0,...t?.halo}},this._createLayers(e)}_createLayers(e){let t=o.featureCollection([]);if(Array.isArray(e))for(const a of e)"Feature"==a.type?["Polygon","MultiPolygon"].includes(a.geometry.type)&&t.features.push(a):["Polygon","MultiPolygon"].includes(a.type)&&t.features.push(o.feature(a));else switch(e.type){case"FeatureCollection":const a=e.features.filter(e=>["Polygon","MultiPolygon"].includes(e.geometry.type));t=o.featureCollection(a);break;case"Feature":["Polygon","MultiPolygon"].includes(e.geometry.type)&&(t=o.featureCollection([e]));break;case"Polygon":case"MultiPolygon":t=o.featureCollection([o.feature(e)])}if(t.features.length<1)return void console.warn("【leaflet.maskhalo】 No valid polygon data to show.");this._halo=l.geoJSON(t,{style:{...this._options.halo}});const a=o.difference(o.featureCollection([r,...t.features]));a?(this._mask=l.geoJSON(a,{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 s(e,t){return new n(e,t)}l.Map.addInitHook(function(){this.maskHalo=(e,t)=>new n(e,t)}),"undefined"!=typeof window&&window.L&&(window.L.MaskHalo=n,window.L.maskHalo=s),e.MaskHalo=n,e.maskHalo=s});
//# sourceMappingURL=leaflet.maskhalo.umd.js.map