reasonable animation system

This commit is contained in:
2026-03-13 19:42:22 +01:00
parent 166ae50c49
commit e0e32d16e2
7 changed files with 93 additions and 55 deletions

View File

@@ -8,33 +8,43 @@ import { createContext, useCallback, useContext, useRef, useState, type ReactNod
gsap.registerPlugin(useGSAP)
gsap.registerPlugin(ScrollTrigger)
gsap.registerPlugin(SplitText)
const GsapContext = createContext<{ addAnimation: (animation: gsap.core.TimelineChild) => void, resetTimeline: () => void} | null>(null)
const GsapContext = createContext<{
addAnimation: (
animation: gsap.core.TimelineChild,
position: gsap.Position
) => void,
resetTimeline: () => void,
resumeTimeline: () => void
} | null>(null)
export function useGsapContext() {
return useContext(GsapContext)
}
export default function GsapProvider({ children }: { children: ReactNode }) {
const [tl, setTl] = useState<GSAPTimeline | undefined>();
const indexRef = useRef<number>(0)
const tl = useRef<gsap.core.Timeline | null>(null)
const { contextSafe } = useGSAP(() => {
console.log("App effect (create timeline)");
const tl = gsap.timeline();
setTl(() => tl);
}, []);
const addAnimation = useCallback((animation: gsap.core.TimelineChild) => {
indexRef.current += 1;
console.log(indexRef.current)
tl && tl.add(animation, indexRef.current * 2);
}, [tl]);
if (!tl.current) {
tl.current = gsap.timeline({ paused: true })
}
console.log("resuming timeline:",tl.current)
return () => { console.log("gsap cleanup") }
})
const addAnimation = useCallback((animation: gsap.core.TimelineChild, position: gsap.Position) => {
console.log("add animation to:", position)
tl.current?.add(animation, position);
},[])
const resetTimeline = useCallback(() => {
const tl = gsap.timeline();
setTl(() => tl)
indexRef.current = 0;
},[tl])
tl.current?.kill()
tl.current?.revert()
tl.current = gsap.timeline({paused:true})
},[])
const resumeTimeline = useCallback(() => {
tl.current?.resume()
},[])
return (
<GsapContext.Provider value={{ addAnimation, resetTimeline }}>
<GsapContext.Provider value={{ addAnimation, resetTimeline,resumeTimeline }}>
{children}
</GsapContext.Provider>
)