Bad performance when using text()

edited May 2016 in Questions about Code

Hey,

I'm trying to display some text on the screen using the text() function.

Everything works out fine with very short strings. But as soon as the string gets a bit longer, the framerate starts to drop rapidly (25 FPS with a string of about 500 characters).

Is there any other way to display longer paragraphs of text without getting into performance problems? Here's some sample code:

PFont boldFont;

String[] stringArray;

void setup() {

  frameRate(60);

  smooth();
  fullScreen();
  noCursor();

  background(255);
  fill(0);

  boldFont = createFont("Consolas Bold", 18, true);

  // load text file with strings
  stringArray = loadStrings("text.txt");
}    

void draw() {

  background(255);
  fill(0);

  textFont(boldFont, 18);

  // display string
  // framerate drops proportional to string length
  text(stringArray[0], 50, 50, width - 100, height - 100);

  println(frameRate);
}

Answers

  • render to a PGraphic in setup and display that as an image? you'd only need to draw the characters once that way (i am assuming that's the holdup, the translation from vector font to pixels).

    (the program above has no interaction and no animation. in such situations i would use noLoop()... but i realise this may just be an example)

    i get 55 fps with a 800+ character string fwiw

  • Thanks for your help!

    The program above was just supposed to illustrate my problem. My actual sketch does have animation and changing text.

    But yes, translation from vector to pixel seems to be the bottleneck. If I use loadFont() instead of createFont() things run smoothly.

    Unfortunately the character spacing gets pretty messed up when using loadFont(). Some characters (like 'm' for example) are overlapping and as far as I know there is no way to adjust character spacing in Processing...

Sign In or Register to comment.