rot.js
rot.js copied to clipboard
[Question] How to get the height of wrapped text?
Hi Ondras, I have made a game with rot.js, the libray is very handy! <3
But there is one feature I hope rot.js could provide natively. When I want to print a long text in the message board, the width and height of which is fixed, I need to know how 'tall' the wrapped text is. For example, the height of the following text is 3:
This is a very long and warpped text.
I have tried myself following these steps:
- Split the original string by spaces.
- Use a loop to join the splitted strings when its length is less than messageBoard.width.
- When the loop ends, push the joined string into an array (wrappedText).
- Go to Step 2.
The length of the array (wrappedText) is the height of the wrapped text.
I don't know whether there are some better solutions. Could you please shed some light on this? Thank you. :)
I think you're looking for ROT.Text.measure
:
measure: function(str, maxWidth)
This returns an object with fields width
and height
.
> ROT.Text.measure("This is a very long and wrapped text.", 13)
{ width: 13, height: 3 }
Maybe @ondras can add an entry to the interactive manual?
FWIW display.drawText
will do the wrapping if you give it a maxWidth
argument at the end; the function also returns the number of lines it ended
up drawing so you can get the height-measure there.
That said, that's only useful if you don't need the height before you do the drawing :)
On Thu, Aug 16, 2018 at 1:14 PM Patrick Meade [email protected] wrote:
I think you're looking for ROT.Text.measure:
measure: function(str, maxWidth)
This returns an object with fields width and height.
ROT.Text.measure("This is a very long and wrapped text.", 13) { width: 13, height: 3 }
Maybe @ondras https://github.com/ondras can add an entry to the interactive manual?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ondras/rot.js/issues/138#issuecomment-413619352, or mute the thread https://github.com/notifications/unsubscribe-auth/AAA1TTCzXAWrEyfNf_MUxyw7vaRMtKeTks5uRaiNgaJpZM4V_ETi .
-- Roger Ostrander
Wow, this is amazing. Thank you all!
That said, that's only useful if you don't need the height before you do the drawing :)
I have figured out a way to work around this.
let width = display.drawText(999, 999, "This is a very long and wrapped text.", 13);
display.clear();
display.drawText(1, 1, "This is a very long and wrapped text.", 13);
console.log("The width of the wrapped text is: " + width);