如何居中画布?

时间:2013-08-19 13:13:51

标签: canvas html5-canvas center

如何将画布放在身体中心(水平和垂直)? 我想以此为中心:http://invisiblelabs.net/ex4.html

1 个答案:

答案 0 :(得分:0)

在头标记中添加:

<style type="text/css">
#box {
height:400px;
width:200px;
background:#FFF;
position:absolute;
left:50%;
top:50%;
margin:-150px 0 0 -250px;
}

.thecanvas {
height:200px;
width:100px;
background:#FFF;
position:absolute;
}
</style>

并将其替换为当前的canvas元素:

<div id="box">
<canvas id="thecanvas" width="400" height="200">
    Your browser does not support the HTML5 canvas element.
</canvas>
</div>  

我为我工作,我希望它也适合你。