This Processing tutorial will teach you how to create a trail of particles at the mouse position, which follow the mouse like a snake.
To complete this tutorial, you will need to have a particle system. Follow this tutorial to learn how to build a particle system in Processing.
Mouse Update
Create a new function called MouseUpdate in the main tab.
void MouseUpdate()
{
final int spawnTime = 10;
if(mousePressed && millis() - lastTime > spawnTime)
{
lastTime = millis();
Particle p = particleSpawner.Spawn();
p.image = particleImage;
p.lifeTime = 500;
p.pos.set(mouseX, mouseY);
p.col = color(255, 30, 20);
}
particleSpawner.Update();
}
We first decide how often we will spawn the particles, and the particles will only be spawned when the mouse is pressed. So in this case, it will spawn one particle every 10 milliseconds if the mouse is pressed.
final int spawnTime = 10;
if(mousePressed && millis() - lastTime > spawnTime)
Next we record the current time, for the next check. Then spawn a particle and set image to the image we loaded in the last tutorial.
lastTime = millis();
Particle p = particleSpawner.Spawn();
p.image = particleImage;
Now we set the life time of the particle to 500 milliseconds.
p.lifeTime = 500;
The position will be set to the mouse position.
p.pos.set(mouseX, mouseY);
The colour will be set to a mostly red colour. Note the particle will be fully opaque, since the default value of the alpha is 255.
p.col = color(255, 30, 20);
Lastly we just need to call the Update function on all the particles. This is where the particle logic occurs. Such as movement and checking the lifetime of the particle. Only active particles are updated.
particleSpawner.Update();
Draw Function
Go to the draw function.
void draw()
Replace this:
ExplosionUpdate();
With this:
MouseUpdate();
Run the Sketch
Now it’s time to have some fun and run the sketch. Press CTRL + R or click on the triangle play symbol.
A particle will be created at the position of the mouse when you hold the mouse button down.
Conclusion
Congratulations on completing this tutorial! Feel free to experiment with different particle shapes, colours and spawn rates.
Thanks for reading this tutorial, let me know in the comments section if you enjoyed it.
Follow this tutorial to learn how to create a steady stream of smoke particles.

