HTMLCanvasElement.height property is a positive integer 反射 height HTML attribute of the <canvas> element interpreted in CSS pixels. When the attribute is not specified, or if it is set to an invalid value, like a negative, the default value of 150 被使用。

This is one of the two properties, the other being HTMLCanvasElement.width , that controls the size of the canvas.

句法

var pxl = canvas.height;
canvas.height = pxl;
					

范例

Given this <canvas> 元素:

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

You can get the height of the canvas with the following code:

var canvas = document.getElementById('canvas');
console.log(canvas.height); // 300
					

规范

规范 状态 注释
HTML 实时标准
The definition of 'HTMLCanvasElement.height' in that specification.
实时标准 无变化从最新快照起, HTML5
HTML 5.1
The definition of 'HTMLCanvasElement.height' in that specification.
推荐
HTML5
The definition of 'HTMLCanvasElement.height' in that specification.
推荐 Snapshot of the HTML 实时标准 containing the initial definition.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
height Chrome 1 Edge 12 Firefox 1.5 IE 9 Opera 9 Safari 3 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: