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

请问如何用canvas计算带拼音的字体的高度?image.png

measureText返回的结果并不对,而且用dom的方式计算也是不准确(image.png),虚心请教各位大佬

阅读 1.3k
1 个回答

可以看下这个是不是能用
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.
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题