只读的 TextMetrics.width 属性,包含文本先前的宽度(行内盒子的宽度),使用 CSS 像素计算。

语法

readonly metrics.width;

示例

事先给定 <canvas> 元素:

<canvas id="canvas"></canvas>

你可以使用下面的代码得到一个 TextMetrics 对象:

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

var text = ctx.measureText("foo"); // TextMetrics object
text.width; // 16;

规范描述

Specification Status Comment
HTML Living Standard
TextMetrics.width
Living Standard  

浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 4.0 1.5 (1.8) 9.0 9.0 3.1
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.1 1.0 (1.8) ? 10.0 3.2

参见

文档标签和贡献者

此页面的贡献者: FredWe, ice-i-snow
最后编辑者: FredWe,