-
-
Save Kreshnik/7a6393eb8d9b101a3c6cc11a2fab38ee to your computer and use it in GitHub Desktop.
useGSAP with React Three Fiber Camera Animation
This file contains hidden or 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
import { useThree } from '@react-three/fiber' | |
import { gsap } from 'gsap' | |
import { useGSAP } from '@gsap/react' | |
export default function AnimateCamera() { | |
// Accessing camera from Three.js | |
const { camera } = useThree() | |
useGSAP(() => { // The new hook will take care of context and life cicle. | |
gsap.fromTo( // Creates the animation | |
camera.position, // The camera object from useThree | |
{ x: 0, z: 95 }, // Initial position | |
{x: 0, z: 35, // Final position | |
ease: 'power1.out', // Easing function | |
duration: 2, // Duration | |
onUpdate: () => { | |
camera.lookAt(0, 0, 0) // Look to a target during the animation | |
}, | |
} | |
) | |
}, {}) //Dependencies, if you have | |
return <></> //Return an empty fragment | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment