setup()
setup() runs once when the sketch starts. It is the perfect place to initialize your canvas, create GUI controls, and spawn objects that populate the rest of your sketch.
🔬 This loop creates all the particles at startup. What happens if you change 100 to 10? To 500? How does particle count affect the visual density and smoothness of the swarm?
for (let i = 0; i < 100; i++) {
particles.push(new Particle(random(width), random(height)));
}
function setup() {
createCanvas(windowWidth, windowHeight);
// Create dat.gui
gui = new dat.GUI();
gui.add(settings, 'particleSpeed', 1, 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
).name('crazy meter');
for (let i = 0; i < 100; i++) {
particles.push(new Particle(random(width), random(height)));
}
}
Line-by-line explanation (5 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire browser window
gui = new dat.GUI();
Instantiates the dat.gui interface for interactive controls
gui.add(settings, 'particleSpeed', 1, 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
).name('crazy meter');
Creates an interactive slider that controls the particleSpeed setting
for (let i = 0; i < 100; i++) {
particles.push(new Particle(random(width), random(height)));
}
Creates 100 particles at random locations and adds them to the particles array
createCanvas(windowWidth, windowHeight);- Creates a canvas sized to fill the entire browser window, making the particle system responsive to window size
gui = new dat.GUI();- Creates a new dat.gui interface object that will hold interactive controls like the crazy meter slider
gui.add(settings, 'particleSpeed', 1, 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ).name('crazy meter');- Adds a slider to the GUI that controls settings.particleSpeed from 1 to an extremely large max value, labeled 'crazy meter'—note the max is intentionally huge to allow dramatic speed increases
for (let i = 0; i < 100; i++) {- Starts a loop that will run 100 times to create 100 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