HTMLCanvasElement.width

HTMLCanvasElement.width 属性是一个对应 <canvas> 元素 CSS 像素 width 的正整数。当这个属性没有指定时,或者被赋予一个不合法的值,比如一个负值,默认使用 300.

这是其中之一,另一个是 HTMLCanvasElement.height, 它们控制了 canvas 的大小尺寸。

语法

var pxl = canvas.width;
canvas.width = pxl;

示例

有这样一个 <canvas> 元素:

html
<canvas id="canvas" width="300" height="300"></canvas>

你能够通过下面的代码得到 canvas 的宽度:

js
var canvas = document.getElementById("canvas");
console.log(canvas.width); // 300

规范

Specification
HTML Standard
# dom-canvas-height

浏览器兼容性

BCD tables only load in the browser

查看更多