setup()
setup() runs once when the sketch starts. The key pattern here is multiplying width and height by percentages (0.15, 0.8, etc.) to make every position and size responsive—when the window resizes, all these values automatically scale. This is the foundation of responsive design in p5.js.
function setup() {
createCanvas(windowWidth, windowHeight);
// Tip: Use windowWidth/windowHeight for responsive canvas
// Initialize positions and sizes relative to canvas dimensions
// This makes the sketch responsive to different screen sizes, including your tablet
gunW = width * 0.15; // Gun width is 15% of canvas width
gunH = height * 0.2; // Gun height is 20% of canvas height
gunX = width * 0.8; // Gun is positioned 80% from the left
gunY = height * 0.9; // Gun is positioned 90% from the top (on the ground)
flareSize = width * 0.02; // Flare size is 2% of canvas width
flareSpeed = height * 0.01; // Flare moves up by 1% of canvas height per frame
airplaneSpeed = width * 0.005; // Airplane moves horizontally by 0.5% of canvas width per frame
supplyDropSpeed = height * 0.003; // Supply drop falls by 0.3% of canvas height per frame
vehicleSpeedBase = width * 0.003; // Base speed for vehicles
vehicleSize = width * 0.04; // Size of vehicle emojis
// Create and position the flare button
flareButton = createButton('Shoot Flare');
flareButton.position(width * 0.05, height * 0.05); // Position top-left
flareButton.style('font-size', '16px');
flareButton.style('padding', '10px 20px');
flareButton.style('background-color', '#ff5733'); // Orange-red color
flareButton.style('color', 'white');
flareButton.style('border', 'none');
flareButton.style('border-radius', '5px');
flareButton.mousePressed(triggerFlareSequence); // Attach trigger function to button press
flareButton.touchStarted(triggerFlareSequence); // Also work on touch devices
}
Line-by-line explanation (14 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire window, enabling the sketch to adapt to any screen size
gunX = width * 0.8;
Positions the gun 80% from the left edge, anchoring it to the bottom-right regardless of window size
airplaneSpeed = width * 0.005;
Scales animation speeds based on canvas width, ensuring motion feels consistent on large and small screens
flareButton = createButton('Shoot Flare');
Creates an interactive button that triggers the flare sequence when clicked or touched
createCanvas(windowWidth, windowHeight);- Creates a canvas that matches the full window size, allowing the sketch to scale responsively to tablets, phones, and desktops
gunW = width * 0.15;- Sets gun width to 15% of canvas width, so it scales proportionally when the window resizes
gunH = height * 0.2;- Sets gun height to 20% of canvas height
gunX = width * 0.8;- Positions gun 80% from the left edge, placing it on the right side of the canvas
gunY = height * 0.9;- Positions gun at 90% down from the top, keeping it near the ground
flareSize = width * 0.02;- Flare diameter is 2% of canvas width, making it visible but not overwhelming
flareSpeed = height * 0.01;- Flare moves upward by 1% of canvas height per frame, creating a smooth, screen-size-aware ascent
airplaneSpeed = width * 0.005;- Airplane moves left by 0.5% of canvas width per frame—half the flare speed—making it slower and more watchable
supplyDropSpeed = height * 0.003;- Supply packages fall by 0.3% of canvas height per frame, slower than the flare, giving vehicles time to react
vehicleSpeedBase = width * 0.003;- Vehicles move by 0.3% of canvas width per frame, same speed as the drop falls, creating tension
flareButton = createButton('Shoot Flare');- Creates a clickable button labeled 'Shoot Flare'
flareButton.position(width * 0.05, height * 0.05);- Positions the button 5% from the left and top, placing it in the top-left corner
flareButton.mousePressed(triggerFlareSequence);- Attaches the triggerFlareSequence function to the button, so clicking it starts the supply drop sequence
flareButton.touchStarted(triggerFlareSequence);- Also attaches the trigger function to touch events, making the button work on tablets and phones