setup()
setup() runs once when the sketch starts. It initializes the canvas, configures drawing settings, and creates the input field with an event listener. The input listener is clever—it only creates letter objects for NEW characters by tracking lastLen, avoiding duplicate processing.
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;
});
}
🔧 Subcomponents:
createCanvas(windowWidth,windowHeight); rectMode(CENTER);textAlign(CENTER,CENTER);textSize(28);
Creates a full-window canvas and configures how rectangles and text are drawn (centered)
inp=createInput('');inp.position(10,10);inp.size(260);
Creates a text input field at position (10,10) where users type characters
inp.input(()=>{ let v=inp.value(); for(let i=lastLen;i<v.length;i++){ ... } lastLen=v.length; });
Listens for text input changes and creates a new letter object for each new character typed
if(ch===' ')continue;
Skips creating letter objects for space characters
Line by Line:
createCanvas(windowWidth,windowHeight);- Creates a canvas that fills the entire browser window, making the sketch responsive to window size
rectMode(CENTER);- Sets rectangle drawing mode so that coordinates represent the center point, not the top-left corner
textAlign(CENTER,CENTER);- Centers text both horizontally and vertically at the coordinates where it's drawn
textSize(28);- Sets the font size to 28 pixels for all text rendering
inp=createInput('');- Creates a text input element and stores it in the inp variable so we can access what the user types
inp.position(10,10);inp.size(260);- Positions the input field 10 pixels from the top-left corner and sets its width to 260 pixels
inp.input(()=>{ ... });- Registers a callback function that runs every time the user types or deletes a character in the input field
let v=inp.value();- Gets the current text from the input field as a string
for(let i=lastLen;i<v.length;i++){- Loops through only the NEW characters added since the last input event (from lastLen to current length)
let ch=v[i];if(ch===' ')continue;- Gets the character at position i and skips it if it's a space (spaces don't create falling letters)
letters.push({x:random(w,width-w),y:0,vx:random(-1,1),vy:0,ch,t:millis()});- Creates a new letter object with random starting x position, zero y position (top of canvas), small random horizontal velocity, zero vertical velocity, the character itself, and a timestamp
lastLen=v.length;- Updates lastLen to the current input length so next time we only process new characters