Danny Fox
Danny Fox

Reputation: 40719

Why scrollbars appear, when I resize the canvas?

I made a page, where the canvas should fill the window. It works, but for some reason scrollbars appear. I don't know, what is the error.

<!doctype html>
<html>

  <head>
    <title>test</title>
    <meta charset="utf-8" />
    <style type="text/css">
      #canvas {
        background-color: #EEEEEE;
      }
      * {
        margin: 0px;
      }
    </style>
    <script type="text/javascript">
      function getSize() {
        var myWidth = 0, myHeight = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
          //Non-IE
          myWidth = window.innerWidth;
          myHeight = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
          //IE 6+ in 'standards compliant mode'
          myWidth = document.documentElement.clientWidth;
          myHeight = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth ||          document.body.clientHeight ) ) {
          //IE 4 compatible
          myWidth = document.body.clientWidth;
          myHeight = document.body.clientHeight;
        }
        return {x:myWidth,y:myHeight};
      }

      window.onload = function () {
        canvas = document.getElementById('canvas');
        var size = getSize();
        canvas.width = size.x;
        canvas.height = size.y;
      };
    </script>
  </head>

  <body>
    <canvas id="canvas" width="200" height="200"></canvas>
  </body>

</html>

Upvotes: 1

Views: 498

Answers (2)

fixlr
fixlr

Reputation: 429

Switching #canvas to a block did the trick for me:

#canvas {
    display: block;
    background-color: #EEEEEE;
}

Upvotes: 3

Jeffrey Sweeney
Jeffrey Sweeney

Reputation: 6114

It was likely a problem with your CSS.

I moved the 'all elements' up to the top, and made the canvas absolutely positioned just to be safe.

Let me know if this works:

<style type="text/css">
          * {
              margin: 0px;
              padding:0px;
          }
          #canvas {
              position:absolute;
              left:0px;
              top:0px;
              background-color: #EEEEEE;
          }

</style>

Upvotes: 1

Related Questions