如何使用HTML5 canvas
标记页面,以便canvas
占据宽度的80%
具有相应的像素高度和宽度,可有效地定义比例(并且当画布拉伸至80%时按比例保持)
垂直和水平居中
您可以假设canvas
是页面上唯一的内容,但如果需要,可以随意将其封装在div
中。
答案 0 :(得分:17)
查看当前的答案,我觉得缺少一个简单而干净的修复方法。以防有人经过并寻找合适的解决方案。 我用一些简单的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
答案 1 :(得分:8)
将画布放入段落标签中,如下所示:
<p align="center">
<canvas id="myCanvas" style="background:#220000" width="700" height="500" align="right"></canvas>
</p>
&#13;
答案 2 :(得分:7)
仅在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>
答案 3 :(得分:4)
为了使画布在窗口中居中+“px”应添加到el.style.top
和el.style.left
。
el.style.top = (viewportHeight - canvasHeight) / 2 +"px";
el.style.left = (viewportWidth - canvasWidth) / 2 +"px";
答案 4 :(得分:1)
使用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/
答案 5 :(得分:1)
简单:
<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>
答案 6 :(得分:0)
鉴于如果没有JavaScript,画布就没有了,请使用JavaScript工具进行尺寸调整和定位(您知道:onresize
,position:absolute
等。)
答案 7 :(得分:0)
用div包装它应该有效。我在Firefox上测试了它,在Fedora 13上测试了Chrome(demo)。
#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>
让我知道它是否有效。欢呼声。
答案 8 :(得分:0)
关于CSS建议:
#myCanvas {
width: 100%;
height: 100%;
}
按照标准,CSS不会调整画布坐标系的大小,而是缩放内容。在Chrome中,提到的CSS会向上或向下缩放画布以适应浏览器的布局。在典型情况下,坐标系小于浏览器的像素尺寸,这有效地降低了绘图的分辨率。它很可能导致非比例绘图。
答案 9 :(得分:0)
以上来自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
时,顶部和左侧偏移才有效。