html-将画布居中

如何使用HTML5 canvas标记页面,以使div

  1. 占宽度的80%

  2. 具有有效定义比例的相应像素高度和宽度(并在画布拉伸到80%时按比例保持)

  3. 垂直和水平居中

您可以假定canvas是页面上唯一的东西,但是如有必要,可以将其封装在divs中。

user122147 asked 2020-01-13T22:46:50Z
11个解决方案
37 votes

这将使画布水平居中:

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

canvas {
   display: inline;
}

HTML:

<div id="canvas-container">
   <canvas>Your browser doesn't support canvas</canvas>
</div>
Ali OKTAY answered 2020-01-13T22:47:02Z
18 votes

查看当前的答案,我觉得缺少一个简单而干净的修复程序。 以防万一有人经过并寻找正确的解决方案。我用一些简单的CSS和javascript相当成功。

将画布居中到屏幕或父元素的中间。 没有包装。

HTML:

<canvas id="canvas" width="400" height="300">No canvas support</canvas>

CSS:

#canvas {
    position: absolute;
    top:0;
    bottom: 0;
    left: 0;
    right: 0;
    margin:auto;
}

Javascript:

window.onload = window.onresize = function() {
    var canvas = document.getElementById('canvas');
    canvas.width = window.innerWidth * 0.8;
    canvas.height = window.innerHeight * 0.8;
}

像魅力一样工作-经过测试:firefox,chrome

小提琴:[http://jsfiddle.net/djwave28/j6cffppa/3/]

Daniel answered 2020-01-13T22:47:49Z
8 votes

最简单的方法

将画布放入以下段落标签中:

<p align="center">
  <canvas id="myCanvas" style="background:#220000" width="700" height="500" align="right"></canvas>
</p>

J the Helper answered 2020-01-13T22:48:17Z
7 votes

仅在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>
Nickolay answered 2020-01-13T22:48:37Z
4 votes

为了使画布在窗口内居中,应将+“ px”添加到el.style.topel.style.left

el.style.top = (viewportHeight - canvasHeight) / 2 +"px";
el.style.left = (viewportWidth - canvasWidth) / 2 +"px";
Shawn answered 2020-01-13T22:48:57Z
1 votes

使用CSS调整画布大小不是一个好主意。 应该使用Javascript完成。 请参阅下面的功能

function setCanvas(){

   var canvasNode = document.getElementById('xCanvas');

   var pw = canvasNode.parentNode.clientWidth;
   var ph = canvasNode.parentNode.clientHeight;

   canvasNode.height = pw * 0.8 * (canvasNode.height/canvasNode.width);  
   canvasNode.width = pw * 0.8;
   canvasNode.style.top = (ph-canvasNode.height)/2 + "px";
   canvasNode.style.left = (pw-canvasNode.width)/2 + "px";


}

演示在这里:[http://jsfiddle.net/9Rmwt/11/show/]

Diode answered 2020-01-13T22:49:26Z
1 votes

简单:

<body>
    <div>
        <div style="width: 800px; height:500px; margin: 50px auto;">
            <canvas width="800" height="500" style="background:#CCC">
             Your browser does not support HTML5 Canvas.
            </canvas>
        </div>
    </div>
</body>
user2664108 answered 2020-01-13T22:49:45Z
0 votes

如果没有JavaScript,画布就一无所有,请使用JavaScript工具进行大小调整和定位(您知道:onresizeposition:absolute等)

Thomas Broyer answered 2020-01-13T22:50:06Z
0 votes

关于CSS建议:

#myCanvas { 
 width: 100%;
 height: 100%;
}

按照标准,CSS不会调整画布坐标系的大小,而是会缩放内容。 在Chrome中,提到的CSS将放大或缩小画布以适合浏览器的布局。 在典型的坐标系小于浏览器尺寸(以像素为单位)的情况下,这实际上会降低图形的分辨率。 它也很可能导致非比例绘制。

jerseyboy answered 2020-01-13T22:50:31Z
0 votes

用div包装它应该起作用。 我在Firefox,Fedora 13(演示)上的Chrome中对其进行了测试。

#content {
   width: 95%;
   height: 95%;
   margin: auto;
}

#myCanvas {
   width: 100%;
   height: 100%;
   border: 1px solid black;
}

画布应该放在标签中

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

让我知道它是否有效。 干杯。

Sainath Mallidi answered 2020-01-13T22:50:59Z
0 votes

与上面的Nickolay相同,但在IE9和chrome上进行了测试(并删除了额外的渲染):

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>

仅当我添加px时,顶部和左侧偏移量才有效。

laishiekai answered 2020-01-13T22:51:28Z
translate from https://stackoverflow.com:/questions/1152203/centering-a-canvas