Map Blur Area
terrae/blur-areaFreeComponent
Blur sensitive areas on the map with customizable styling.
Install it
npx shadcn@latest add https://www.terrae.dev/blur-area.jsonSource
1"use client"23import { useEffect, useRef, useCallback, type CSSProperties } from "react"4import { useMap } from "./hooks"5import type { MapCoordinates } from "./types"67const DEFAULT_BLUR_AMOUNT = 88const DEFAULT_ROUNDED = 09const MIN_AREA_SIZE = 201011type BlurAreaConfig = {12 coordinates: MapCoordinates[]13 blur?: number14 backgroundColor?: string15 rounded?: number | "full"16}1718type MapBlurAreaProps = {19 coordinates?: MapCoordinates[]20 areas?: BlurAreaConfig[]21 blur?: number22 backgroundColor?: string23 rounded?: number | "full"24 blockInteraction?: boolean25}2627type Bounds = {28 minX: number29 maxX: number30 minY: number31 maxY: number32}3334const getBounds = (points: { x: number; y: number }[]): Bounds => {35 const xs = points.map((p) => {36 return p.x37 })38 const ys = points.map((p) => {39 return p.y40 })4142 return {43 minX: Math.min(...xs),44 maxX: Math.max(...xs),45 minY: Math.min(...ys),46 maxY: Math.max(...ys),47 }48}4950type BlurOverlayProps = {51 coordinates: MapCoordinates[]52 blur: number53 backgroundColor?: string54 rounded: number | "full"55 blockInteraction: boolean56}5758const BlurOverlay = ({ coordinates, blur, backgroundColor, rounded, blockInteraction }: BlurOverlayProps) => {59 const { map, isLoaded } = useMap()60 const overlayRef = useRef<HTMLDivElement | null>(null)6162 const updatePosition = useCallback(() => {63 if (!overlayRef.current || !map) {64 return65 }6667 const canvas = map.getCanvas()68 const canvasWidth = canvas.clientWidth69 const canvasHeight = canvas.clientHeight7071 const projectedPoints = coordinates.map((coord) => {72 return map.project(coord)73 })74 const bounds = getBounds(projectedPoints)7576 const width = bounds.maxX - bounds.minX77 const height = bounds.maxY - bounds.minY78 const isVisible =79 bounds.maxX >= 0 &&80 bounds.minX <= canvasWidth &&81 bounds.maxY >= 0 &&82 bounds.minY <= canvasHeight &&83 width >= MIN_AREA_SIZE &&84 height >= MIN_AREA_SIZE8586 if (!isVisible) {87 overlayRef.current.style.opacity = "0"88 } else {89 overlayRef.current.style.opacity = "1"90 overlayRef.current.style.left = `${bounds.minX}px`91 overlayRef.current.style.top = `${bounds.minY}px`92 overlayRef.current.style.width = `${width}px`93 overlayRef.current.style.height = `${height}px`94 }95 }, [map, coordinates])9697 useEffect(() => {98 if (!map || !isLoaded || coordinates.length < 3) {99 return100 }101102 updatePosition()103104// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from terrae
All 40 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Map Animated Circleanimated-circle | terrae | Components | Free | 1 dep | |
| Map Animated Footprintanimated-footprint | terrae | Components | Free | 2 deps | |
| Map Animated Polygonanimated-polygon | terrae | Components | Free | 1 dep | |
| Map Animated Pulseanimated-pulse | terrae | Components | Free | 1 dep | |
| Map Arc Animatedarc-animated | terrae | Components | Free | 1 dep | |
| Map Camera Followcamera-follow | terrae | Components | Free | 1 dep | |
| Map Choroplethchoropleth | terrae | Components | Free | 1 dep | |
| Map Circlecircle | terrae | Components | Free | 1 dep |
