Jump to content

GUI - Resize TextBlock according to idealWidth


Recommended Posts

Bug? - Try to resize the window, text is cropped instead of resized:



Feature suggestion :) - When idealWidth is present, would it be possible to resize text blocks based on an internal calculation of its dimensions (pixels) at the idealWidth? (or make it optional)

That way the width and height of a text block could be defined according to that internal calculation, and since they'll be defined in px with idealWidth present, they'll be resized accordingly.

The way things are now we'd have to figure roughly how big the text block is and define it manually in px for it to be resized in proportions to idealWidth.

For instance for image we can simply set the width and height of the gui image to that of the dom image in order to automate the calculation, but for text blocks we don't have any way to actually know the width and height of the text. (or do we?)


Link to comment
Share on other sites

Thanks for the fix :)


So the way you know for instance how to align text blocks according to their actual size is via context.measure which uses font size?

If so, since by design font sizes aren't affected by resolution (correct me if I'm wrong), maybe we could treat the idealWidth as a text block's 100%? Since a text block is rendered at a fixed size according to font size by default regardless of stage size, if that can be calculated (the way you calculate the text block's actual size in order to align it) then resizing according to idealWidth should be possible.

Makes sense?

Link to comment
Share on other sites

11 hours ago, royibernthal said:

So the way you know for instance how to align text blocks according to their actual size is via context.measure which uses font size?

Correct: https://github.com/BabylonJS/Babylon.js/blob/master/gui/src/controls/textBlock.ts#L112

Not sure to follow you: you want to:

- compute text size. Let's say it is 300px with a font size of 50px

- scale that 300px according to idealWidth and current resolution. so if idealWidth = 600 and current resolution = 300 then the new text size should be 150px

- Revert the measureText to deduce the right font size to produce a text of 150px width?


The last step seems complex as I see no other option than brute force :(

Link to comment
Share on other sites

Yes this was pretty match what I meant.

Is it needed to deduce the right font size? Why not keep the same font size and simply multiply the values returned from context.measure by the text scale?

That way instead of trying to deduce the right font size, you still deal with pixels and the font size remains the same, only the whole thing is scaled.

I've never dealt with that low-level api so it's highly likely that I'm missing something.

Link to comment
Share on other sites

Let's simplify it -

At the moment I can see 2 functionalities:

1) Measure text block size (e.g. that's how you align text blocks)

2) Resize text block (e.g. to be dependent on idealWidth)


What I'm doing now is setting the text block size in px by eye, and then it is resized according to idealWidth.

Why not measure the text block size, and then set its width and height in px according to that? It should really be as simple as that, since the rest of the logic is already there.


While we're on the subject -

Can you expose a way to get text block ACTUAL width and height in px?


Here I don't see anything exposed:


and width and height return 100% (makes sense but doesn't help me)

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

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.


  • Recently Browsing   0 members

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