Handling particles beyond canvas bounds
Particles are only drawn if they are within the visible area of the canvas, accounting for their size.
Particles are considered to still be visible if their edges are within the canvas bounds,
preventing large particles from disappearing when their center crosses the edge.
When particles move beyond the floating area bounds, they will teleport to the opposite side.
Handling particle connections beyond canvas bounds
The floating area for the particles is larger than the canvas bounds to ensure no lines between particles
will suddenly dissapear near the edges. The floating space added to all 4 sides is exactly equal to the
maximum length of a line, defined with options.particles.connectDist.
By default, a line is not drawn between particles if both are outside the visible canvas area.
Each particle's grid position is calculated based on its location relative to the visible canvas area. This
allows for determining whether two particles, even when outside the canvas, should have a connecting line
drawn between them because part of it can still be seen.
Performance optimizations
Canvas Particles JS is a highly optimized particle renderer (ignoring the fact that it's coded in JS).
A lot of time has gone into profiling the code and improving efficiency on expensive tasks.
An IntersectionObserver is used to pause and resume the animation based on
if the canvas is within the viewbox
Particles smaller than one pixel are drawn as squares instead of circles which is ±183% faster.
Dynamically generating the fillStyle combinations for each connection in
every frame is the most expensive task. It's about 5x faster to precomputing all 256 particle colors,
accounting only for varying opacity levels, and storing them in a lookup table.
Do you think something is missing or did you spot a (small) mistake?
Don't hesitate to reach out and
send me an email at:
kyle.hoeckman@gmail.com