I am building an animation with HTML5 Canvas and KineticJS. The animation works just fine. However, I've noticed that sometimes on refresh, the layer order of the images I am animating gets rearranged. This switch in layer order seems to happen at random. For instance, I can click refresh 5 times and the layer order is fine... click refresh another 3 times and the layer order rearranges. Can anyone help as to why this random switch in layer order occurs?
相关问题
- Does using percentages instead of pixels change an
- Method for drawing circles in a pyramid pattern
- Resizing a .png image when drawing to HTML5 canvas
- HTML 5 - how to zoom canvas background image
- Canvas fillStyle none in HTML5
相关文章
- Drawing a filled circle in a canvas on mouseclick
- How to destroy / reload the canvas in html 5?
- Canvas to blob on Edge
- How to change one texture image of a 3d model(maya
- How to count Color on Image using HTML canvas getI
- How to freedraw Circle in fabricjs using mouse?
- Constrained/Proportional Scaling in KineticJS
- How to compare two images in Robot Framework
This is your problem right here:
If you disable your browser cache, the problem will occur more often. Using these 3 onload functions, you have no way of controlling which image loads first. Chances are they'll load in order, but sometimes they won't. Whichever order they load in, that
layer
will be added first, and so sometimes they are out of order.If you must use 3 different layers for your 3 images, you can fix this issue by adding the layers in order OUTSIDE of the
onload
functions:This guarantees the order that the layers are added in.
You also need to use the
draw()
function on each layer after the image loads, so that you can see the image on the canvas. Here's the updated jsfiddleSuggestion:
Instead of using 3 layers for your 3 images, use 1 Layer and 3 Groups with each Group containing 1 image (and anything else you need to add within each Group). Like this:
See this question for more information: What are the differences between group and layer in KineticJs
Final Note:
As a last alternative, you could probably also use the
zIndex
property to order which layer (or group!) should appear on top of the others. Kinetic.Container#setZIndex