simeydotme / sparticles

JavaScript Particles in Canvas ~~ Fast, Lightweight, High Performance.
https://sparticlesjs.dev
Mozilla Public License 2.0
251 stars 20 forks source link

Change `count` logic to `density` logic #25

Open cyrilchapon opened 1 year ago

cyrilchapon commented 1 year ago

Hi,

Reading the warning about mobile performance; it came to me the idea that count is practically a nonsense in a resizable environment.

When implementing "snow" effect for example; what you want is a constant visual density, instead of a constant "number of flakes". Thus, I implemented a new density logic. I defaulted this to keep the same count as of today on a classic fullscreen desktop container (1400 * 660).

It will recalculate the count, which is now dynamic based on density x resolution.

It's also less dangerous on mobile; based on the naïve principle of "a larger screen resolution basically often mean a stronger GPU calculation capacities".


This is a breaking change, non backward compatible

simeydotme commented 1 year ago

hi @cyrilchapon , I understand and like this modification ... however, what if we didn't make it breaking/incompatible?

if I'm not mistaken, this line;

this.count = Math.ceil((this.resolution / BASE_RESOLUTION) * this.settings.density);

could be changed to;

this.count = Math.ceil(
  typeof this.settings.density !== "undefined" ? 
    this.resolution / BASE_RESOLUTION * this.settings.density : 
    this.settings.count
  );

which would allow for anyone using count to continue doing so?

cyrilchapon commented 1 year ago

Hey @simeydotme,

This is not stupid at all; I'd slightly change it though, maybe making (and documenting) the opposite (density prioritized over count), to make density the new-normal; but keep backward compatibility with count. Maybe marking count deprecated in the code, but still working.

(the reason being simple : density being way less dangerous than count for lower resolution, despite the warning on homepage)

simeydotme commented 1 year ago

I wrote that quite late, but I think it is doing just as you propose; if density is undefined, then use count. but if density is defined; use density. and all the other code (default density, and document density) remains. so new users will be using density, and old users will have no problems.