Jump to content
Search Community

Best practice for particles animation

cecileRX test
Moderator Tag

Recommended Posts

Hello, I have a SVG file on my home page with about 500 dots that I animate randomly as particles, using this basic GSAP tween:

gsap.to('.particule', {
duration: 200,
y: "random(-200,200, 5)",
x: "random(-200, 200, 5)",
repeat: -1
});

 

Is it too demanding for the user resources or is it the right way to do it? Thanks!

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

If it works it works. Maybe check if you're on a smaller screen and draw less elements, but I don't see why this couldn't work. I don't see any slow down on my end. Please include a minimal demo next time, saves anyone time wanting to jump in and it really helps us help you! Hope it helps and happy tweening! 

 

See the Pen mdGdrNJ by mvaneijgen (@mvaneijgen) on CodePen

  • Like 1
Link to comment
Share on other sites

It works, right? 

 

I would make sure you use only the v3 syntax and not the old TweenMax syntax. And you can always optimize the SVG it self and bundle some of the dots to one shape and instead of animating 500 individual dots make clusters of 100 dots each, flatten them in your design program of choice and animate those 5 clusters, but it seems to not slowdown, so why fix an issue that is not there.  

 

See the Pen wvEvgjN?editors=0010 by mvaneijgen (@mvaneijgen) on CodePen

Link to comment
Share on other sites

I personally would never use an infinite repeat on an SVG animation. Even on canvas animations looping something over and over is going to get a bit resource intensive.

Of course that entirely depends on *what* the use case is. I made some animations that were displayed on a screen at an event, they only ran in chrome and on a very fast machine.

Whereas if you're infinitely looping a big SVG animation for web use and people are going to be viewing it on low end mobile devices that's a different story 🫠

If it's on a webpage, my advice would be to stop the animation playing as it scrolls out of view and start the animation as it comes into view again. That will help.

In terms of debugging these are the two most useful options in chrome dev tools
   

 




 

  • Like 1
Link to comment
Share on other sites

Those green flashing areas aren't good, they're triggering repaints,

Chrome and firefox actually allow hardware acceleration on SVG elements nowadays, I actually thought it did it automatically (it does with CSS animations) so I was a bit surprised to see the green flashes, but it looks like if you add 'will-change: transform' in your CSS it stops it triggering paint

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

Link to comment
Share on other sites

There used to be a bit of hesitancy about overuse of will-change, but webflow whacks it on everything and tests I've done recently don't seem to show any issues. So I reckon you can sprinkle it on your SVG elements quite happily to boost your perf a little!

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