| Reference | Downloads | Github

Center text with spaces

Hi all,

I’m trying to display single-line text in the center of the screen in an online study. The text is updated every frame according to what the subject types. If the subject types a letter, the whole text moves slightly so that the center of the text is in the center of the screen. However, if the subject types a space, the text does not move (it will if a new character is typed afterwards). PsychoJS seems to ignore trailing spaces. I would like the text to move even when spaces are typed. Is there a way to do this?

Thank you!


Hi Laurent,

Interesting issue! This is likely due to how web-browsers treat spaces; they ignore any spaces around text and double spaces within text. I played around a bit with a text stimulus in a test-experiment (manipulate my_text in the browser console), to see if I could find a way:

Now, normally a workaround would be typing in  , that’s an explicit whitespace, illustrated here. Left     right. But it doesn’t work; it just literally gets printed as-is. I tried…

my_text.setText("test    ");
my_text._pixi.text = "test    ";
my_text._pixi.setText("test    ");

@sotiri, you got any ideas on this?

Best, Thomas

Thank you for your investigation Thomas. I haven’t found a solution to this problem either. I abandoned this idea for my study and changed my larger approach.

If you’d like we could brainstorm a bit about your study aims and figure out if it could be achieved in another way?

Thank you but it’s ok: centering was not absolutely necessary, it’s just that I had another piece of text that moved with it according to the width of the first text piece, and I was not ignoring spaces to compute the width, so there was a mismatch, but I changed how I estimated the width so it’s ok.

1 Like