feat: rebuild the orbital vault as an operational map
This commit is contained in:
@@ -1,34 +1,25 @@
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useLayoutEffect } from "react";
|
||||
|
||||
/** Seconds since mount, frozen while paused. */
|
||||
export function useGalaxyTime(paused: boolean): number {
|
||||
const [t, setT] = useState(0);
|
||||
const start = useRef(performance.now());
|
||||
const frozen = useRef(0);
|
||||
const wasPaused = useRef(paused);
|
||||
|
||||
useEffect(() => {
|
||||
if (paused) {
|
||||
frozen.current = (performance.now() - start.current) / 1000;
|
||||
wasPaused.current = true;
|
||||
return;
|
||||
}
|
||||
if (wasPaused.current) {
|
||||
start.current = performance.now() - frozen.current * 1000;
|
||||
wasPaused.current = false;
|
||||
}
|
||||
let raf = 0;
|
||||
const tick = () => {
|
||||
setT((performance.now() - start.current) / 1000);
|
||||
raf = requestAnimationFrame(tick);
|
||||
export function useGalaxyMotion(active: boolean, draw: (elapsed: number) => boolean) {
|
||||
useLayoutEffect(() => {
|
||||
let frame = 0;
|
||||
let previous: number | undefined;
|
||||
const tick = (now: number) => {
|
||||
const keepDrawing = draw(previous === undefined ? 0 : (now - previous) / 1000);
|
||||
previous = now;
|
||||
if (keepDrawing) frame = requestAnimationFrame(tick);
|
||||
};
|
||||
raf = requestAnimationFrame(tick);
|
||||
return () => cancelAnimationFrame(raf);
|
||||
}, [paused]);
|
||||
|
||||
return paused ? frozen.current : t;
|
||||
}
|
||||
|
||||
export function easeOutCubic(x: number): number {
|
||||
return 1 - Math.pow(1 - x, 3);
|
||||
const sync = () => {
|
||||
cancelAnimationFrame(frame);
|
||||
previous = undefined;
|
||||
if (!document.hidden && active) frame = requestAnimationFrame(tick);
|
||||
};
|
||||
draw(0);
|
||||
sync();
|
||||
document.addEventListener("visibilitychange", sync);
|
||||
return () => {
|
||||
cancelAnimationFrame(frame);
|
||||
document.removeEventListener("visibilitychange", sync);
|
||||
};
|
||||
}, [active, draw]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user