Resize HTML5 canvas to fit window

前端 未结 16 1120
遥遥无期
遥遥无期 2020-11-22 12:03

How can I automatically scale the HTML5 element to fit the page?

For example, I can get a

to scale by setting th
相关标签:
16条回答
  • 2020-11-22 12:21

    If you're interested in preserving aspect ratios and doing so in pure CSS (given the aspect ratio) you can do something like below. The key is the padding-bottom on the ::content element that sizes the container element. This is sized relative to its parent's width, which is 100% by default. The ratio specified here has to match up with the ratio of the sizes on the canvas element.

    // Javascript
    
    var canvas = document.querySelector('canvas'),
        context = canvas.getContext('2d');
    
    context.fillStyle = '#ff0000';
    context.fillRect(500, 200, 200, 200);
    
    context.fillStyle = '#000000';
    context.font = '30px serif';
    context.fillText('This is some text that should not be distorted, just scaled', 10, 40);
    /*CSS*/
    
    .container {
      position: relative; 
      background-color: green;
    }
    
    .container::after {
      content: ' ';
      display: block;
      padding: 0 0 50%;
    }
    
    .wrapper {
      position: absolute;
      top: 0;
      right: 0;
      left: 0;
      bottom: 0;
    }
    
    canvas {
      width: 100%;
      height: 100%;
    }
    <!-- HTML -->
    
    <div class=container>
      <div class=wrapper>
        <canvas width=1200 height=600></canvas>  
      </div>
    </div>

    0 讨论(0)
  • 2020-11-22 12:22

    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>
    
    0 讨论(0)
  • 2020-11-22 12:24

    Unless you want the canvas to upscale your image data automatically (that's what James Black's answer talks about, but it won't look pretty), you have to resize it yourself and redraw the image. Centering a canvas

    0 讨论(0)
  • 2020-11-22 12:26

    I think this is what should we exactly do: http://www.html5rocks.com/en/tutorials/casestudies/gopherwoord-studios-resizing-html5-games/

    function resizeGame() {
        var gameArea = document.getElementById('gameArea');
        var widthToHeight = 4 / 3;
        var newWidth = window.innerWidth;
        var newHeight = window.innerHeight;
        var newWidthToHeight = newWidth / newHeight;
    
        if (newWidthToHeight > widthToHeight) {
            newWidth = newHeight * widthToHeight;
            gameArea.style.height = newHeight + 'px';
            gameArea.style.width = newWidth + 'px';
        } else {
            newHeight = newWidth / widthToHeight;
            gameArea.style.width = newWidth + 'px';
            gameArea.style.height = newHeight + 'px';
        }
    
        gameArea.style.marginTop = (-newHeight / 2) + 'px';
        gameArea.style.marginLeft = (-newWidth / 2) + 'px';
    
        var gameCanvas = document.getElementById('gameCanvas');
        gameCanvas.width = newWidth;
        gameCanvas.height = newHeight;
    }
    
    window.addEventListener('resize', resizeGame, false);
    window.addEventListener('orientationchange', resizeGame, false);
    
    0 讨论(0)
  • 2020-11-22 12:27
    (function() {
    
        // get viewport size
        getViewportSize = function() {
            return {
                height: window.innerHeight,
                width:  window.innerWidth
            };
        };
    
        // update canvas size
        updateSizes = function() {
            var viewportSize = getViewportSize();
            $('#myCanvas').width(viewportSize.width).height(viewportSize.height);
            $('#myCanvas').attr('width', viewportSize.width).attr('height', viewportSize.height);
        };
    
        // run on load
        updateSizes();
    
        // handle window resizing
        $(window).on('resize', function() {
            updateSizes();
        });
    
    }());
    
    0 讨论(0)
  • 2020-11-22 12:28

    Setting the canvas coordinate space width and height based on the browser client's dimensions requires you to resize and redraw whenever the browser is resized.

    A less convoluted solution is to maintain the drawable dimensions in Javascript variables, but set the canvas dimensions based on the screen.width, screen.height dimensions. Use CSS to fit:

    #containingDiv { 
      overflow: hidden;
    }
    #myCanvas {
      position: absolute; 
      top: 0px;
      left: 0px;
    } 
    

    The browser window generally won't ever be larger than the screen itself (except where the screen resolution is misreported, as it could be with non-matching dual monitors), so the background won't show and pixel proportions won't vary. The canvas pixels will be directly proportional to the screen resolution unless you use CSS to scale the canvas.

    0 讨论(0)
提交回复
热议问题