height属性以像素为单位指定 <canvas>元素的高度,每次重新设置画布的高度或宽度时,都会清除画布内容。
一个高度和宽度为200像素的<canvas>元素:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML <canvas> height 属性使用-菜鸟教程(cainiaojc.com)</title>
</head>
<body>
<canvas id="myCanvas" width="200" height="200" style="border:1px solid">
Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "#92B901";
ctx.fillRect(50, 50, 100, 100);
</script>
</body>
</html>
测试看看 ‹/›IEFirefoxOperaChromeSafari
所有主流浏览器都支持 height 属性。
注意: Internet Explorer 8 及更早IE版本不支持 <canvas> 标签。
height属性以像素为单位指定<canvas>元素的高度。
提示:使用width 属性以像素为单位指定<canvas>元素的宽度。
提示: 每次重新设置画布的高度或宽度时,都会清除画布内容(请看页面底部的示例)。
提示: 请在我们的 HTML Canvas 教程中学习更多有关 <canvas>的知识。
<canvas> 是 HTML5 中的新标签。
<canvas height="pixels">
Value | Description |
---|---|
pixels | 规定以像素计的 canvas 高度(比如 "100px" 或仅仅是 "100")。默认是 "150"。 |
通过设置width或height属性(使用JavaScript)清除画布:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML <canvas> width 属性使用-菜鸟教程(cainiaojc.com)</title>
</head>
<body>
<canvas id="myCanvas" width="200" height="200" style="border:1px solid">
Your browser does not support the HTML5 canvas tag.
</canvas>
<br>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "#92B901";
ctx.fillRect(50, 50, 100, 100);
function clearCanvas() {
c.height = 300;
}
</script>
<button onclick="clearCanvas()">Clear canvas</button>
</body>
</html>
测试看看 ‹/›