geener Posted June 12 Share Posted June 12 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 Link to comment Share on other sites More sharing options...
mvaneijgen Posted June 12 Share Posted June 12 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 More sharing options...
geener Posted June 12 Author Share Posted June 12 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 More sharing options...
mvaneijgen Posted June 12 Share Posted June 12 2 minutes ago, geener said: moving all the code to gsap.context didn't help. The code you've linked has not been updated to show this. But, someone will jump in here shortly with more React knowledge! Link to comment Share on other sites More sharing options...
geener Posted June 12 Author Share Posted June 12 2 minutes ago, mvaneijgen said: The code you've linked has not been updated to show this. But, someone will jump in here shortly with more React knowledge! Sorry, I already updated this codehttps://codesandbox.io/s/optimistic-poitras-cym566?file=/src/cube.tsx Link to comment Share on other sites More sharing options...
Solution Rodrigo Posted June 12 Solution Share Posted June 12 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! 1 Link to comment Share on other sites More sharing options...
geener Posted June 13 Author Share Posted June 13 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 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now