setup()
setup() runs once at the start. Here it also wires up a p5.js DOM element (createInput) and attaches a custom callback with inp.input(), a common pattern for connecting HTML controls to a sketch's behavior.
🔬 This loop skips spaces so they never become falling blocks. What happens if you delete the `if(ch===' ')continue;` line - will spaces fall as invisible blank blocks that still take up collision space?
for(let i=lastLen;i<v.length;i++){
let ch=v[i];if(ch===' ')continue;
letters.push({x:random(w,width-w),y:0,vx:random(-1,1),vy:0,ch,t:millis()});
}
function setup(){
createCanvas(windowWidth,windowHeight);
rectMode(CENTER);textAlign(CENTER,CENTER);textSize(28);
inp=createInput('');inp.position(10,10);inp.size(260);
inp.input(()=>{
let v=inp.value();
for(let i=lastLen;i<v.length;i++){
let ch=v[i];if(ch===' ')continue;
letters.push({x:random(w,width-w),y:0,vx:random(-1,1),vy:0,ch,t:millis()});
}
lastLen=v.length;
});
}
Line-by-line explanation (8 lines)
🔧 Subcomponents:
for(let i=lastLen;i<v.length;i++){
Only processes characters typed since the last update, using lastLen as a bookmark, so old characters aren't re-added
let ch=v[i];if(ch===' ')continue;
Prevents spaces from becoming falling letter blocks
createCanvas(windowWidth,windowHeight);- Makes the canvas fill the entire browser window.
rectMode(CENTER);textAlign(CENTER,CENTER);textSize(28);- Configures drawing so rectangles are positioned by their center point, text is centered on its x/y coordinate, and the default font size is 28.
inp=createInput('');inp.position(10,10);inp.size(260);- Creates an empty HTML text input, places it near the top-left corner, and makes it 260 pixels wide.
inp.input(()=>{- Registers a callback function that runs every time the text inside the input box changes.
let v=inp.value();- Reads the current full text of the input box into the variable v.
let ch=v[i];if(ch===' ')continue;- Grabs the newly typed character; if it's a space, skip it so it never becomes a letter block.
letters.push({x:random(w,width-w),y:0,vx:random(-1,1),vy:0,ch,t:millis()});- Adds a new letter object to the array: a random horizontal starting position, y=0 (top of screen), a small random sideways velocity, zero vertical velocity, the character itself, and a timestamp for aging/fading.
lastLen=v.length;- Updates the bookmark so the next input event only looks at characters typed after this point.