在我看來,我必須在一個小時內遇難,好像在Firefox,Safari和Chrome中發生的奇怪行爲。這裏是一些代碼:畫布最大化錯誤?
<!doctype html>
<html>
<head>
<title>Watch me grow scrollbars!</title>
</head>
<body onload="resizeCanvas()">
<canvas id="canvas"></canvas>
</body>
</html>
<script type="application/javascript">
function resizeCanvas() {
var canvas = document.getElementById("canvas");
canvas.setAttribute("height", window.innerHeight);
canvas.setAttribute("width", window.innerWidth);
}
</script>
基本上,畫布總是似乎最大化'太多',並且窗口增長兩側的滾動條。我嘗試過使用各種方法獲取文檔尺寸,包括嵌套在100%寬的絕對定位的div中的canvas,以及提取document.documentElement.clientWidth/Height屬性。
爲了確保我不會瘋狂,我放置了一個圖像代替畫布,瞧,相同的代碼完美地將圖像拉伸到文檔尺寸。
我在這裏做錯了什麼?我太累,無法理解。必須......喝點東西。
您可能還需要爲'body'和'canvas'元素設置'margin:0;填充:0;邊框寬度:0;'。但說實話,我在Firefox中的快速測試仍然顯示滾動條。 – Zecc 2011-05-28 18:26:38
爲畫布設置'display:block'就是我想的。 – urraka 2012-12-24 22:48:11