juliangarnier / anime

JavaScript animation engine
https://animejs.com
MIT License
50.42k stars 3.68k forks source link

A question from a newbie #830

Open NguyenfromVN opened 2 years ago

NguyenfromVN commented 2 years ago

Hi Anime team,

I am a new person here, I heard about this library when I saw a random Youtube video's title yesterday. I come to the official website and the demo looks interesting. I am considering using this library for my personal portfolio website as currently I am trying to find a way to optimize my animation. My website has some animation and it sometimes feels laggy, especially the grid system with a bunch of cells (when a cell gets touched, it will create a wave from that cell and the wave will travel to the rest of the cells in that grid). Currently, after reducing the number of cells in the grid system and even trying to use will-change for the background-color property, things don't look so smooth though, especially on mobile devices.

So my question is: besides the API you guys write in order to create animation in an easy way, are there any secret sauces (some kind of low-level optimizations) to the library for performance optimization? I think using this library will definitely help to create animation easily but I also need some good optimization too, and if you guys do have some "secret sauces" in it, I am very happy to learn more about this library.

Thank you and look forward to your reply 😁

I also attached a short video about my personal website for your reference (in person, it will feel a bit smoother because of the screen recorder).

rilysh commented 2 years ago

There are several optimizations this library can do, for example, instead of using a custom flattenArray function animejs can use a built-in flat function for array prototypes, see here. However, these functions are mainly for backporting support browsers, especially IE.

Besides this, the library is right now kind of in an unmaintained state. Algorithms that are used here many of them are optimizable. In your case, you can lower down animations for better performance, especially on mobile devices as mobile devices have lower-grade processors compared to desktop/laptop processors. So it's expected to performance degrade in phones.

Oh, also about low-level optimizations...that's not possible with JS but you can use WebAssembly for that.

NguyenfromVN commented 2 years ago

I see, thank you πŸ‘

different55 commented 2 years ago

I wouldn't say it's unmaintained, it's in a state of transition. Check out the activity of the entire repo, not just the main branch, and you'll see the main dev's been very hard at work.

NguyenfromVN commented 2 years ago

@different55 @rilysh I just spent some time reading your code, lib/anime.js, and what I see:

not sure if what I mentioned here is correct, please fix me if there is any misunderstanding. I am trying to understand how you guys can create complex animation that can run well and smoothly, and as I see, taking control and creating every single frame is the key point here (I use CSS and let CSS control the animation by using transition, it works pretty well until there are a lot of elements that need to be updated at the same time, it seems like CSS alone is not good enough to create a smooth animation and requestAnimationFrame, controlling animation using JS to create every single frame, maybe a good solution for me)

yellow1912 commented 2 years ago

I think in most cases if you can do it with CSS then you should.

https://web.dev/css-vs-javascript/

https://web.dev/animations-overview/

https://developer.mozilla.org/en-US/docs/Web/Performance/Animation_performance_and_frame_rate

NguyenfromVN commented 2 years ago

after careful testing, my final solution is to use setInterval to manually control the fps, and it works great though. I "lock" the fps at 20, so if the browser is willing to offer more resources to achieve a higher fps, it will never can to that, and thus other parts of the site can spend those resources freely and lag is no longer a thing on my site, on both PC and mobiles devices πŸ˜‹ 20fps fits my need when it is the sweet point between UX and the machine's resources (24fps can also run well, but the difference in smoothness is super small, and by reducing 4fps, the other parts of the site can run even smoother)

so my conclusion:

anyway, thanks a lot for the support πŸ‘Œ