Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make canvas as big as possible without scrollbars

I'd like to create a web page which contains a canvas object which fills the screen without creating scroll bars.

Setting the canvas width and height to $(window).height() and $(window).width() results in scroll bars. Reducing the width by a few pixels is not reliable because the number I need to reduce by is different depending on the browser.

Is there a cross browser way to do this?

like image 817
rvabdn Avatar asked Dec 12 '25 20:12

rvabdn


1 Answers

In Chrome, this works for me.

<html>
    <head>
        <title></title>
        <script>
           function init() {
                var canvasBG = document.getElementById("test");
                var ctxBG = canvasBG.getContext("2d");

                canvasBG.style.width = window.innerWidth;
                canvasBG.style.height = window.innerHeight;
            };

            window.onload = init;
        </script>
    </head>
    <body>
        <canvas id="test" style="background:#eeeeee; 
               margin:0; padding:0; 
               position:absolute; 
               top:0; left:0">
        </canvas>
    </body>
    </html>

update: You might also want to attached a resize listener to the page so if the user resizes the page you can reset the width/height.

like image 190
Jarrod Avatar answered Dec 14 '25 10:12

Jarrod