setup()
setup() runs once when the sketch starts. It initializes the canvas, sets up the color system, and creates all the particles that will be animated throughout the sketch. Using windowWidth and windowHeight makes the sketch responsive to window resizing.
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100, 100); // Set color mode to HSB with alpha
noStroke(); // No outlines for shapes
// Initialize particles with random positions and velocities
for (let i = 0; i < numParticles; i++) {
particles.push(new Particle(random(width), random(height)));
}
}
๐ง Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-window canvas that fills the entire browser window
colorMode(HSB, 360, 100, 100, 100);
Sets color system to HSB (Hue, Saturation, Brightness) with ranges 0-360, 0-100, 0-100, 0-100 for alpha
for (let i = 0; i < numParticles; i++) { particles.push(new Particle(random(width), random(height))); }
Creates 250 Particle objects at random positions and adds them to the particles array
Line by Line:
createCanvas(windowWidth, windowHeight);- Creates a canvas that matches the full width and height of the browser window, making the sketch responsive
colorMode(HSB, 360, 100, 100, 100);- Switches from default RGB to HSB color mode, which is better for creating color gradients based on velocity. The ranges mean: Hue 0-360, Saturation 0-100, Brightness 0-100, Alpha 0-100
noStroke();- Disables outlines on all shapes, so particles and wells are filled circles without borders
for (let i = 0; i < numParticles; i++) {- Loops 250 times to create 250 particles
particles.push(new Particle(random(width), random(height)));- Creates a new Particle at a random x,y position on the canvas and adds it to the particles array