Drawing shapes in between loadPixels() and updatePixels()

I notice that I can’t draw primitive shapes in between calls to loadPixels() and updatePixels(). Consider the following sketch:

void setup() {
  size(200, 100);
}

void draw() {
  fill(0);
  ellipseMode(CENTER);
  ellipse(50, 50, 50, 50);
  
  loadPixels();
  ellipse(150, 50, 50, 50);
  updatePixels();
}

When run, the first circle is visible, but the second circle does not appear. If I remove the calls to loadPixels() and updatePixels(), both circles appear as expected.

Is there a way I can draw shapes in between calls to loadPixels() and updatePixels()? The project I’m working on requires this and it would be difficult/less performant to implement differently.

Thank you so much! :slight_smile:

1 Like

The project you are working on does not require you to write broken code! :wink:

This isn’t supported for a reason. What are you actually trying to achieve?

1 Like

You can draw them but won’t see them.

“All Processing programs update the screen at the end of draw(), never earlier.”
You can print() to the console.

Consider:

void setup() 
  {
  size(200, 100);
  }

void draw() 
  {
  //Shape 1  
  fill(0);
  ellipseMode(CENTER);
  ellipse(50, 50, 50, 50);
  
  loadPixels(); // Loads pixels[] array with data from display window (Shape 1)
  
  //Shape 2
  println("Something happening here...");
  ellipse(150, 50, 50, 50);
  println("And here");
  
  updatePixels(); // Updates display window with the data in pixels[] (Shape1)
  }

References:

Can you elaborate on this requirement?

:)

3 Likes

updatePixels() will write your pixels buffer back, as you have seen. That is what it is for. Why are you loading – to read values, or to modify values? If you are only calling loadPixels to read values, like color c = pixels[y*width+x]; then just don’t updatePixels() at all.

void setup() {
  size(200, 100);
}

void draw() {
  fill(0);
  ellipseMode(CENTER);
  ellipse(50, 50, 50, 50);
  
  loadPixels();
  // why are you loading, anyway?
  // call get() or read pixels[] here
  color c = pixels[mouseY*width+mouseX];
  fill(c);

  // if you didn't set pixels[] or call set(),
  // then don't call:
  // updatePixels();

  ellipse(150, 50, 50, 50);
}

For some context see:

https://processing.org/tutorials/pixels/

1 Like