Jump to content
Search Community

Transparency reset during scrub animation

geener test
Moderator Tag

Recommended Posts

I have a problem with three js and gsap animation, its logic is that as I scroll, the faces of the cube are attached to the cube itself. when you attach a face to the cube, the face becomes for a while with 100% transparency. The problem is that let's say in production when I update the page in the section below the section with the animation I described above, all of the faces of my cube become opaque as if they are simultaneously joined to the cube, what can this be about and how can it be fixed? now will send my code in the next message

animation: 
 

useIsomorphicLayoutEffect(() => {
    if (!width) return;
    if (width >= 1200) {
      const changeOpacity = (props: ChangeOpacity) => {
        const {
          variant = "increase",
          selector,
          start = "+=75",
          end = `+=${window.innerHeight}`,
        } = props;

        if (variant === "increase") {
          return gsap.to(
            {},
            {
              scrollTrigger: {
                trigger: contextRef.current,
                scrub: 0.1,
                start,
                end,
                onUpdate: (self) => {
                  const progress = self.progress;
                  gsap.to(selector, {
                    opacity: progress * 2,
                    duration: 0,
                  });
                },
              },
            }
          );
        }

        return gsap.to(
          {},
          {
            scrollTrigger: {
              trigger: contextRef.current,
              scrub: 0.1,
              start,
              end,
              onUpdate: () => {
                gsap.to(selector, {
                  opacity: 0,
                  duration: 0,
                });
              },
            },
          }
        );
      };
      const ctx = gsap.context(() => {
        changeOpacity({
          selector: ".first-panel",
          start: `+=75`,
        });
        changeOpacity({
          variant: "decrease",
          selector: ".first-panel",
          start: `+=${75 + window.innerHeight + 75}`,
        });

        changeOpacity({
          selector: ".second-panel",
          start: `+=${window.innerHeight}`,
        });
        changeOpacity({
          variant: "decrease",
          selector: ".second-panel",
          start: `+=${window.innerHeight * 2}`,
        });

        changeOpacity({
          selector: ".third-panel",
          start: `+=${window.innerHeight * 2 + 75}`,
        });
        changeOpacity({
          variant: "decrease",
          selector: ".third-panel",
          start: `+=${window.innerHeight * 3}`,
        });

        changeOpacity({
          selector: ".fourth-panel",
          start: `+=${window.innerHeight * 3 + 75}`,
        });
        changeOpacity({
          variant: "decrease",
          selector: ".fourth-panel",
          start: `+=${window.innerHeight * 4}`,
        });

        changeOpacity({
          selector: ".fifth-panel",
          start: `+=${window.innerHeight * 4 + 75}`,
        });
        changeOpacity({
          variant: "decrease",
          selector: ".fifth-panel",
          start: `+=${window.innerHeight * 5}`,
        });

        changeOpacity({
          selector: ".sixth-panel",
          start: `+=${window.innerHeight * 5 + 75}`,
        });
      }, contextRef);
      return () => ctx.revert();
    }
  }, [width, contextRef]);



codesandbox: https://codesandbox.io/p/sandbox/sharp-fast-j625lw

problem: image.thumb.png.cfa848aea0fb285a54bbbeb6fb11ba30.png

must be like this :
image.thumb.png.e42703a0f862544af253550836bf9983.png

Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or CodeSandbox that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

If you're using something like React/Next/Vue/Nuxt or some other framework, you may find StackBlitz easier to use. We have a series of collections with different templates for you to get started on these different frameworks: React/Next/Vue/Nuxt.

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

2 hours ago, GSAP Helper said:

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or CodeSandbox that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

 

If you're using something like React/Next/Vue/Nuxt or some other framework, you may find StackBlitz easier to use. We have a series of collections with different templates for you to get started on these different frameworks: React/Next/Vue/Nuxt.

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

but I provided link to codesandbox
codesandbox: https://codesandbox.io/p/sandbox/sharp-fast-j625lw

Link to comment
Share on other sites

Hi @geener,

 

I see it working as expected on my end, just fix the final element's end point, maybe I'm missing something 🤷‍♂️

https://i.imgur.com/Vj98Cct.mp4

 

Also this function should return the GSAP Timeline instance being created so GSAP Context can revert it. Right now those timelines are being created outside GSAP Context so they're not reverted during the cleanup phase:

const baseUpdate = ({ mesh, start, end }: BaseUpdate) => {
  const tl = gsap
  .timeline({
    scrollTrigger: {
      trigger: contextRef.current,
      pin: contextRef.current,
      scrub: 0.5,
      start,
      end,
      markers: true,
      onLeave: () =>
      gsap.to(mesh.material, {
        opacity: 0.25,
      }),
    },
  })
  .to(mesh.material, {
    opacity: 1,
  })
  .to(
    mesh.position,
    {
      x: 0,
      y: 0,
      z: 0,
    },
    "<"
  );
  // Return the Timeline instance so GSAP Context can revert it during cleanup!
  return tl;
};

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

4 hours ago, Rodrigo said:

Hi @geener,

 

I see it working as expected on my end, just fix the final element's end point, maybe I'm missing something 🤷‍♂️

https://i.imgur.com/Vj98Cct.mp4

 

Also this function should return the GSAP Timeline instance being created so GSAP Context can revert it. Right now those timelines are being created outside GSAP Context so they're not reverted during the cleanup phase:

const baseUpdate = ({ mesh, start, end }: BaseUpdate) => {
  const tl = gsap
  .timeline({
    scrollTrigger: {
      trigger: contextRef.current,
      pin: contextRef.current,
      scrub: 0.5,
      start,
      end,
      markers: true,
      onLeave: () =>
      gsap.to(mesh.material, {
        opacity: 0.25,
      }),
    },
  })
  .to(mesh.material, {
    opacity: 1,
  })
  .to(
    mesh.position,
    {
      x: 0,
      y: 0,
      z: 0,
    },
    "<"
  );
  // Return the Timeline instance so GSAP Context can revert it during cleanup!
  return tl;
};

Hopefully this helps.

Happy Tweening!

Unfortunately, the solution did not work
Maybe you can understand what causes this behavior like in the video?
In the video when the screen blinked twice, it was me reloading the page twice
https://streamable.com/u63in7
 

Link to comment
Share on other sites

Hi,

 

In this example that I forked from your original sandbox, I can see the transparency being applied to all the faces, except the last (due to the end point calculation as I mentioned previously), as shown in the video I shared in my previous post.

 

But yeah this is a different issue. The animation is happening as you scroll down and is scrubbing as expected, the problem happens when you reload the page. ScrollTrigger should get the scroll position and update the progress of all the animations tied to a ScrollTrigger instance. So that particular behaviour seems odd to me. You could log the onEnter and onLeave callbacks and check if those instances are actually running.

 

In this example you can see that ScrollTrigger is updating the animation to where is supossed to be:

https://stackblitz.com/edit/vitejs-vite-hebtj7?file=src%2Findex.css,src%2FApp.jsx&terminal=dev

 

Unfortunately the codesandbox example always takes the scroll back to zero, so there is no way for us to know how that is actually working.

 

Hopefully this helps.

Happy Tweening!

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...