A React hook for adding flower layers to MapLibre GL JS and MapBox GL JS maps. Flowers appear randomly on the map and disappear with animations when hovered.
- 🌺 Random flower placement on map bounds
- ✨ Smooth hover animations (rotation, scaling, opacity)
- 📅 Seasonal display with date range support
- 🎨 Customizable flower icons and scaling
- 🗺️ Compatible with both MapLibre GL JS and MapBox GL JS
- ⚡ Lightweight and performant
npm install @mnameghi/map-flowers-hookyarn add @mnameghi/map-flowers-hookpnpm add @mnameghi/map-flowers-hookimport React, { useRef, useEffect, useState } from "react";
import maplibregl from "maplibre-gl";
import "maplibre-gl/dist/maplibre-gl.css";
import { useFlowersLayer } from "@mnameghi/map-flowers-hook";
function MapComponent() {
const mapContainer = useRef<HTMLDivElement>(null);
const [map, setMap] = useState<maplibregl.Map | null>(null);
useEffect(() => {
if (!mapContainer.current) return;
const mapInstance = new maplibregl.Map({
container: mapContainer.current,
style: "https://demotiles.maplibre.org/style.json",
center: [0, 0],
zoom: 2,
});
setMap(mapInstance);
return () => mapInstance.remove();
}, []);
// Add flowers to the map
useFlowersLayer(map, {
count: 15,
flowerScale: { min: 0.5, max: 1.2 },
});
return <div ref={mapContainer} style={{ height: "400px", width: "100%" }} />;
}Display flowers only during specific date ranges:
useFlowersLayer(map, {
count: 20,
startDate: "03-20 00:00", // March 20th
endDate: "06-21 23:59", // June 21st
iconData: "/path/to/spring-flower.png",
});useFlowersLayer(map, {
count: 10,
iconData: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...", // Base64 image
flowerScale: { min: 0.3, max: 0.8 },
});| Parameter | Type | Default | Description |
|---|---|---|---|
map |
SupportedMap | null |
- | MapLibre or MapBox map instance |
options |
UseFlowersLayerOptions |
{} |
Configuration options |
| Option | Type | Default | Description |
|---|---|---|---|
sourceId |
string |
'flowers-source' |
GeoJSON source identifier |
layerId |
string |
'flowers-layer' |
Map layer identifier |
iconName |
string |
'flower-icon' |
Icon name for the map |
iconData |
string |
Built-in blossom | Icon URL or base64 data |
count |
number |
10 |
Number of flowers to display |
flowerScale |
FlowerScale |
{min: 0.3, max: 1} |
Scale range for flowers |
startDate |
string |
- | Start date (MM-DD HH:MM format) |
endDate |
string |
- | End date (MM-DD HH:MM format) |
type FlowerScale = {
min: number;
max: number;
};
type UseFlowersLayerOptions = {
sourceId?: string;
layerId?: string;
iconName?: string;
count?: number;
iconData?: string;
flowerScale?: FlowerScale;
startDate?: string;
endDate?: string;
};- Random Placement: Flowers are randomly positioned within the current map bounds
- Hover Animation: When hovered, flowers rotate, scale down, and fade out over 30 frames
- Auto Replacement: After animation completes, flowers are replaced with new ones
- Map Movement: New flowers are generated when the map view changes
- Seasonal Control: Flowers only appear during specified date ranges (if configured)
react>= 16.8.0maplibre-gl>= 3.0.0 ORmapbox-gl>= 2.0.0
Works in all modern browsers that support:
- ES2015+ features
- Canvas API
- WebGL
- RequestAnimationFrame
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
MIT License - see the LICENSE file for details.
- update document
- Initial release
- Basic flower layer functionality
- Hover animations
- Seasonal date range support
- MapLibre and MapBox compatibility
