如何将两幅画布相互叠加?(How do I center two canvases on top of one another?)

编程入门 行业动态 更新时间:2024-10-22 23:40:50
如何将两幅画布相互叠加?(How do I center two canvases on top of one another?)

我需要在另一个上面显示一个HTML5画布。 我已经按以下方式管理:

<canvas id="lower" width="900" height="550" style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas> <canvas id="upper" width="900" height="550" style="position: absolute; left: 0; top: 0; z-index: 1;"></canvas>

然而,我似乎无法弄清楚如何将这两个画布居中,同时保持一个在另一个之上。 有任何想法吗?

I need to display one HTML5 canvas on top of another. This I have already managed in the following manner:

<canvas id="lower" width="900" height="550" style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas> <canvas id="upper" width="900" height="550" style="position: absolute; left: 0; top: 0; z-index: 1;"></canvas>

However, I can't seem to figure out how to CENTER these two canvases while keeping one on top of the other. Any ideas?

最满意答案

创建一个带有text-align:center的外部容器text-align:center和position:relative ,创建一个position:relative的内部容器,将画布放在内部容器中,删除left:0;top:0; 在两幅画布上,并删除position:absolute在画布的下方。 并确保在html中,较低的画布位于上部画布之前,就像你一样。

#container { text-align:center; position:relative; } #inner_container{ position:relative; } #upper { z-index: 1; } #lower { position:absolute; z-index: 0; }

http://jsfiddle.net/NW6Fx/

编辑 :我相信如果你这样做,就不需要z-index属性。 顺序只是哪个画布首先出现在html中,而position:relative是position:relative 。

Make an outer container with text-align:center and position:relative, make an inner container with position:relative, put the canvases inside the inner container, remove left:0;top:0; on both canvases, and remove the position:absolute on the lower of the canvases. And make sure in the html that the lower canvas comes before the upper canvas, like you have.

#container { text-align:center; position:relative; } #inner_container{ position:relative; } #upper { z-index: 1; } #lower { position:absolute; z-index: 0; }

http://jsfiddle.net/NW6Fx/

Edit: I believe the z-index property isn't needed if you do it this way. The order is just a matter of which canvas comes first in the html and which is position:relative.

更多推荐

本文发布于:2023-08-07 13:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464247.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:画布   如何将   两幅   top   center

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!