如何精确测量Canvas中带拼音的字体高度?-灵析社区

无心插柳柳成萌

请问如何用canvas计算带拼音的字体的高度?![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241010/8c201f6c1928d118750c05f603ed872b.png) measureText返回的结果并不对,而且用dom的方式计算也是不准确(![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241010/a4594fa779eef623f1140c0de063a73f.png)),虚心请教各位大佬

阅读量:215

点赞量:0

问AI
可以看下这个是不是能用 "https://opentype.js.org/" (https://link.segmentfault.com/?enc=olswOFJp3gUs4hKwrUOoHQ%3D%3D.ch7n3rB5HkYMEs0NhvTodDjDhaxrSMbJ8hte9MVxh68%3D) "Font.draw(ctx, text, x, y, fontSize, options)" «Create a Path that represents the given text.ctx: A 2D drawing context, like Canvas. x: Horizontal position of the beginning of the text. (default: 0) y: Vertical position of the baseline of the text. (default: 0) fontSize: Size of the text in pixels (default: 72). Options is an optional object containing:kerning: if true, takes kerning information into account (default: true) features: an object with OpenType feature tags as keys, and a boolean value to enable each feature. Currently only ligature features "liga" and "rlig" are supported (default: true). hinting: if true uses TrueType font hinting if available (default: false).» "Glyph.getBoundingBox()" «Calculate the minimum bounding box for the unscaled path of the given glyph. Returns an opentype.BoundingBox object that contains x1/y1/x2/y2. If the glyph has no points (e.g. a space character), all coordinates will be zero.»