Resize HTML5 canvas to fit window Resize HTML5 canvas to fit window javascript javascript

Resize HTML5 canvas to fit window


I believe I have found an elegant solution to this:

JavaScript

/* important! for alignment, you should make things * relative to the canvas' current width/height. */function draw() {  var ctx = (a canvas context);  ctx.canvas.width  = window.innerWidth;  ctx.canvas.height = window.innerHeight;  //...drawing code...}

CSS

html, body {  width:  100%;  height: 100%;  margin: 0;}

Hasn't had any large negative performance impact for me, so far.


The following solution worked for me the best. Since I'm relatively new to coding, I like to have visual confirmation that something is working the way I expect it to. I found it at the following site:http://htmlcheats.com/html/resize-the-html5-canvas-dyamically/

Here's the code:

<!DOCTYPE html><head>    <meta charset="utf-8">    <title>Resize HTML5 canvas dynamically | www.htmlcheats.com</title>    <style>    html, body {      width: 100%;      height: 100%;      margin: 0px;      border: 0;      overflow: hidden; /*  Disable scrollbars */      display: block;  /* No floating content on sides */    }    </style></head><body>    <canvas id='c' style='position:absolute; left:0px; top:0px;'>    </canvas>    <script>    (function() {        var        // Obtain a reference to the canvas element using its id.        htmlCanvas = document.getElementById('c'),        // Obtain a graphics context on the canvas element for drawing.        context = htmlCanvas.getContext('2d');       // Start listening to resize events and draw canvas.       initialize();       function initialize() {           // Register an event listener to call the resizeCanvas() function            // each time the window is resized.           window.addEventListener('resize', resizeCanvas, false);           // Draw canvas border for the first time.           resizeCanvas();        }        // Display custom canvas. In this case it's a blue, 5 pixel         // border that resizes along with the browser window.        function redraw() {           context.strokeStyle = 'blue';           context.lineWidth = '5';           context.strokeRect(0, 0, window.innerWidth, window.innerHeight);        }        // Runs each time the DOM window resize event fires.        // Resets the canvas dimensions to match window,        // then draws the new borders accordingly.        function resizeCanvas() {            htmlCanvas.width = window.innerWidth;            htmlCanvas.height = window.innerHeight;            redraw();        }    })();    </script></body> </html>

The blue border shows you the edge of the resizing canvas, and is always along the edge of the window, visible on all 4 sides, which was NOT the case for some of the other above answers. Hope it helps.


Basically what you have to do is to bind the onresize event to your body, once you catch the event you just need to resize the canvas using window.innerWidth and window.innerHeight.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title>Canvas Resize</title>    <script type="text/javascript">        function resize_canvas(){            canvas = document.getElementById("canvas");            if (canvas.width  < window.innerWidth)            {                canvas.width  = window.innerWidth;            }            if (canvas.height < window.innerHeight)            {                canvas.height = window.innerHeight;            }        }    </script></head><body onresize="resize_canvas()">        <canvas id="canvas">Your browser doesn't support canvas</canvas></body></html>