简体   繁体   English

同一画布中的HTML5画布分层对象

[英]HTML5 Canvas Layering Objects within the same canvas

I've looked around for layering objects within the same canvas but haven't found a lot of information about it. 我到处寻找在同一画布中分层的对象,但没有找到很多有关它的信息。

At the moment I've used the multiple canvas technique to layer things on top of each other 此刻,我已经使用了多种画布技术将事物彼此叠加

example: canvas Holder <--- this holds all other canvas's loading canvas menu canvas game canvas background canvas 例如:canvas Holder <---包含所有其他画布的加载画布菜单画布游戏画布背景画布

and by adding them to the " stage = new stage (mainCanvas) " in a specific order, i get the desired layering 然后按特定顺序将它们添加到“ stage = new stage (mainCanvas) ”中,就可以得到所需的分层

stage.addChild(background);
stage.addChild(game);
stage.addChild(menu);
stage.addChild(loading);

This works great, however I'm wondering whether there is a way to change the zIndex of an image added to the 'game' canvas if I had 2 images in that canvas? 这很好用,但是我想知道是否有一种方法可以更改添加到“游戏”画布中的图像的zIndex?

I've seen this sort of thing done in the fieldrunners game, the game follows a grid like format and when you place a shooter in the square above another shooter, is gets repositioned behind it.. 我已经在Fieldrunners游戏中看到了这种情况,游戏遵循类似格网的格式,当您将射击者放置在另一个射击者上方的正方形中时,它会重新放置在其后面。

http://fieldrunnershtml5.appspot.com/#sd --- works in chrome http://fieldrunnershtml5.appspot.com/#sd-适用于Chrome

any ideas how it was done? 有什么想法吗?

Thanks 谢谢

There is no need for multiple canvases. 不需要多个画布。 When you work with a game in canvas 2d you usually clear and redraw the canvas ~60 times per second. 当您在canvas 2d中使用游戏时,通常每秒会清除并重新绘制画布60次。 What you draw last ends up on top. 您最后绘制的内容最终排在最前面。 So in order to simulate layers you sort all game objects in an array based on their z-index then you iterate over all objects in the array, invoking their draw methods. 因此,为了模拟图层,您需要根据数组的z-index对数组中的所有游戏对象进行排序,然后遍历数组中的所有对象,并调用其draw方法。

There is much room for optimizing such a renderer, but this is a basic and simple way to make it work. 优化此类渲染器的空间很大,但这是使其工作的基本且简单的方法。

A canvas is just pixels - it has no "layers". canvas只是像素-没有“图层”。

If you want to perform parallax scrolling, that sort of thing, put multiple canvases in the same place, and use transparency to show the ones behind. 如果要执行视差滚动之类的操作,请将多个画布放在同一位置,然后使用透明效果将其显示在后面。

Your technique using multiple canvases to implement layers is totally good approach. 您使用多个画布实施图层的技术是完全好的方法。 You should also keep track on which layer needs to be cleared/redrawn - for example map should be refreshed only when scrolled or GUI/HUD really doesn't need to be redrawn 60 times per second. 您还应该跟踪需要清除/重绘的图层-例如,仅在滚动或GUI/HUD确实不需要每秒重绘60次时才应刷新map

There is no such thing as z-index or objects in canvas, all mechanisms depends on your own implementation. 画布中没有z-index或对象之类的东西,所有机制都取决于您自己的实现。 For example you can make an array of commands or objects to draw - then sort it by zIndex (or whatever you name it) and execute each element. 例如,您可以制作一个要绘制的命令或对象数组-然后按zIndex(或您命名的名称)对其进行排序并执行每个元素。

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

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