我想创建画布元素以占用窗口的所有空间,而无需滚动。我执行以下操作:
- 重置边距和填充
- 取窗口大小
- 删除 body 的所有子项
- 创建画布,将画布大小设置为窗口大小
- 将画布添加到正文
它可以工作,但我在页面底部多取了一些像素,浏览器上下滚动以显示这些像素。我在 iPad 和 Android (Galaxy Nexus) 上对其进行了测试——效果相同,所以它不像是一个包,而是一个误会的功能。
你知道这些像素是什么以及如何去除它们吗?
编码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>My Page</title>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" />
<link rel="stylesheet" type="text/css" href="http://meyerweb.com/eric/tools/css/reset/reset.css" />
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function() {
initApp();
});
function initApp() {
windowWidth = $(window).width();
windowHeight = $(window).height();
canvas = document.createElement('canvas');
canvas.width = windowWidth;
canvas.height = windowHeight;
context = canvas.getContext("2d");
var body = document.getElementById("bo");
while (body.firstChild) {
body.removeChild(body.firstChild);
}
body.appendChild(canvas);
// write something for test
context.fillStyle = "#00ff00";
context.fillRect(0, 0, windowWidth, windowHeight);
context.fillStyle = "#ff0000";
context.fillRect(0, 0, 200, 200);
context.fillStyle = "#000000";
context.font = '20px _sans';
context.textBaseline = 'top';
context.fillText ("Canvas " + windowWidth + "x" + windowHeight + "!", 0, 0);
}
</script>
</head>
<body id="bo">
</body>
</html>