Map Raster Video
terrae/raster-videoFreeComponent
Video overlay layers on the map.
Install it
npx shadcn@latest add https://www.terrae.dev/raster-video.jsonSource
1"use client"23import { useEffect, useRef, useState } from "react"4import mapboxgl from "mapbox-gl"5import { useMap } from "./hooks"6import type { MapImageCorners } from "./types"78type MapRasterVideoProps = {9 id: string10 urls: string[]11 coordinates: MapImageCorners12 opacity?: number13 autoplay?: boolean14 loop?: boolean15 muted?: boolean16}1718export const MapRasterVideo = ({19 id,20 urls,21 coordinates,22 opacity = 1,23 autoplay = false,24 loop = true,25 muted = true,26}: MapRasterVideoProps) => {27 const { map, isLoaded } = useMap()28 const initializedRef = useRef(false)2930 useEffect(() => {31 if (!isLoaded || !map || initializedRef.current) return3233 try {34 // Add video source35 map.addSource(id, {36 type: "video",37 urls,38 coordinates,39 } as mapboxgl.VideoSourceSpecification)4041 // Add raster layer42 map.addLayer({43 id,44 type: "raster",45 source: id,46 paint: {47 "raster-opacity": opacity,48 },49 })5051 // Set video properties and control playback52 const source = map.getSource(id) as mapboxgl.VideoSource53 if (source && source.getVideo) {54 const video = source.getVideo()55 if (video) {56 video.loop = loop57 video.muted = muted5859 if (autoplay) {60 source.play()61 }62 }63 }6465 initializedRef.current = true66 } catch (error) {67 console.error("Error adding raster video:", error)68 }6970 return () => {71 if (map && map.getStyle()) {72 try {73 if (map.getLayer(id)) {74 map.removeLayer(id)75 }76 if (map.getSource(id)) {77 map.removeSource(id)78 }79 } catch (error) {80 console.error("Error cleaning up raster video:", error)81 }82 }83 initializedRef.current = false84 }85 }, [map, isLoaded, id, urls, coordinates, opacity, autoplay, loop, muted])8687 return null88}8990export const useRasterVideoControl = (layerId: string) => {91 const { map } = useMap()92 const [isPlaying, setIsPlaying] = useState(false)9394 const play = () => {95 if (!map) return96 const source = map.getSource(layerId) as mapboxgl.VideoSource97 source?.play?.()98 setIsPlaying(true)99 }100101 const pause = () => {102 if (!map) return103 const source = map.getSource(layerId) as mapboxgl.VideoSource104 source?.pause?.()105 setIsPlaying(false)106 }107108 const toggle = () => {109 if (isPlaying) {110 pause()111 } else {112// … 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 |
