Jump to content
Search Community

How to use gsap and canvas in React?

Romanus test
Moderator Tag

Recommended Posts

Hello! I am learning Canvas. Made a simple example using gsap.ticker.

A cube is a separate entity with its own behavior pattern. How can I create many cubes from this template?

I don't understand how to separate the code. I have seen examples using classes. Is it ok to use classes in functional React components? How would you advise me to proceed?

 

I also don't understand, should I call gsap.ticker once, or should each cube call its own render?

See the Pen XWPmZmR?editors=0010 by romanusname (@romanusname) on CodePen

Link to comment
Share on other sites

Hi,

 

I'm not really an expert in canvas so I can't really give you a lot of pointers without diving into your example.

 

Me recommendation would be to make this work in Vanilla JS first and then port it into a React.

 

Finally I don't think this is the best approach:

<main>
  <Player startDirection={1} canvasRef={canvasRef}/>
  <Player startDirection={2} canvasRef={canvasRef}/>
  <canvas width={500} height={500} ref={canvasRef}></canvas>
</main>

Creating two components just for calling a drawing method as in this case, just create a method that accepts the start direction in the same component and you'll get access to the ref there.

 

14 hours ago, Romanus said:

I also don't understand, should I call gsap.ticker once, or should each cube call its own render?

Yep, no need to add the render method to the ticker more than once.

 

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