如何精确测量Canvas中带拼音的字体高度?

作者站长头像
站长
· 阅读数 25

请问如何用canvas计算带拼音的字体的高度?如何精确测量Canvas中带拼音的字体高度?

measureText返回的结果并不对,而且用dom的方式计算也是不准确(如何精确测量Canvas中带拼音的字体高度?),虚心请教各位大佬

回复
1个回答
avatar
test
2024-06-23

可以看下这个是不是能用https://opentype.js.org/

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.
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容