shuding / cobe

5kB WebGL globe lib.
https://cobe.vercel.app
MIT License
3.15k stars 169 forks source link

Increased recalculations per second (+CPU usage etc.) when used with other animations #67

Closed kevinmitch14 closed 1 year ago

kevinmitch14 commented 1 year ago

When using COBE alone on my pages, everything works well.

Screenshot 2023-04-04 at 11 21 53

However when I use COBE along with a marquee animation, performance really suffers. Recalcs/second is 60 continuously and CPU usage is pretty high.

Screenshot 2023-04-04 at 11 23 19

The problem only happens when using both together, when I use either alone, there is no problems with perf. Is this behaviour expected?

The same issue can be seen on https://vercel.com/ homepage when scrolling down to the COBE implementation, recalcs are at 60 per second and CPU usage is high.

Screenshot 2023-04-04 at 11 29 52

However when I remove the marquee animation from the DOM on https://vercel.com/, we go back to 0 recalcs per second.

Screenshot 2023-04-04 at 11 32 22

The desired performance can also be found on https://dub.sh/ implementation without making any changes.

I know it is possible to conditionally use the globe with Intersection Observer but it seems weird that the issues only happen when there are other animations on the page. Any help would be greatly appreciated!

Mayank-Sh07 commented 1 year ago

I've experienced the same issue while testing a site on a mid-range windows laptop (Intel iris integrated graphics). The webpage works fine on a Macbook Pro (much more powerful GPU) but there's a lot of jitter on the windows laptop. The globe also seems to disappear after a flash of black once I scroll down to the section of the webpage which contains the globe. I'm using around 2-3 infinite animations on the same page (marquee and rotate).

shuding commented 1 year ago

This is a Chrome bug unfortunately: https://bugs.chromium.org/p/chromium/issues/detail?id=1252311

Basically if you have RequestAnimationFrame and CSS3 animations together on a page, a style recalc will be triggered on every frame. I'm afraid that this lib can do nothing about it — one workaround is to disable CSS animations when the canvas is on the viewport, or vice versa.