Map Lightning
terrae/lightningFreeComponent
Animated lightning bolt strikes with branching and flash effects.
Install it
npx shadcn@latest add https://www.terrae.dev/lightning.jsonSource
1"use client"23import { useEffect, useRef, useState } from "react"4import { useMap } from "./hooks"5import type { MapCoordinates } from "./types"67type RgbColor = {8 red: number9 green: number10 blue: number11}1213type LightningBranch = {14 startX: number15 startY: number16 endX: number17 endY: number18 width: number19 opacity: number20}2122type LightningBolt = {23 segments: LightningBranch[]24 flashOpacity: number25 life: number26 maxLife: number27}2829type LightningControl = {30 strike: () => void31 isActive: boolean32}3334type LightningRenderer = {35 width: number36 height: number37 data: Uint8ClampedArray38 context?: CanvasRenderingContext2D39 bolts: LightningBolt[]40 isActive: boolean41 lastStrikeTime: number42 onAdd: () => void43 render: () => boolean44 strike: () => void45}4647type MapLightningProps = {48 id: string49 coordinates: MapCoordinates50 size?: number51 boltColor?: string52 flashColor?: string53 flashIntensity?: number54 autoStrike?: boolean55 strikeInterval?: number56 boltWidth?: number57 branchProbability?: number58}5960const DEFAULT_SIZE = 20061const DEFAULT_BOLT_COLOR = "#ffffff"62const DEFAULT_FLASH_COLOR = "#e0e8ff"63const DEFAULT_FLASH_INTENSITY = 0.664const DEFAULT_STRIKE_INTERVAL = 400065const DEFAULT_BOLT_WIDTH = 466const DEFAULT_BRANCH_PROBABILITY = 0.36768const MIN_STRIKE_INTERVAL = 50069const BOLT_LIFETIME = 870const FLASH_DECAY_RATE = 0.571const SEGMENT_MIN_LENGTH = 2072const SEGMENT_MAX_LENGTH = 4073const SEGMENT_OFFSET_RANGE = 3074const BRANCH_LENGTH_FACTOR = 0.575const BRANCH_WIDTH_FACTOR = 0.576const BRANCH_ANGLE_RANGE = 6077const GLOW_BLUR_AMOUNT = 2078const CORE_GLOW_BLUR = 1079const DEGREES_TO_RADIANS = Math.PI / 18080const PIXEL_RATIO = 281const CONTROL_UPDATE_INTERVAL = 5082const MAX_BRANCH_DEPTH = 383const BRANCH_OPACITY_DECAY = 0.284const WIDTH_DECAY_FACTOR = 0.0585const FLASH_THRESHOLD = 0.0186const OUTER_GLOW_WIDTH_MULTIPLIER = 387const OUTER_GLOW_OPACITY_MULTIPLIER = 0.58889const lightningControls = new Map<string, LightningControl>()9091export const useLightningControl = (id: string): LightningControl | null => {92 const [, forceUpdate] = useState(0)9394 useEffect(() => {95 const interval = setInterval(() => {96 forceUpdate((previous) => {97 return previous + 198 })99 }, CONTROL_UPDATE_INTERVAL)100101 return () => {102 clearInterval(interval)103 }104 }, [])105106 return lightningControls.get(id) || null107}108109const hexToRgb = (hex: string): RgbColor => {110 const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)111112 if (!result) {113// … 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 Blur Areablur-area | terrae | Components | Free | 1 dep | |
| Map Camera Followcamera-follow | terrae | Components | Free | 1 dep | |
| Map Choroplethchoropleth | terrae | Components | Free | 1 dep |
