setup()
setup() runs once when the sketch starts. It's where you initialize your canvas size, set starting variable values, and create interactive UI elements like sliders and buttons. Every input slider uses a callback function to update a variable in real time.
function setup() {
createCanvas(windowWidth, windowHeight);
noStroke();
ball.x = width / 2;
ball.y = height / 2;
// Create the settings panel container
settingsPanel = createDiv();
settingsPanel.class('settings-panel');
settingsPanel.style('display', 'none'); // Hidden by default
// Create the toggle button for the settings panel
settingsToggleBtn = createButton('⚙️ Settings');
settingsToggleBtn.class('settings-toggle-btn');
settingsToggleBtn.mousePressed(toggleSettingsPanel);
settingsToggleBtn.touchStarted(toggleSettingsPanel); // Also for mobile touch
settingsToggleBtn.position(10, 10); // Top-left
// Add settings elements to the settingsPanel
addSetting('Ball Size:', ballSizeSlider = createSlider(10, 100, ball.size, 1));
ballSizeSlider.input(() => ball.size = ballSizeSlider.value());
addSetting('Ball Color:', ballColorPicker = createColorPicker(ball.color));
ballColorPicker.input(() => ball.color = ballColorPicker.value());
addSetting('Follow Speed:', followSpeedSlider = createSlider(0.01, 0.2, followSpeed, 0.01));
followSpeedSlider.input(() => followSpeed = followSpeedSlider.value());
addSetting('Particle Count:', particleCountSlider = createSlider(0, 50, particleCount, 1));
particleCountSlider.input(() => particleCount = particleCountSlider.value());
addSetting('Particle Color:', particleColorPicker = createColorPicker(particleColor));
particleColorPicker.input(() => particleColor = particleColorPicker.value());
addSetting('Particle Size:', particleSizeSlider = createSlider(2, 20, particleSize, 1));
particleSizeSlider.input(() => particleSize = particleSizeSlider.value());
addSetting('Trail Length:', trailLengthSlider = createSlider(5, 50, 10, 1));
trailLengthSlider.input(() => {}); // Value used in draw()
addSetting('Glow Effect:', glowCheckbox = createCheckbox('', glowEnabled));
glowCheckbox.changed(() => glowEnabled = glowCheckbox.checked());
addSetting('Glow Color:', glowColorPicker = createColorPicker(glowColor));
glowColorPicker.input(() => glowColor = glowColorPicker.value());
addSetting('Glow Strength:', glowStrengthSlider = createSlider(0, 50, glowStrength, 1));
glowStrengthSlider.input(() => glowStrength = glowStrengthSlider.value());
}
Line-by-line explanation (11 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
noStroke();
ball.x = width / 2;
ball.y = height / 2;
Creates a full-window canvas, disables shape outlines, and centers the ball on startup
settingsPanel = createDiv();
settingsPanel.class('settings-panel');
settingsPanel.style('display', 'none');
Creates the hidden container for all interactive controls
addSetting('Ball Size:', ballSizeSlider = createSlider(10, 100, ball.size, 1));
ballSizeSlider.input(() => ball.size = ballSizeSlider.value());
Creates a slider and links its value directly to the ball size variable so changes appear instantly
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire window—windowWidth and windowHeight update if the window is resized
noStroke();- Disables outlines around all shapes drawn after this, so the ball and particles appear as solid filled circles
ball.x = width / 2;- Places the ball at the horizontal center of the canvas by setting its x position to half the canvas width
ball.y = height / 2;- Places the ball at the vertical center of the canvas
settingsPanel = createDiv();- Creates an empty HTML div element that will hold all the settings controls
settingsPanel.class('settings-panel');- Applies the CSS class 'settings-panel' to style the container with dark background, flex layout, and scrolling
settingsPanel.style('display', 'none');- Hides the settings panel on startup—it only appears when the user clicks the settings button
settingsToggleBtn = createButton('⚙️ Settings');- Creates the button that users click to open and close the settings panel
settingsToggleBtn.mousePressed(toggleSettingsPanel);- Calls the toggleSettingsPanel function when the button is clicked on desktop
settingsToggleBtn.touchStarted(toggleSettingsPanel);- Also calls toggleSettingsPanel on touch devices, ensuring the button works on mobile
ballSizeSlider.input(() => ball.size = ballSizeSlider.value());- Every time the slider moves, its numeric value is immediately copied to ball.size so the visual change happens instantly