Using Loops with the Mouse

This sketch draws a line from the mouse to a random point nearby (mouseX-50 to mouseX+50, etc).
void setup() { size(800,600); } void draw() { float x2,y2; x2=random(mouseX-50,mouseX+50); y2=random(mouseY-50,mouseY+50); background(0); stroke(255); if (mousePressed) { line(mouseX,mouseY,x2,y2); } }

Same sketch with a loop

void setup() { size(800,600); } void draw() { float x2,y2; int i; background(0); stroke(255); if (mousePressed) { for (i=0;i < 10; i++) { x2=random(mouseX-50,mouseX+50); y2=random(mouseY-50,mouseY+50); line(mouseX,mouseY,x2,y2); } } }

Light scribble

This sketch scribbles 10 lines, each roughly between the current mouse position and previous position.
void setup() { size(800,600); } void draw() { float x1,y1,x2,y2; stroke(255,255,255,16); if (mousePressed) { for (int i=0;i < 10;i++) { x1=random(mouseX-10,mouseX+10); y1=random(mouseY-10,mouseY+10); x2=random(pmouseX-10,pmouseX+10); y2=random(pmouseY-10,pmouseY+10); line(x1,y1,x2,y2); } } if (keyPressed) { background(0); } }