Centering text char by char
in
Programming Questions
•
1 year ago
Hi.
I am trying to accomplish the following: draw a string, char by char, on the center (both vertically and horizontally) inside a bounding box (also both both centered vertically and horizontally),
everytime I click the mouse.
The problem I can't seem to wrap my head around is how to do each char everytime I click the mouse. Below, there's the code for achieving what I want,
but it draws the all text at once, and that's not what I want.
Any idea how to achive it?
I guess one solution might be, doing all the centering stuff by myself and making a text function call by each letter everytime the mouse gets clicked, but that would imply writing a lot of desnecessary could (?)
Here's an a few images to better explain my question:


Thanks.
- size(400, 400);
- int cx = width/2;
- int cy = height/2;
- int boxWidth = 300;
- int boxHeight = 300;
- PFont font = createFont("FFScala", 32);
- textFont(font);
- rectMode(CENTER);
- textAlign(CENTER, CENTER);
- fill(255);
- rect(cx, cy, boxWidth, boxHeight);
- fill(102);
- //text("foobar", cx, cy, boxWidth, boxHeight); // image 1
- //text("foobar and some other foobar", cx, cy, boxWidth, boxHeight); // image 2
- //text("foobar and some other foobar but this is one more long than the previous", cx, cy, boxWidth, boxHeight); // image 3
1