嘗試在整個頁面上擴展HTML canvas
,但jQuery width()
和height()
函數正在創建垂直和水平滾動條。如何在不創建滾動條的情況下獲取視口的高度和寬度?
HTML
<body>
<canvas id="bg_canvas" width="" height=""></canvas>
</body>
jQuery的
// Expand canvas to full viewport size
var vp_width = window.clientWidth;
var vp_height = window.clientHeight;
$("#bg_canvas").css({
"width" : vp_width,
"height" : vp_height
});
// Canvas Drawing
var canvas = document.getElementById("bg_canvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle="#cbf7ed";
ctx.fillRect(100,100,vp_width,100);
也嘗試過的Javascript window.innerheight
和window.innerWidth
,但也創造了滾動條。
是'vp_width'返回(/ A)正確的價值?你也可以嘗試使用CSS:'#bg_canvas {width:100vw; height:100vh;}'。 – Alvaro