I’ve created a simple canvas element
<canvas style="width:100%;height:100%;" id="canvas"></canvas>
when I try to get the height, however in javascript:
var canvas = document.getElementById(config.elementId);
console.log(canvas.height); //150
console.log(canvas.width); //300
console.log(canvas.style.height); //100%
console.log(canvas.style.width); //100%
If I inspect the element in chrome’s debugger, and actually mouse over the element, chrome is reporting the element size as
1627x925px
How on earth do I get to the 1627x925px measurements in js?
As @Alex says, you can use the
getComputedStylefunction to get the current size of the canvas.But… to make the canvas full screen width and height always, meaning even when the browser is resized, you need to run your draw loop within a function that resizes the canvas to the
window.innerHeightandwindow.innerWidth.Once you do that, you can use the normal
canvas.heightandcanvas.widthfunctions to get properly the canvas size:Plus: Use this CSS hacks to make the canvas full size without problems: