Set background during animation (noobie)

edited December 2015 in Questions about Code

Hello,

I am sure that this question has been ask before. I had really tried to dig in (forum, friends) but I am still stuck. I think it is a very simple thing and more a misunderstanding. By the way sorry about my poor english.

I'm doing an animation like a twill fabric machine. Two objects go and goes leaving a colored "string" behind. Right to left, top o bottom. The color can be set with 'b' and 'n'. We can freeze with a press mouse. All is perfectly fine, by I would like set the background by a key (here 'c' and 'v', black or white), but it does not work, the background seems to run over the previous drewn string. I tried by placing a rectangle at level -1, float, Matrix etc. nothing is working. I think it is a lack of logic P5 comprehension Thanks a lot, have a nice day

int x;
int y;
int deplaceX;
int deplaceY;
int changementX = 1;
float fond = 255;
int v=585; //X
int w=585; //Y
int deplaceV;
int deplaceW;
int f;


void setup(){
  size(600,600); 
  noStroke(); 
 background(f); 
}

void draw(){
  smooth();

 if ( key == 'f')  {
  f = 255; 
}


//FOND

/*background(fond);
  fond = changementX;

  if ( key == 'v') {

 changementX= 255;
}

  if ( key == 'c')  {
  changementX = 0;
}*/


//FORMES
  if ( key == 'n') fill(random(100,250),0,0);
  if ( key == 'b') fill(0,0, random(170,250));
  ellipse(x, y, 15, 15);
  rect(v, w, 15, 15);



///DESCEND CERCLE

  x=x+deplaceX+3;
  w=w+deplaceW-3;

if (x>width)
{deplaceX= -20; //retour
y=y+20; //à la ligne
}

if(x<20)
{
 deplaceX=1;
}

///VERSGAUCHE CARRE

if (w<1)
{deplaceW= 20;
v=v-20;
}

if(w>560)
{
deplaceW=-1;
}



}

//COMMANDES SOURIS
void mousePressed() {
  noLoop();  
}

void mouseReleased() {
  loop(); 
}

Answers

  • edited December 2015

    Just place background in draw().

  • edited December 2015

    Hello Ater, I don't get it, I already did it. By placing this :

    if ( key == 'v') {

    background(255); }

    if ( key == 'c') { background(0);}

    Just after void draw(){ it is working (the background change if c or v is pushed) but the white or black run over the graphics made before.

    int x;
    int y;
    int deplaceX;
    int deplaceY;
    float fond = 255;
    int v=585; //X
    int w=585; //Y
    int deplaceV;
    int deplaceW;
    int f;
    
    
    void setup(){
      size(600,600); 
      noStroke(); 
    
    }
    
    void draw(){
        if ( key == 'v') {
    
     background(255);
    }
    
      if ( key == 'c')  {
      background(0);}
      smooth();
    
     if ( key == 'f'){
      f = 255; }
    
    
    
    
    //FORMES
      if ( key == 'n') fill(random(100,250),0,0);
      if ( key == 'b') fill(0,0, random(170,250));
      ellipse(x, y, 15, 15);
      rect(v, w, 15, 15);
    
    
    
    ///DESCEND CERCLE
    
      x=x+deplaceX+3;
      w=w+deplaceW-3;
    
    if (x>width)
    {deplaceX= -20; //retour
    y=y+20; //à la ligne
    }
    
    if(x<20)
    {
     deplaceX=1;
    }
    
    ///VERSGAUCHE CARRE
    
    if (w<1)
    {deplaceW= 20;
    v=v-20;
    }
    
    if(w>560)
    {
    deplaceW=-1;
    }
    
    
    
    }
    
    //COMMANDES SOURIS
    void mousePressed() {
      noLoop();  
    }
    
    void mouseReleased() {
      loop(); 
    }
    

    Run it, press n or b, wait some goings and comings, press c or v to change the background. The background goes over the strings.

    Thanks GoToLoop, i'm not sure where you want me to go, but I keep trying with it.

  • edited December 2015 Answer ✓

    Since you wanna use background() w/o wiping out what's already drawn,
    you need to draw in a separate 100% transparent PGraphics object.
    Basically something like this: *-:)

    PGraphics pg;
    color bg = -1;
    
    void draw() {
      background(bg);
      updateLayer();
      image(pg, 0, 0);
    }
    
    void mousePressed() {
      bg = (color) random(#000000);
    }
    
    void updateLayer() {
      pg.beginDraw();
      // ...
      pg.endDraw();
    }
    
  • Hello GTL, thanks a lot for responding, I tried but I have a NullPointerException error.I unsuccessfully looked for a answer, I will see with my teacher, I'm not that patient. Thx for your time, have a nice day

Sign In or Register to comment.