Jump to content

How to center text?


Bossman759
 Share

Recommended Posts

http://www.html5gamedevs.com/topic/7129-how-to-center-a-bitmaptext/

 

Basically, you have to position it to the center of the screen minus half the width of the text to center it. To right-align it, you have to position the text where you want it minus the full width of the text. Left align doesn't require any changes. This is because the first line of text is always aligned to the top left - subsequent lines are aligned based on the align setting you choose.

Link to comment
Share on other sites

You probably are talking about two different things. The bitmap font, and the canvas rendered fonts (to a single image sprite).

 

The anchor.setTo(0.5) works fine, although there's small issue with it if the rendered bitmap size is even. The sprite is aligned at subpixel level and the text gets blurry. To counter this you can do:

text.anchor.x = Math.round(text.width * 0.5) / text.width;

This will set anchor to 0.5 or offsets it by fraction so that the adjusted position will be full pixel. Same works for y axis. I think the same also applies any sprite, so the anchor set to 0.5 is not ideal if you want to preserve crisp looks, while retaining subpixel for animations.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...