Jump to content
Search Community

Scrub animation works only when markers set to true

geener test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

I want to implement an animation that would collect parts of the cube on a scroll. The farther you scroll, the more faces of the cube joined. I encountered a problem that the animation works only if markers: true. I read a thread on a similar case, it said that the parent has flex or that the height is not 100vh, but none of that worked for me.
In the case where markers: false, the animation works until the first edge, and then the scroll works as normal, not as scrub. What are some other options to solve the problem?

The link with the current problem: https://codesandbox.io/s/optimistic-poitras-cym566?file=/src/cube.tsx

2023-06-12_13-15-12.png

Link to comment
Share on other sites

Hi @geener welcome to the forum!

 

Have you by any chance read the following article, there are a lot of tips in there how best to use GSAP and in turn ScrollTrigger. I don't use React, but what I can see is that some of your GSAP code is outside the gsap.context() which can cause issues. Hope it helps and happy tweening! And if this does not fix the issue someone with more React knowledge will come by shortly!

 

Link to comment
Share on other sites

8 minutes ago, mvaneijgen said:

Hi @geener welcome to the forum!

 

Have you by any chance read the following article, there are a lot of tips in there how best to use GSAP and in turn ScrollTrigger. I don't use React, but what I can see is that some of your GSAP code is outside the gsap.context() which can cause issues. Hope it helps and happy tweening! And if this does not fix the issue someone with more React knowledge will come by shortly!

 

Unfortunately, moving all the code to gsap.context didn't help. My animation is still limited to attaching only one face and stops there

Link to comment
Share on other sites

  • Solution

Hi,

 

The main issue here seems to stem from the fact that you're creating two ScrollTrigger instances for each mesh and that onUpdate callback in every ScrollTrigger instance. Why not just use the onLeave callback and be done with it?

onLeave: () => gsap.to(mesh.material, {
  opacity: 0.26
}),

Just create one timeline for each mesh and create one timeline and using the position parameter make them start at the same time, and use the ScrollTrigger config on the timeline. This seems to work the way you intend:

const baseUpdate = ({ mesh, start, end }: BaseUpdate) => {
  gsap.timeline({
    scrollTrigger: {
      trigger: contextRef.current,
      pin: contextRef.current,
      // markers: true, // animation works correctly only when markers is set to true
      scrub: 0.5,
      start,
      end,
      onLeave: () => gsap.to(mesh.material, {
        opacity: 0.26
      }),
    },
  })
    .to(mesh.material, {
    opacity: 1,
  })
    .to(mesh.position, {
    x: 0,
    y: 0,
    z: 0
  }, "<");
};

https://codesandbox.io/s/strange-montalcini-jrjs5h?file=/src/cube.tsx

 

Hopefully this helps.

Happy Tweening!

  • Like 1
Link to comment
Share on other sites

10 hours ago, Rodrigo said:

Hi,

 

The main issue here seems to stem from the fact that you're creating two ScrollTrigger instances for each mesh and that onUpdate callback in every ScrollTrigger instance. Why not just use the onLeave callback and be done with it?

onLeave: () => gsap.to(mesh.material, {
  opacity: 0.26
}),

Just create one timeline for each mesh and create one timeline and using the position parameter make them start at the same time, and use the ScrollTrigger config on the timeline. This seems to work the way you intend:

const baseUpdate = ({ mesh, start, end }: BaseUpdate) => {
  gsap.timeline({
    scrollTrigger: {
      trigger: contextRef.current,
      pin: contextRef.current,
      // markers: true, // animation works correctly only when markers is set to true
      scrub: 0.5,
      start,
      end,
      onLeave: () => gsap.to(mesh.material, {
        opacity: 0.26
      }),
    },
  })
    .to(mesh.material, {
    opacity: 1,
  })
    .to(mesh.position, {
    x: 0,
    y: 0,
    z: 0
  }, "<");
};

https://codesandbox.io/s/strange-montalcini-jrjs5h?file=/src/cube.tsx

 

Hopefully this helps.

Happy Tweening!

Thank you very much, your solution helped

  • Like 1
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...