setup()
setup() runs exactly once when the sketch starts. It's the perfect place to initialize the canvas size, define colors, and create your first objects. This sketch uses it to set up a fullscreen responsive canvas and spawn an initial wave of particles.
function setup() {
createCanvas(windowWidth, windowHeight);
// Tip: Use windowWidth/windowHeight for responsive canvas
// Initialize quicksand colors
quicksandColor1 = color(100, 70, 40); // Darker brown
quicksandColor2 = color(150, 110, 80); // Lighter brown
// Add some initial particles
for (let i = 0; i < 50; i++) {
addParticle();
}
}
Line-by-line explanation (4 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a fullscreen canvas that responds to the window size
quicksandColor1 = color(100, 70, 40); // Darker brown
Stores color objects used for the quicksand gradient and particle coloring
for (let i = 0; i < 50; i++) { addParticle(); }
Creates 50 particles at the start so the sketch doesn't begin empty
createCanvas(windowWidth, windowHeight);- Creates a canvas matching the full browser window size, making the sketch responsive to window resizing
quicksandColor1 = color(100, 70, 40); // Darker brown- Defines a darker brown color object used for the quicksand base; stored in a global variable so draw() can access it
quicksandColor2 = color(150, 110, 80); // Lighter brown- Defines a lighter brown color object used for particle variation; creates visual depth with two shades
for (let i = 0; i < 50; i++) {- A for-loop that repeats 50 times, calling addParticle() each time to populate the particles array