Reset background() outside draw and setup to clear display

Hi there.

I’m writing a code to make a wordcloud using a couple of libraries. I need to redraw the (updated) cloud after I receive a vocal input. I’m doing that by calling the function Background(); that should draw a background over the old one. I’m placing the background outside the draw function because there doesn’t work properly.

I would appreciate a suggestion on how to solve the problem or a potential workaround.
Thanks all for the help!!!

Here my code

void setup() {
  size(1200, 800);
  smooth();
  background(226, 35, 26);
  makeWordCram();
}

void makeWordCram() {
  cloud = new WordCram(this)
    .fromTextFile(wordFile)
    .lowerCase()a
    .withColor(#ededed)
    .sizedByWeight(12, 60)
    .withWordPadding(5);
}

void draw() {
  if (cloud.hasMore()) {
    cloud.drawNext();
  } else {
    noLoop();
  }
}

void webSocketServerEvent(String data) {
  println(data);
  String[] lines = loadStrings(wordFile);
  String[] newWord = splitTokens(data, " ");
  saveStrings(wordFile, concat(lines, newWord));

  background(226, 35, 26);
  makeWordCram();
  loop();
}

Hi,

If the websocket listener is running on its own thread then I think you shouldn’t draw anything inside webSocketServerEvent, but instead set variables that are later used inside the draw() function, which runs in the graphics thread. I believe there are similar issues in other libraries that receive external events out of the main graphics thread.

As an example, you could have a global boolean dataReceived = false; which you toggle when you receive data, and the draw function checks this boolean to know if something should be updated.

3 Likes

Thanks for the answer, but I don’t know where to call the draw

void draw() {
  if (dataReceived) {
    background(226, 35, 26);
  }
  ...
}

thanks for the help. I meant I don’t know where to call the dataReceived.
If i redraw the cloud like this:

void draw() {
  if (dataReceived == true) {
    background(226, 35, 26);
  }
  if (cloud.hasMore()) {
    cloud.drawNext();
  } else {
    noLoop();
  }
}

It draw a new background for each word he write meaning i don’t see anything. I cant understand where i should make a new background() so that:
Program start: draw background > draw the cloud > new Vocal input> draw (new) background > draw cloud

Well, you should turn back off the dataReceived variable:

if (dataReceived == true) {
  background(226, 35, 26);
  dataReceived = false;
}
1 Like