简体   繁体   English

HTML5画布更改图像Z索引

[英]HTML5 Canvas Change Image Z-Index

I am creating an HTML5 Canvas game and I came to a problem, is there a way to change the image layering order? 我在创建HTML5 Canvas游戏时遇到问题,是否可以更改图像分层顺序? Something like Z-Index in normal HTML? 像普通HTML中的Z-Index一样? I can't really put it into words so I'll upload an image of what's happening right now. 我真的无法用语言表达出来,所以我将上传当前情况的图像。

在此处输入图片说明

What I want is the grey part to cover all the other images inside the canvas. 我想要的是灰色部分,可以覆盖画布内的所有其他图像。 The scope already has transparency so it should render whatever is behind it but I don't want to render what is on the grey part. 示波器已经具有透明度,因此它应该呈现其背后的任何内容,但我不想呈现灰色部分的内容。 Is there a way to do that? 有没有办法做到这一点?

Thanks in advance 提前致谢

You can manage this using Compositing in HTML5 Canvas drawing API 您可以使用HTML5 Canvas绘图API中的“合成”来管理此操作

http://www.w3.org/html/wg/drafts/2dcontext/html5_canvas/#compositing http://www.w3.org/html/wg/drafts/2dcontext/html5_canvas/#compositing

With examples 有例子

http://www.tutorialspoint.com/html5/canvas_composition.htm http://www.tutorialspoint.com/html5/canvas_composition.htm

One way to make multiple layers is by using multiple canvases and stacking them over each other with CSS positioning. 制作多层的一种方法是使用多个画布,并通过CSS定位将它们彼此堆叠。

When you want to use only one canvas, you can start your drawing loop with erasing your whole canvas and then redrawing the whole scene from back to front. 当您只想使用一个画布时,可以先擦除整个画布,然后从头到尾重新绘制整个场景,从而开始绘制循环。 When you use requestAnimationFrame , you shouldn't even have any flickering, because the rendering engine of the web browser will take care of that. 当您使用requestAnimationFrame时 ,您甚至都不会闪烁,因为网络浏览器的渲染引擎将解决这一问题。

Another option could be to use globalCompositeOperation . 另一种选择是使用globalCompositeOperation It allows you to mask what you draw with what is already drawn. 它允许您用已绘制的内容遮盖绘制的内容。 When you use the mode destination-over , you will only draw on pixels where nothing else is drawn. 在使用destination-over模式时,您只会在未绘制任何其他内容的像素上进行绘制。

You can append the canvas to a element instead of to the document body, like this: 您可以将画布附加到元素而不是文档主体,如下所示:
Let your div have an id, like this: 让您的div具有一个ID,如下所示:

<div id="myDiv1"></div>  

Then have another div with a different id: 然后使用另一个具有不同ID的div:

<div id="myDiv2"></div>  

These two should be peers, meaning one is not nested inside the other. 这两个应该是对等的,这意味着一个不会嵌套在另一个内部。
Now you can position these any way you like, and give them z-index properties of your choice, either directly on the div elements or externally and referenced in. 现在,您可以按自己喜欢的方式放置这些对象,并直接在div元素上或在外部引用这些对象,并为其选择z-index属性。
Now you have, say: 现在,您可以说:

<div id="myDiv" style="z-index:1"></div>  
<div id="myDiv2" style="z-index:2"></div>  

If these are positioned on top of each other, then myDiv2 will appear on top of myDiv1. 如果它们位于彼此的顶部,则myDiv2将出现在myDiv1的顶部。 Now finally, in Javascript you can append a canvas element to either div (or a different one in each), like this: 现在,最后,在Javascript中,您可以将canvas元素附加到div(或每个div)中,如下所示:

var c = document.createElement("canvas");  
var myCanvas1 = document.getElementById('myDiv1');  
myCanvas1.appendChild(c);  
var d = document.createElement("canvas");  
var myCanvas2 = document.getElementById('myDiv2');  
myCanvas2.appendChild(d);  

and then continue with the contents of the canvases. 然后继续画布的内容。 Finally your can control the stacking through Javascript by changing the z-indexes of the corresponding container divs. 最后,您可以通过更改相应容器div的z索引来通过Javascript控制堆栈。 For example, given the setup above, you can do the following Javascript statement, in some Javascript function of your choice, to put myCanvas1 on top of myCanvas2: 例如,根据上述设置,您可以在所选的某些Javascript函数中执行以下Javascript语句,将myCanvas1置于myCanvas2之上:

document.getElementById('myCanvas1').style.zIndex = 3;  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM