Galaxy vault: black hole scene with flow/lock interaction
Stars per orbit, snapshot moons when locked, toggle on singularity.
This commit is contained in:
34
web/src/hooks/useGalaxyMotion.ts
Normal file
34
web/src/hooks/useGalaxyMotion.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { useEffect, useRef, useState } 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);
|
||||
};
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user