Centering a canvas

后端 未结 11 1435
野的像风
野的像风 2021-01-30 10:00

How do I markup a page with an HTML5 canvas such that the canvas

  1. Takes up 80% of the width

  2. Has a corresponding pixel h

相关标签:
11条回答
  • This will center the canvas horizontally:

    #canvas-container {
       width: 100%;
       text-align:center;
    }
    
    canvas {
       display: inline;
    }
    

    HTML:

    <div id="canvas-container">
       <canvas>Your browser doesn't support canvas</canvas>
    </div>
    
    0 讨论(0)
  • 2021-01-30 10:45

    Same codes from Nickolay above, but tested on IE9 and chrome (and removed the extra rendering):

    window.onload = window.onresize = function() {
       var canvas = document.getElementById('canvas');
       var viewportWidth = window.innerWidth;
       var viewportHeight = window.innerHeight;
       var canvasWidth = viewportWidth * 0.8;
       var canvasHeight = canvasWidth / 2;
    
       canvas.style.position = "absolute";
       canvas.setAttribute("width", canvasWidth);
       canvas.setAttribute("height", canvasHeight);
       canvas.style.top = (viewportHeight - canvasHeight) / 2 + "px";
       canvas.style.left = (viewportWidth - canvasWidth) / 2 + "px";
    }
    

    HTML:

    <body>
      <canvas id="canvas" style="background: #ffffff">
         Canvas is not supported.
      </canvas>
    </body>
    

    The top and left offset only works when I add px.

    0 讨论(0)
  • 2021-01-30 10:48

    Tested only on Firefox:

    <script>
    window.onload = window.onresize = function() {
        var C = 0.8;        // canvas width to viewport width ratio
        var W_TO_H = 2/1;   // canvas width to canvas height ratio
        var el = document.getElementById("a");
    
        // For IE compatibility http://www.google.com/search?q=get+viewport+size+js
        var viewportWidth = window.innerWidth;
        var viewportHeight = window.innerHeight;
    
        var canvasWidth = viewportWidth * C;
        var canvasHeight = canvasWidth / W_TO_H;
        el.style.position = "fixed";
        el.setAttribute("width", canvasWidth);
        el.setAttribute("height", canvasHeight);
        el.style.top = (viewportHeight - canvasHeight) / 2;
        el.style.left = (viewportWidth - canvasWidth) / 2;
    
        window.ctx = el.getContext("2d");
        ctx.clearRect(0,0,canvasWidth,canvasHeight);
        ctx.fillStyle = 'yellow';
        ctx.moveTo(0, canvasHeight/2);
        ctx.lineTo(canvasWidth/2, 0);
        ctx.lineTo(canvasWidth, canvasHeight/2);
        ctx.lineTo(canvasWidth/2, canvasHeight);
        ctx.lineTo(0, canvasHeight/2);
        ctx.fill()
    }
    </script>
    
    <body>
    <canvas id="a" style="background: black">
    </canvas>
    </body>
    
    0 讨论(0)
  • 2021-01-30 10:58

    in order to center the canvas within the window +"px" should be added to el.style.top and el.style.left.

    el.style.top = (viewportHeight - canvasHeight) / 2 +"px";
    el.style.left = (viewportWidth - canvasWidth) / 2 +"px";
    
    0 讨论(0)
  • 2021-01-30 10:58

    Given that canvas is nothing without JavaScript, use JavaScript too for sizing and positionning (you know: onresize, position:absolute, etc.)

    0 讨论(0)
  • 2021-01-30 10:58

    Wrapping it with div should work. I tested it in Firefox, Chrome on Fedora 13 (demo).

    #content {
       width: 95%;
       height: 95%;
       margin: auto;
    }
    
    #myCanvas {
       width: 100%;
       height: 100%;
       border: 1px solid black;
    }
    

    And the canvas should be enclosed in tag

    <div id="content">
        <canvas id="myCanvas">Your browser doesn't support canvas tag</canvas>
    </div>
    

    Let me know if it works. Cheers.

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