Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

TextMetrics.width

只读的 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
WHATWG HTML Living Standard
TextMetrics.width
Living Standard  

浏览器兼容性

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,