@Alex が言うように、関数を使用しgetComputedStyle
てキャンバスの現在のサイズを取得できます。
しかし...window.innerHeight
キャンバスを常にフルスクリーンの幅と高さにするには、ブラウザのサイズが変更された場合でも、キャンバスのサイズをおよびに変更する関数内で描画ループを実行する必要がありますwindow.innerWidth
。
それができたら、通常の関数canvas.height
とcanvas.width
関数を使用して、キャンバスのサイズを適切に取得できます。
(function() {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d');
// resize the canvas to fill browser window dynamically
window.addEventListener('resize', resizeCanvas, false);
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
/* Your drawings need to be inside this function
* to avoid canvas to be cleared. */
drawStuff();
}
resizeCanvas();
function drawStuff() {
console.log(canvas.height); //925
console.log(canvas.width); //1627
// do your drawing stuff here
}
})();
プラス:この CSS ハックを使用して、問題なくキャンバスをフル サイズにします。
/* remove the top and left whitespace */
* { margin:0; padding:0; }
/* just to be sure these are full screen*/
html, body { width:100%; height:100%; }
/* To remove the scrollbar */
canvas { display:block; }