To update the properties of the particles in particles.js while keeping their positions intact, you need to directly manipulate the settings of the currently running instance without reinitializing it.
Here's a step-by-step guide on how to change the color of the particles dynamically:
- Access the Particles.js instance:
// Assuming you've initialized particles.js with:
particlesJS('particles-js', {/* options */});
// Access the instance
var pJS = window.pJSDom[0].pJS;
Ensure that you're accessing the right instance in the pJSDom array if there are multiple instances.
- Change the color property:
To change the color of the particles, you'll have to modify the color property within pJS.particles.
// Set the color you want; it can be in any supported color format
// such as hexadecimal, RGB, or HSL
var newColor = '#ff0000'; // Example: Red
// Update the color property
pJS.particles.color.value = newColor;
// If you have an array of colors and want to change to random colors
// pJS.particles.color.value = ['#ff0000', '#00ff00', '#0000ff', ...];
For shapes such as stroke, you will follow a similar pattern.
- Update particles:
After changing the color property, you need to tell particles.js to redraw the particles. Normally, this is done automatically each frame. However, the colors are initially set when particles are created. You can force the update by doing the following:
// Loop over all particles to change their color
pJS.particles.array.forEach(function (particle) {
// Option 1: Set the color directly if it's a single color
particle.color.value = newColor;
// Option 2: If you want to use an array of colors and pick one randomly
// for each particle, you can do:
// particle.color.value = pJS.particles.color.value[Math.floor(Math.random() * pJS.particles.color.value.length)];
});
// After changing the properties, call the function to update the canvas
pJS.fn.particlesRefresh();
Please note that the pJS.fn.particlesRefresh() function will recalculate particles' positions because it's equivalent to reinitializing the particles. If you need to change only the color without affecting the positions, you might need a custom function that only redraws the particles without recalculating positions. Unfortunately, as of my knowledge cutoff in 2023, there isn't a built-in function in particles.js to redraw with the new settings while keeping the old positions.
A workaround could be updating the draw function to use the current color of each particle rather than their initial color:
// Overwrite the draw function for each particle
pJS.particles.array.forEach(function (particle) {
var originalDraw = particle.draw;
particle.draw = function() {
this.color.value = newColor;
originalDraw.apply(this);
};
});
// Manually trigger a redraw of the canvas if it's not happening automatically
pJS.fn.vendors.draw();
This should update the color of the particles without resetting their positions. Since this is essentially a hack, I recommend creating a backup of your original code and testing thoroughly to ensure there are no unexpected side effects.