micojs / micojs.github.io

10 stars 0 forks source link

Allow getting a string's width in pixels #7

Open felipemanga opened 1 year ago

felipemanga commented 1 year ago

const width = getWidth("string"); // gets width with current font

lucentbeam commented 1 year ago

TIC-80 (and maybe pico-8?) just return the text width after a print (text in MicoJS) command. So in general if you need to work with a calculated width, you just print somewhere offscreen and then do your business. A little funky, and not sure how well that would jive with how you do your render queue... But it does have the added benefit of sequential "marquee" style outputs:

let x = 50;
x += text("Hello ", x, y);
setPen(emphasisColor);
text("WORLD!", x, y);

This example contrived, but especially in UI that shows counters I find myself wanting this kind of functionality more often than not.

Just some food for thought :)

felipemanga commented 1 year ago

Hmmm, that sounds useful for printing a long line with various text calls, but annoying for something like centralizing text or putting text on the right side of the screen. Maybe we should we have both.