Sign in to follow this  
Bossman759

How to center text?

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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.