setup()
setup() runs once when the sketch starts. It's where you initialize the canvas, position UI elements, set up audio, and prepare data structures. This sketch's setup() is quite rich because it has to coordinate a full-screen canvas, centered windows, interactive buttons, and audio synthesis all at once.
🔬 This loop populates scannedFiles with fake paths. If you change 'maxScannedFiles * 5' to just 'maxScannedFiles', what happens to the scrolling file list in the scanning phase? Will it still feel smooth?
// Generate some fake file paths for the scan
for (let i = 0; i < maxScannedFiles * 5; i++) { // Generate more than needed to scroll
scannedFiles.push(generateFakeFilePath());
}
function setup() {
// Create a full-screen canvas
createCanvas(windowWidth, windowHeight);
// Calculate position for the simulated prank window to be centered on the canvas
windowX = (width - prankWindowWidth) / 2;
windowY = (height - prankWindowHeight) / 2;
progressBarWidth = prankWindowWidth - 60; // Calculate based on prankWindowWidth
textAlign(LEFT, TOP);
textSize(16);
textFont('monospace'); // For a terminal-like feel
// Initialize audio components
alarmingOsc = new p5.Oscillator('sine');
whirringNoise = new p5.Noise('white');
whirringNoise.amp(0); // Start silent
// Start the audio context (required for p5.sound)
userStartAudio();
// Generate some fake file paths for the scan
for (let i = 0; i < maxScannedFiles * 5; i++) { // Generate more than needed to scroll
scannedFiles.push(generateFakeFilePath());
}
// Initialize the prank state
prankState = 'SCANNING';
progress = 0;
startTime = millis();
alarmingOsc.start();
alarmingOsc.amp(0.1);
// Create and hide prank button
// Position relative to the *actual* canvas dimensions (full screen)
fixButton = createButton('FIX NOW');
fixButton.position(width / 2 - 75, height / 2 + 100);
fixButton.size(150, 50);
fixButton.style('background-color', '#FF0000');
fixButton.style('color', '#FFFFFF');
fixButton.style('border', 'none');
fixButton.style('border-radius', '5px');
fixButton.style('font-size', '24px');
fixButton.style('font-weight', 'bold');
fixButton.style('cursor', 'pointer');
fixButton.hide(); // Hide initially
fixButton.mousePressed(() => {
prankState = 'FIXING';
progress = 0;
startTime = millis();
fixButton.hide(); // Hide the button
alarmingOsc.stop(); // Stop alarming sound
whirringNoise.start(); // Start whirring sound
whirringNoise.amp(0.2);
});
}
Line-by-line explanation (13 lines)
🔧 Subcomponents:
windowX = (width - prankWindowWidth) / 2;
Centers the simulated prank window horizontally on the canvas by calculating the offset
alarmingOsc = new p5.Oscillator('sine');
Creates a sine wave oscillator that will play alarming tones throughout the prank
for (let i = 0; i < maxScannedFiles * 5; i++) { scannedFiles.push(generateFakeFilePath()); }
Populates the scannedFiles array with fake file paths so there are always more files than can display at once, enabling scrolling illusion
fixButton.mousePressed(() => { prankState = 'FIXING'; ... });
Defines what happens when the user clicks the 'FIX NOW' button—transitions to FIXING state and starts whirring audio
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, making the prank full-screen and immersive
windowX = (width - prankWindowWidth) / 2;- Calculates the horizontal offset needed to center the prank window on the canvas
windowY = (height - prankWindowHeight) / 2;- Calculates the vertical offset needed to center the prank window on the canvas
textFont('monospace');- Sets the font to monospace (like a terminal or command prompt) to enhance the system-scanner aesthetic
alarmingOsc = new p5.Oscillator('sine');- Creates a sine wave oscillator—this will generate the beeping/alarming tones heard during scanning and infection
whirringNoise = new p5.Noise('white');- Creates a white noise generator that will play the 'whirring' sound during the fixing phase
userStartAudio();- Initializes the Web Audio API—required by p5.sound before any audio can play in the browser
for (let i = 0; i < maxScannedFiles * 5; i++) { scannedFiles.push(generateFakeFilePath()); }- Generates 75 fake file paths (15 * 5) and stores them in the scannedFiles array—more than needed so the list can scroll convincingly
prankState = 'SCANNING';- Sets the initial state to SCANNING, which tells draw() to run drawScanningScreen() each frame
startTime = millis();- Records the current time in milliseconds—used to calculate how much time has elapsed in the current state
alarmingOsc.start();- Starts the oscillator playing—it will now generate sound until alarmingOsc.stop() is called
fixButton = createButton('FIX NOW');- Creates an HTML button element with the text 'FIX NOW'—this is what users can click to 'fix' the fake virus
fixButton.hide();- Hides the button initially so it doesn't appear until the INFECTED state, when it becomes relevant to the prank