-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
308c2e1
commit 01d5fe8
Showing
3 changed files
with
78 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
import { gstime } from "satellite.js"; | ||
import type { Satellite } from "./Satellite"; | ||
import { SatelliteMarker } from "./SatelliteMarker"; | ||
|
||
type SatelliteMarkersProps = { | ||
satellites: Satellite[]; | ||
}; | ||
|
||
export function SatelliteMarkers({ satellites }: SatelliteMarkersProps) { | ||
const [date, setDate] = useState<Date>(new Date(performance.timeOrigin)); | ||
|
||
const updateDate = useCallback((timestamp: DOMHighResTimeStamp) => { | ||
setDate(new Date(performance.timeOrigin + timestamp)); | ||
}, []); | ||
useAnimationFrame(updateDate); | ||
|
||
const gmst = gstime(date); | ||
|
||
return ( | ||
<> | ||
{satellites.map((satellite) => ( | ||
<SatelliteMarker | ||
key={satellite.name} | ||
satellite={satellite} | ||
date={date} | ||
gmst={gmst} | ||
/> | ||
))} | ||
</> | ||
); | ||
} | ||
|
||
// https://bom-shibuya.hatenablog.com/entry/2020/10/27/182226 | ||
const useAnimationFrame = ( | ||
callback: (timestamp: DOMHighResTimeStamp) => void, | ||
) => { | ||
const reqIdRef = useRef<number>(null); | ||
|
||
const animate = useCallback( | ||
(timestamp: DOMHighResTimeStamp) => { | ||
reqIdRef.current = requestAnimationFrame(animate); | ||
callback(timestamp); | ||
}, | ||
[callback], | ||
); | ||
|
||
useEffect(() => { | ||
reqIdRef.current = requestAnimationFrame(animate); | ||
return () => { | ||
if (reqIdRef.current) { | ||
cancelAnimationFrame(reqIdRef.current); | ||
} | ||
}; | ||
}, [animate]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export type { Satellite } from "./Satellite"; | ||
export { SatelliteMarker } from "./SatelliteMarker"; | ||
export { SatelliteMarkers } from "./SatelliteMarkers"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters