开发者

How to find the dimensions of context.fillText(text, x, y)

开发者 https://www.devze.com 2023-02-04 16:40 出处:网络
I am drawing text on a canvas and was wondering how I can get the dimensions of the text to be drawn.

I am drawing text on a canvas and was wondering how I can get the dimensions of the text to be drawn.

Height should be simple enough.

My only thought is that to use a monospace font, and开发者_运维百科 calculate the length from the number of characters. Is there a better way to do this yet?


Have you read this drawing text using canvas documentation? Especially from the methods section;
The measureText() method seems to do what you ask.

Although there is no example at the documentation I'm guessing the use is something like:

var text = 'this is my text';
var text_ctx = ctx.measureText(text);
var text_width = text_ctx.width;
0

精彩评论

暂无评论...
验证码 换一张
取 消