Jump to content
Search Community

Use GSAP to animate squares between two section using React

alpaca test
Moderator Tag

Recommended Posts

Hello, it's my first time I use Gsap so I'ma bit confused. I followed this video tutorial that was really good.

 

My goal is create a site in React composed (at the moment) of only two "sections": each section should fill the entire browser window.

 

Below the design:


image.thumb.png.153d402e4d0e749859ffee3f4f85fb24.png

As you can see, the first section contains a button (next) in the center of the page and some squares (some in red and other in orange). The squares should move (and rotate) in loop with a smooth, random and natural way.
I immagine that each square moves very slowly and when it reaches the border of the screen, it continues its movement in the opposite direction.

 

When the user clicks on the next button, it goes to the second section.
The transition between the first and the second section should be something like this:

  • orange squares go to opacity = 0
  • next button goes to opacity = 0
  • red squares continue to move
  • in the meantime some other red squares appear from the outside of the screen and from opacity 0 goes to opacity 1
  • also a label (alpaca) appears.

Ok, now we are at section #2.
Here squares moves as before but their space to move is smaller then before so they are closer to the center of the screen.

This seems too complicate to me so I need some suggestions before start to write code:

  • there is a similar example (codesandbox, codepen, ...) I can look at that can help me?
  • the site can be a single page application, I don't necessary need to use routes so section#1 and section#2 can be both on the same route (for example mysite.com/test)
  • how can I handle animations and transitions with gsap?

This is my code at the moment: https://codesandbox.io/p/sandbox/gsap-animation-4qcwxn?file=%2Fsrc%2FApp.js.
It doesn't work well. 
First of all the squares animation it's very strange, second of all I didn't know how to animate squares between first and second section.

Can someone help me?

Thanks a lot!
 

Link to comment
Share on other sites

Hi there! I see you're using React -

Proper cleanup is very important with frameworks, but especially with React. React 18 runs in strict mode locally by default which causes your useEffect() and useLayoutEffect() to get called TWICE.

 

Since GSAP 3.12, we have the useGSAP() hook (the NPM package is here) that simplifies creating and cleaning up animations in React (including Next, Remix, etc). It's a drop-in replacement for useEffect()/useLayoutEffect(). All the GSAP-related objects (animations, ScrollTriggers, etc.) created while the function executes get collected and then reverted when the hook gets torn down.

 

Here is how it works:

const container = useRef(); // the root level element of your component (for scoping selector text which is optional)

useGSAP(() => {
  // gsap code here...
}, { dependencies: [endX], scope: container }); // config object offers maximum flexibility

Or if you prefer, you can use the same method signature as useEffect():

useGSAP(() => {
  // gsap code here...
}, [endX]); // simple dependency Array setup like useEffect()

This pattern follows React's best practices.

 

We strongly recommend reading the React guide we've put together at https://gsap.com/resources/React/

 

If you still need help, here's a React starter template that you can fork to create a minimal demo illustrating whatever issue you're running into. Post a link to your fork back here and we'd be happy to take a peek and answer any GSAP-related questions you have. Just use simple colored <div> elements in your demo; no need to recreate your whole project with client artwork, etc. The simpler the better. 

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