This example uses two custom classes. The Particle class stores a position, velocity, and hue. It renders a circle using the current position and hue, and it updates the position using the current velocity. The Path class stores an array of objects created from the Particle class. It renders lines connecting each of the particles. When the user clicks the mouse, the sketch creates a new instance of the Path class. When the user drags the mouse, the sketch adds a new instance of the Particle class to the current path.
Connected Particles by p5.js Contributors and the Processing Foundation is licensed under CC BY-NC-SA 4.0.