Exercise 3-3: Explain why we see a trail of rectangles if we move background() to setup(), leaving it out of draw().


void setup() {   
  size(200,200); 
  background(255); 
}   

void draw()  {   
  // Body  
  stroke(0);  
  fill(175);  
  rectMode(CENTER);  
  rect(mouseX,mouseY,50,50); 
} 
Answer: When background() is in setup(), the background is drawn only once when your Processing sketch first starts up. Therefore each square is drawn on top of the previous square. With background() in draw(), the background is drawn continuously each time before the square is drawn. Therefore your sketch shows a single square following the mouse.


»

No comments yet.

Leave a comment