Css 将画布居中
如何使用HTML5Css 将画布居中,css,html,canvas,Css,Html,Canvas,如何使用HTML5canvas标记页面,以便canvas 占宽度的80% 具有相应的像素高度和宽度,可有效定义比率(当画布拉伸到80%时按比例保持) 垂直和水平居中 您可以假设画布是页面上唯一的东西,但如果需要,可以随意将其封装在divs中。如果画布没有JavaScript,那么也可以使用JavaScript来调整大小和位置(您知道:onresize,位置:绝对,等等)仅在Firefox上测试: <script> window.onload = window.onresize = f
canvas
标记页面,以便canvas
您可以假设
画布
是页面上唯一的东西,但如果需要,可以随意将其封装在div
s中。如果画布没有JavaScript,那么也可以使用JavaScript来调整大小和位置(您知道:onresize
,位置:绝对
,等等)仅在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>
window.onload=window.onresize=function(){
var C=0.8;//画布宽度与视口宽度之比
var W_TO_H=2/1;//画布宽度与画布高度之比
var el=document.getElementById(“a”);
//用于IE兼容性http://www.google.com/search?q=get+viewport+size+js
var viewportWidth=window.innerWidth;
var viewportHeight=window.innerHeight;
var canvasWidth=视口宽度*C;
var canvasHeight=canvasWidth/W_至_H;
el.style.position=“固定”;
el.setAttribute(“宽度”,画布宽度);
el.setAttribute(“高度”,画布高度);
el.style.top=(视口高度-画布高度)/2;
el.style.left=(视口宽度-画布宽度)/2;
window.ctx=el.getContext(“2d”);
ctx.clearRect(0,0,画布宽度,画布高度);
ctx.fillStyle='黄色';
ctx.moveTo(0,画布高度/2);
ctx.lineTo(画布宽度/2,0);
ctx.lineTo(画布宽度、画布高度/2);
ctx.lineTo(画布宽度/2,画布高度);
ctx.lineTo(0,画布高度/2);
ctx.fill()
}
用div包装应该可以。我在Firefox上测试了它,在Fedora13()上测试了Chrome
画布应该用标签包起来
<div id="content">
<canvas id="myCanvas">Your browser doesn't support canvas tag</canvas>
</div>
您的浏览器不支持画布标记
让我知道它是否有效。干杯。为了使画布在窗口内居中,应将“px”添加到
el.style.top
和el.style.left
el.style.top = (viewportHeight - canvasHeight) / 2 +"px";
el.style.left = (viewportWidth - canvasWidth) / 2 +"px";
关于CSS的建议:
#myCanvas {
width: 100%;
height: 100%;
}
根据标准,CSS不会调整画布坐标系的大小,而是缩放内容。在Chrome中,上面提到的CSS将向上或向下缩放画布以适应浏览器的布局。在坐标系小于浏览器尺寸(以像素为单位)的典型情况下,这会有效地降低图形的分辨率。这很可能导致非比例绘图 使用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";
}
此处演示:
.这将使画布水平居中:
#canvas-container {
width: 100%;
text-align:center;
}
canvas {
display: inline;
}
HTML:
您的浏览器不支持画布
与上面Nickolay的代码相同,但在IE9和chrome上进行了测试(并删除了额外的渲染):
HTML:
不支持画布。
顶部和左侧偏移仅在我添加
px
时起作用。查看当前的答案,我觉得缺少了一个简单而清晰的修复方法。以防有人路过并寻找正确的解决方案。
我非常成功地使用了一些简单的CSS和javascript
将画布居中放置在屏幕或父元素的中间。无包装。
HTML:
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
小提琴:简单:
<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>
您的浏览器不支持HTML5画布。
最简单的方法
将画布放入段落标记中,如下所示:
@标记您不能在onload
-事件中调用onload
-事件;)@yckart:我只是复制并粘贴了Nickolay放的东西;我甚至没想过。解释了为什么最初不呈现:-)回答得好。您可以通过在开始时执行单个scale()调用来清理绘图代码,然后可以删除所有与画布相关的绘图代码。这里的示例:#画布容器{宽度:100%;文本对齐:居中;边距:自动;}这对meI有效。我不认为这解决了OPs希望画布比例达到其父级宽度的80%,同时保持其宽高比的问题。我不认为这解决了OPs希望画布比例达到其父级宽度的80%,同时保持其宽高比的问题。
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";
}
<body>
<canvas id="canvas" style="background: #ffffff">
Canvas is not supported.
</canvas>
</body>
<canvas id="canvas" width="400" height="300">No canvas support</canvas>
#canvas {
position: absolute;
top:0;
bottom: 0;
left: 0;
right: 0;
margin:auto;
}
window.onload = window.onresize = function() {
var canvas = document.getElementById('canvas');
canvas.width = window.innerWidth * 0.8;
canvas.height = window.innerHeight * 0.8;
}
<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>