简体   繁体   English

HTML画布分层框架

[英]HTML Canvas Layering Frameworks

I'm building an app using the HTML 5 canvas tag, and so far it works great. 我正在使用HTML 5 canvas标签构建一个应用程序,到目前为止效果很好。 The next step for the app is layering, so that I have a background image and them some more layers on top of that. 该应用程序的下一步是分层,这样我就有了背景图像,并且在它们之上还有更多层。 Right now I'm doing this: 现在我正在这样做:

this.drawMarkers = function(markers) {
    selectedImage = null;
    map.width = map.width;
    for(var i in markers) {
        for(var j in markers[i]) {
            var holder = markers[i][j];
            if(holder !== null) {
                var marker = new Marker(new Cell(holder.row, holder.column), holder.type);
                marker.src = holder.src;
                marker.draw();
            }
        }
    }
    initGrid();
}

where i is the layer and j is the things on that layer. 其中i是层, j是该层的事物。 The initial code for this just drew everything on one layer and it worked great, and this one does too if everything downloads in the correct order. 最初的代码只是将所有内容都吸引到了一层,并且效果很好,如果所有内容都以正确的顺序下载,那么该代码也可以做到。 If not, things get stacked incorrectly. 如果不是这样,东西堆放不正确。 For example, if the background is large and ends up downloading last, it ends up clearing out everything on top of it due to everything being asynchronous. 例如,如果背景很大并且最后下载一次,则由于所有内容都是异步的,因此最终清除了所有内容。

Are there any frameworks out there for adding layering to canvas to avoid this? 是否有用于在画布上添加分层的框架以避免这种情况? I'm fine if things don't load in the correct order, so long as the stacking is preserved. 只要事情没有按正确的顺序加载就可以了,只要可以保留堆栈。

First question to ask yourself: 问自己的第一个问题:

If you draw it repeatedly, say every 100 milliseconds, does the problem go away? 如果您反复绘制它,比如说每100毫秒,问题会消失吗?

If no, the "drawing in the wrong order" is something you are doing and not an async error. 如果不是,则“正在按错误的顺序绘制”是您正在执行的操作,而不是异步错误。 I suspect this might be the case. 我怀疑可能是这种情况。

Images may not draw, but nothing should draw in the wrong order , assuming your draw routine is correct and that you are redrawing everything each time. 图像可能不会绘制,但是如果draw例程正确并且每次都在重新绘制所有内容,则任何绘制都不会以错误的顺序进行。 According to the canvas spec, a call to drawImage does nothing if the image isn't loaded, so incorrect stacking should be impossible unless it is the order of your own calls. 根据canvas规范,如果未加载图像,则对drawImage的调用不会执行任何操作,因此除非您自己进行调用,否则不应该进行错误的堆栈。

In any case, you should wait until all of the images you are going to use have loaded before you draw. 无论如何,您都应等到要使用的所有图像都已加载后再绘制。 Use their onload events and/or $(window).load() or jQuery(document).ready , etc. 使用其onload事件和/或$(window).load()jQuery(document).ready等。

Some other notes: 其他注意事项:

  • Depending on how interactive your canvas is and how the layers work, sometimes it is better to use multiple canvases stacked on top of each other to increase performance, especially if a top layer changes very little and a layer underneath changes often. 根据画布的交互性以及各层的工作方式,有时最好使用多个彼此叠置的画布来提高性能,尤其是在顶层变化很小而其下层经常变化的情况下。
  • If your background is a static file such as a png, set the CSS background-image of the canvas to that image to make the drawing easier on yourself. 如果您的背景是静态文件(例如png),则将画布的CSS背景图像设置为该图像,以使自己绘制起来更容易。

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

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