如何找到context.fillText(text,x,y)的维度 [英] How to find the dimensions of context.fillText(text, x, y)

查看:883
本文介绍了如何找到context.fillText(text,x,y)的维度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在画布上绘制文字,想知道如何获取要绘制的文本的尺寸。

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

高度应该足够简单。

我唯一的想法是使用等宽字体,并根据字符数计算长度。还有更好的方法吗?

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?

推荐答案

你读过这个使用画布绘制文本文档?
特别是来自方法部分;

< a href =https://developer.mozilla.org/en/drawing_text_using_a_canvas#section_7 =nofollow> measureText()方法似乎就是按照你的要求做的。

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;

这篇关于如何找到context.fillText(text,x,y)的维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆