Skip to content

Instantly share code, notes, and snippets.

@ektogamat
Created March 14, 2024 01:22
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ektogamat/8ba8c0d103fa683e7a836661aada55ed to your computer and use it in GitHub Desktop.
Save ektogamat/8ba8c0d103fa683e7a836661aada55ed to your computer and use it in GitHub Desktop.
useGSAP with React Three Fiber Camera Animation
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