Determining the size of a canvas element
If you stretch the canvas using CSS, you stretch the poor canvas, the default size of which is 300x150 pixels β imagine that you have an image of this size and that you did the same β it will look pretty bad.
The contents of the canvas cannot be stretched using CSS (it acts like an image in this way). You need to explicitly specify the pixel size of the canvas.
This means that you will need to get the size of the parent container and set it as the pixel value on the canvas:
First of all, to get rid of scrollbars, etc., namely CSS:
body, html { width: 100%; height: 100%; margin: 0; padding:0; overflow:hidden; }
Now change this rule:
#myCanvas { background-color: green; position:fixed; left:0; top:0; }
Now reduce your markup to this (if you want the canvas to cover the entire sceeen, you don't need a container other than a window):
<canvas id="myCanvas"></canvas>
Now we can calculate the size:
var canvas; window.onload = window.onresize = function() { canvas = document.getElementById('myCanvas'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; }
What is it.
Working demo (resize window to see).
source share