3 ms·
One canvas limitation I ran into was obtaining the height of a given piece of text. Width is available, but height is not, so you can't center some text vertica
by est31 5y ago
One canvas limitation I ran into was obtaining the height of a given piece of text. Width is available, but height is not, so you can't center some text vertically. Stack overflow tells you to use an "M" character as it has roughly the same width and height...
- xemdetia 5y agoThis is actually a hard problem even for most font libraries as it requires 'execution' of the font and kerning to actually know the parameters. I'm not saying this is a good answer but this is a tricky problem outside just Canvas2D. E.g. sour, soup, slip, and shin are four words with different heights, how do each in turn play out? Most of the time this is external to the font library and is in the layout engine which is talking to the font rendering context the same way you are in a raw font library/canvas. To be honest this is one of the things that made web browsers and html so good is because it solves this problem really well compared to having to figure this out in native apps across languages the hard way.
- slmjkdbtl 5y agoDepends on your use case, in my case I'm using the font size as the height just fine, m.actualBoundingBoxAscent + m.actualBoundingBoxAscent or m.fontBoundingBoxAscent + m.fontBoundingBoxDescent also works for some cases.
- Matheus28 5y agoYou can set the textBaseline if your intent is vertically centering it