setup()
setup() runs once when the sketch starts. It's where you initialize your canvas, create UI elements, and set up any data structures. The key concept here is creating an offscreen graphics buffer (trailGraphics) that allows us to draw trails that gradually fade out, creating the beautiful trailing effect you see in the animation.
function setup() {
// Create the main canvas
createCanvas(windowWidth, windowHeight);
// Use HSB color mode for easy gradient generation
colorMode(HSB, 360, 100, 100, 100);
// Create the offscreen graphics buffer for trails
trailGraphics = createGraphics(windowWidth, windowHeight);
trailGraphics.colorMode(HSB, 360, 100, 100, 100);
// Fill the trail buffer with a dark background initially
trailGraphics.background(0, 0, 0, 100);
// --- Create Sliders ---
// Pendulum Count Slider
pendulumCountLabel = createP('Pendulum Count: ');
pendulumCountLabel.position(10, 10);
pendulumCountLabel.class('p5js_label'); // Add class for styling
pendulumCountSlider = createSlider(5, 50, 25, 1); // Min 5, Max 50, Initial 25, Step 1
pendulumCountSlider.position(10, 40);
pendulumCountSlider.class('p5js_slider'); // Add class for styling
// Re-initialize pendulums whenever the slider changes
pendulumCountSlider.input(initializePendulums);
// Gravity Slider
gravityLabel = createP('Gravity: ');
gravityLabel.position(10, 70);
gravityLabel.class('p5js_label'); // Add class for styling
gravitySlider = createSlider(0.1, 2, g, 0.01); // Min 0.1, Max 2, Initial g, Step 0.01
gravitySlider.position(10, 100);
gravitySlider.class('p5js_slider'); // Add class for styling
// Set the initial pendulum count and initialize them
currentPendulumCount = pendulumCountSlider.value();
initializePendulums();
}
π§ Subcomponents:
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100, 100);
Creates a full-window canvas and switches to HSB color mode for easier color gradients
trailGraphics = createGraphics(windowWidth, windowHeight);
trailGraphics.colorMode(HSB, 360, 100, 100, 100);
trailGraphics.background(0, 0, 0, 100);
Creates an offscreen graphics buffer to accumulate and display pendulum trails
pendulumCountSlider = createSlider(5, 50, 25, 1);
pendulumCountSlider.input(initializePendulums);
Creates an interactive slider to control the number of pendulums (5-50) and triggers re-initialization when changed
gravitySlider = createSlider(0.1, 2, g, 0.01);
Creates an interactive slider to control gravity strength from 0.1 to 2
Line by Line:
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, making the visualization responsive
colorMode(HSB, 360, 100, 100, 100);- Switches to HSB (Hue, Saturation, Brightness) color mode with ranges 0-360 for hue, 0-100 for saturation and brightness, and 0-100 for alpha. This makes it easy to create smooth color gradients
trailGraphics = createGraphics(windowWidth, windowHeight);- Creates an offscreen graphics buffer (invisible drawing surface) the same size as the canvas to accumulate pendulum trails
trailGraphics.colorMode(HSB, 360, 100, 100, 100);- Sets the trail graphics buffer to use the same HSB color mode as the main canvas
trailGraphics.background(0, 0, 0, 100);- Fills the trail buffer with a dark background (black with some transparency) to prepare it for drawing
pendulumCountLabel = createP('Pendulum Count: ');- Creates a text label above the pendulum count slider
pendulumCountSlider = createSlider(5, 50, 25, 1);- Creates a slider with minimum 5 pendulums, maximum 50, starting at 25, incrementing by 1
pendulumCountSlider.input(initializePendulums);- Connects the slider so that whenever its value changes, the initializePendulums() function is called automatically
gravitySlider = createSlider(0.1, 2, g, 0.01);- Creates a slider with minimum gravity 0.1, maximum 2, starting at the current g value, incrementing by 0.01
currentPendulumCount = pendulumCountSlider.value();- Reads the initial slider value and stores it to track when the user changes it
initializePendulums();- Calls the initialization function to create the first set of pendulums based on the slider values