【发布时间】:2013-06-18 18:51:27
【问题描述】:
类似于i want a div to take 100% height、100% 宽度或两者兼而有之。我想要一个Canvas 占用 100% 的宽度和 100% 的高度:
(Link to JsFiddle for your perusal)
标记:
<div id="canvasContainer">
<canvas id="myCanvas"></canvas>
</div>
CSS:
html {
width: 100%;
height: 100%;
}
body {
width: 100%;
height: 100%;
margin: 0;
}
#canvasContainer {
width: 100%;
height: 100%;
background-color: green;
}
#myCanvas {
width: 100%;
height: 100%;
}
Javascript:
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(canvas.width, canvas.height);
ctx.stroke();
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(canvas.width, 0);
ctx.lineTo(0, canvas.height);
ctx.stroke();
这会导致某些东西占据浏览器窗口的 100%:
如果你删除Canvas,它会正常工作:
缺点是我没有Canvas。
【问题讨论】:
-
你想达到什么目的?
-
@Ani 100% 包含 div 的画布。最后,我们将把原生 CAD 应用程序转换为画布。第一步:创建一个与 "client docked" div 大小相同的画布。也许我不确定你在问什么:我希望整个画布立即在屏幕上可见 - 没有需要滚动条的溢出。