HTMLCanvasElement.width 属性是一个对应 <canvas> 元素 CSS 像素 width 的正整数. 当这个属性没有指定时, 或者被赋予一个不合法的值, 比如一个负值, 默认使用 300.
这是其中之一,另一个是 HTMLCanvasElement.height, 它们控制了 canvas 的大小尺寸.
语法
var pxl = canvas.width; canvas.width = pxl;
示例
有这样一个 <canvas> 元素:
<canvas id="canvas" width="300" height="300"></canvas>
你能够通过下面的代码得到 canvas 的宽度:
var canvas = document.getElementById('canvas');
console.log(canvas.width); // 300
规范
| Specification | Status | Comment | 
|---|---|---|
| HTML Living Standard HTMLCanvasElement.width  | 
   Living Standard | No change since the latest snapshot, HTML5 | 
| HTML 5.1 HTMLCanvasElement.width  | 
   Recommendation | |
| HTML5 HTMLCanvasElement.width  | 
   Recommendation | Snapshot of the HTML Living Standard containing the initial definition. | 
浏览器兼容性
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari | 
|---|---|---|---|---|---|---|
| Basic support | 4 | (Yes) | 3.6 (1.9.2) | 9 | 9 | 3.1 | 
| Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | 
|---|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | 1.0 (1.9.2) | (Yes) | (Yes) | (Yes) | 
查看更多
- The interface defining it, 
HTMLCanvasElement. - 另一个控制 canvas 大小的属性, 
HTMLCanvasElement.height.