Jump to content
Search Community

gsap.from() saving initial value and using it

sandeepdev test
Moderator Tag

Recommended Posts

import React, { useRef } from 'react'
import { useGLTF } from '@react-three/drei'
import gsap from 'gsap'
import { useGSAP } from '@gsap/react';
 
export function BeerCan(props) {
 
  const { nodes, materials } = useGLTF('/beercannew.glb')
  const canRef = useRef(null)
 
  useGSAP(() => {
    let ctx = gsap.context(() => {
      gsap.from(canRef.current.position, { y: 10, duration: 1, })
 
    })
    return () => ctx.revert()
 
  }, [])
  return (
    <group ref={canRef} position={[-0.829, 4.1, -4.274]} rotation={[-0.1, 1.35, 0]} scale={[1.11, 2.0, 1.11]}>
      <mesh geometry={nodes.Cylinder_1.geometry} material={materials['top of can']} />
      <mesh geometry={nodes.Cylinder_2.geometry} material={materials.image} />
    </group>
  )
}
 
useGLTF.preload('/beercannew.glb'),once i save it ,it works well,but after i reload my page it takes previous position as initial position
Link to comment
Share on other sites

I'm not quite sure what you're asking, but you don't need to use a context inside the useGSAP() hook - it does that for you:

// BAD
useGSAP(() => {
  let ctx = gsap.context(() => {
    gsap.from(canRef.current.position, { y: 10, duration: 1, })
  });
  return () => ctx.revert()
}, []);

// GOOD
useGSAP(() => {
  gsap.from(canRef.current.position, { y: 10, duration: 1, })
});

If you still need help, please provide a minimal demo, like in Stackblitz. Here's a starter template you can fork: 

https://stackblitz.com/edit/gsap-react-basic-f48716

Link to comment
Share on other sites

I looked at your demo and I refreshed the page a bunch of times and I don't see any problems. I'm sure I'm missing something - how do I replicate the issue? Can you please be very specific about the steps to reproduce? 

 

Also, it's a good idea to register the useGSAP hook before you use it:

gsap.registerPlugin(useGSAP);

 

Link to comment
Share on other sites

I don't think that's a GSAP issue - it might be a rendering issue with your 3D tool. If I log out the actual values that GSAP is setting, they're all correct: 

useGSAP(() => {
    console.log("original", canRef.current.position.y);
    gsap.from(canRef.current.position, { 
      y: 10, 
      duration: 1,
      onUpdate() {
        console.log("update", canRef.current.position.y);
      },
      onComplete() {
        console.log("final", canRef.current.position.y);
      }
    })
  })

 

Interestingly, if you set immediateRender: false, and delay: 0.01, it seems to work. So there's something about the way your 3D stuff initially renders that's throwing things off. 🤷‍♂️

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...