leaflet.maskhalo
Version:
A Leaflet plugin for creating halo effects around GeoJSON features with masking capabilities
153 lines (108 loc) • 3.45 kB
Markdown
[](https://www.npmjs.com/package/leaflet.maskhalo)
[](LICENSE)
Leaflet MaskHalo is a plugin for creating halo effects around GeoJSON features on a Leaflet map. It adds a mask that darkens the area outside the specified GeoJSON features and highlights the features with a customizable "halo" effect.
 <!-- Replace with actual demo image/gif when available -->
- Creates a mask that darkens the area outside GeoJSON features
- Adds a customizable halo effect around GeoJSON features
- Works with any GeoJSON geometry (Polygon, MultiPolygon, etc.)
- Easy to integrate with existing Leaflet maps
- Lightweight and performant
## Installation
### Using npm
```bash
npm install leaflet.maskhalo
```
### Using yarn
```bash
yarn add leaflet.maskhalo
```
### Using pnpm
```bash
pnpm add leaflet.maskhalo
```
### Peer Dependencies
Leaflet MaskHalo requires the following peer dependencies to be installed in your project:
```bash
npm install leaflet @turf/turf
```
## Usage
### Import the plugin
```javascript
import 'leaflet.maskhalo';
```
Or using CommonJS:
```javascript
require('leaflet.maskhalo');
```
```javascript
// Create a map
const map = L.map('map').setView([51.505, -0.09], 13);
// Add a tile layer
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);
// Sample GeoJSON data
const geojsonData = {
type: "FeatureCollection",
features: [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[
[],
[],
[],
[],
[],
[],
[],
//...
[]
]]
}
}
]
};
// Add MaskHalo to the map
const maskHalo = map.maskHalo();
maskHalo.addHalo(geojsonData);
```
```javascript
const maskHalo = map.maskHalo({
mask: {
fillColor: '#000',
fillOpacity: 0.5
},
halo: {
color: '#ff0000',
weight: 3
}
});
maskHalo.addHalo(geojsonData);
```
```javascript
maskHalo.remove();
```
Creates a new MaskHalo instance.
- `options` (Object, optional):
- `mask` (PathOptions, optional): Style options for the mask.
- `halo` (PathOptions, optional): Style options for the halo.
Adds a halo effect for the specified GeoJSON data.
- `data` (GeoJSON object or array): The GeoJSON feature(s) to highlight.
support multiple types of geoJson data.
- `Polygon | Polygon[]`: A object polygon or array of polygon.
- `MultiPolygon | MultiPolygon[]`: A object multi-polygon or array of multi-polygon.
- `FeatureCollection<Polygon | MultiPolygon>`: A collection of polygons or multi-polygons.
- `Feature<Polygon | MultiPolygon>| Feature<Polygon | MultiPolygon>[]`: A feature polygon or multi-polygon or an array of feature polygon or multi-polygon.
### `maskHalo.remove()`
Removes the mask and halo from the map.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.