Pixi.js: BitmapText .textWidth only correct after render?

Created on 13 Dec 2016  ·  4Comments  ·  Source: pixijs/pixi.js

After setting the .text property of a BitmapText, the .textWidth is incorrect until after a render. Had to use requestAnimationFrame() to wait when doing re positioning calculations. Is this expected behavior?

💾 v4.x (Legacy) 📢 Accepting PRs 🕷 Bug

All 4 comments

Err, it's expected behaviour right now because that's what the code does, but we can make it better ;)

You see, text isn't updated until it's renderered for performance reasons. Imagine you had a counter, counting up super fast. You wouldn't want to re-create each number in the text class because it's only when it's rendered that you need it to show the right thing.

The normal Text class, for example, makes sure that when you try to get the width of it, it updates the text outside of the rendering loop, to make sure that the value returned is accurate and up to date. BitmapText is not doing this (but it should).

Whilst a fix is created, you can call .updateText() on the object before querying .textWidth. This should give you the accurate measurement without having to wait for a rAF

the fix would be to make the current .textWidth a private ._textWidth, create a read only getter called .textWidth, which calls .validate() before returning ._textWidth;

for the getter of .width (and height),, I believe it should call .updateTransform() before returning the width

.validate is what I was looking for. Thanks :). I'll implement + pr later this evening if I get a chance.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

madroneropaulo picture madroneropaulo  ·  3Comments

gigamesh picture gigamesh  ·  3Comments

Darker picture Darker  ·  3Comments

neciszhang picture neciszhang  ·  3Comments

YuryKuvetski picture YuryKuvetski  ·  3Comments