Can't draw anything just after using surface.setSize()

edited October 2016 in Questions about Code

Hi, I have a problem using surface.setSize(). Here is a code example :

void setup(){ background(0); surface.setSize(1000, 1000); fill(255); rect(10, 10, 10, 10); } void mouseClicked(){ surface.setSize(1000, 1000); fill(255); rect(10, 10, 10, 10); } void draw(){}

When starting the sketch, I only have the black windows, the rectangle doesn't appear. But when I click the mouse, the rectangle appears. If I change mouseClicked() like that :

void mouseClicked(){ surface.setSize(width + 1, height + 1); fill(255); rect(10, 10, 10, 10); }

The windows becomes 1 pixel larger as expected but the white rectangle doesn't appear. I tried to do text(), to add delay() after surface.setSize() but it's the same.

Somebody knows how to fix this issue ?

Thanks.

Tagged:

Answers

  • Answer ✓

    Ok I found out my error. The surface.setSize() is done at next draw() loop.

  • @BriceFW -- Great! In general, if your draw loop is empty and being called, the drawing will be empty. So move all drawing code to draw(), or to something called by draw().

    void setup(){
      surface.setSize(200, 200);
      fill(255);
    }
    void draw(){
      background(0);
      rect(10, 10, width/2, height/2);
    }
    void mouseClicked(){
      surface.setSize(width + 10, height + 10);
    }
    

    You can also control in-draw code with the variable mousePressed:

    void setup(){
      surface.setSize(200, 200);
      fill(255);
    }
    void draw(){
      background(0);
      if(mousePressed){
        rect(10, 10, width/2, height/2);
      }
    }
    void mouseClicked(){
      surface.setSize(width + 10, height + 10);
    }
    

    ...but don't call surface.setSize in the draw loop directly or with continuous variables / functions like mousePressed / mousePressed() -- a stream of multiple resize commands can get stuck in a nasty loop.

    if(mousePressed){ surface.setSize(width + 1, height + 1); }

Sign In or Register to comment.