<i id='jPM9o'><tr id='jPM9o'><dt id='jPM9o'><q id='jPM9o'><span id='jPM9o'><b id='jPM9o'><form id='jPM9o'><ins id='jPM9o'></ins><ul id='jPM9o'></ul><sub id='jPM9o'></sub></form><legend id='jPM9o'></legend><bdo id='jPM9o'><pre id='jPM9o'><center id='jPM9o'></center></pre></bdo></b><th id='jPM9o'></th></span></q></dt></tr></i><div id='jPM9o'><tfoot id='jPM9o'></tfoot><dl id='jPM9o'><fieldset id='jPM9o'></fieldset></dl></div>
      <tfoot id='jPM9o'></tfoot>
        <bdo id='jPM9o'></bdo><ul id='jPM9o'></ul>

        <small id='jPM9o'></small><noframes id='jPM9o'>

        <legend id='jPM9o'><style id='jPM9o'><dir id='jPM9o'><q id='jPM9o'></q></dir></style></legend>
      1. Fabric.js - 绘制多个图像 zindex 的问题

        Fabric.js - problem with drawing multiple images zindex(Fabric.js - 绘制多个图像 zindex 的问题)

              • <bdo id='2ycHs'></bdo><ul id='2ycHs'></ul>

                <legend id='2ycHs'><style id='2ycHs'><dir id='2ycHs'><q id='2ycHs'></q></dir></style></legend>
                  <tbody id='2ycHs'></tbody>
              • <i id='2ycHs'><tr id='2ycHs'><dt id='2ycHs'><q id='2ycHs'><span id='2ycHs'><b id='2ycHs'><form id='2ycHs'><ins id='2ycHs'></ins><ul id='2ycHs'></ul><sub id='2ycHs'></sub></form><legend id='2ycHs'></legend><bdo id='2ycHs'><pre id='2ycHs'><center id='2ycHs'></center></pre></bdo></b><th id='2ycHs'></th></span></q></dt></tr></i><div id='2ycHs'><tfoot id='2ycHs'></tfoot><dl id='2ycHs'><fieldset id='2ycHs'></fieldset></dl></div>

                <tfoot id='2ycHs'></tfoot>

                <small id='2ycHs'></small><noframes id='2ycHs'>

                  本文介绍了Fabric.js - 绘制多个图像 zindex 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用这个脚本:

                  var canvas = new fabric.Canvas('game_canvas', { selection: false });
                  
                  fabric.Image.fromURL('images/bg.jpg', function(img) {
                    img.set('left', 1024/2).set('top', 600/2).set('zindex', 0);
                    canvas.add(img);        
                  });
                  
                  fabric.Image.fromURL('images/panel-2-bg-l-r.png', function(img) {
                    img.set('left', 262/2).set('top', (390/2)+110).set('zindex', 1);
                    canvas.add(img);        
                  });
                  
                  fabric.Image.fromURL('images/player-board.png', function(img) {
                    img.set('left', 254/2).set('top', (122/2)).set('zindex', 2);
                    canvas.add(img);        
                  });
                  
                  fabric.Image.fromURL('images/player-board-top-red.png', function(img) {
                    img.set('left', 203/2).set('top', (109/2)).set('zindex', 3);
                    canvas.add(img).bringToFront(img);          
                  });
                  

                  第三张图像绘制工作正常,并在另一张上显示.但是,如果我添加第 4 个,它就会隐藏在第 3 个后面.如果我指定图像的 zindex,它仍然低于 3rd.

                  3rd image draw is working properly and showing one on top of the other. But if I add 4th one it's hiding behind 3rd. If I specify zindex of the image it's still under 3rd only.

                  这有什么问题?我在这里做错了吗?请帮忙.

                  What is wrong with this? Am I doing something wrong here? Please help.

                  谢谢
                  彼得

                  推荐答案

                  这里有几个问题.

                  1) 您不能通过设置图像的 zindex 属性来更改图像的 zindex.织物图像根本没有这样的属性,改变它不会改变画布上图像的 z 索引.这使得所有那些 .set('zindex', 0).set('zindex', 1) 等几乎都是无操作的.

                  1) You can't change zindex of images by setting their zindex property. Fabric images simply don't have such property and changing it doesn't change z index of images on canvas. This makes all those .set('zindex', 0), .set('zindex', 1), etc. pretty much a no-op.

                  2) bringToFront 在这里按预期工作,将最后一张图像一直带到绘图堆栈的顶部.但问题是您最后加载的images/player-board-top-red.png"图像不能保证是最后添加的.这 4 个请求是异步的;它们以任意顺序出现,因此回调也以任意顺序执行.例如,在我的测试中,最后一张图片排在第二位,回调执行,图片被带到前面,但随后被 2 个回调覆盖".

                  2) bringToFront works as expected here, bringing last image all the way to the top of the drawing stack. But the problem is that "images/player-board-top-red.png" image which you're loading last is not guaranteed to be the last one added. Those 4 requests are asynchronous; they are coming in any order, and so callbacks are executed in any order as well. In my test, for example, the last image comes second, callback executes, image is brought to the front, but is then "overwritten" by following 2 callbacks.

                  如何使最后一个图像呈现在顶部?好吧,我们可以简单地检查所有图片是否已加载,然后再尝试将最后一张图片置于顶部:

                  How to make last image render on top? Well, we can simply check that all images are loaded before attempting to bring last one to the top:

                  var img4;
                  // ...
                  function checkAllLoaded() {
                    if (canvas.getObjects().length === 4) {
                      canvas.bringToFront(img4);
                    }
                  }
                  // ...
                  fabric.Image.fromURL('images/1.png', function(img) {
                    img.set('left', 0).set('top', 0);
                    canvas.add(img);
                    checkAllLoaded(img);
                  });
                  // load other images, making sure to include `checkAllLoaded` in callback
                  fabric.Image.fromURL('images/4.png', function(img) {
                    img4 = img.set('left', 0).set('top', 0);
                    canvas.add(img);
                    checkAllLoaded(img);
                  });
                  

                  顺便说一句,不要忘记你可以像这样将一个对象传递给 set 方法:

                  By the way, don't forget that you can pass an object to set method like so:

                  img.set({ left: ..., top: ... });
                  

                  并且由于 set 是可链接的并返回对实例的引用,您甚至可以将其传递给 add,如下所示:

                  And since set is chainable and returns reference to an instance, you can even pass it to add like so:

                  canvas.add(img.set({ left: ..., top: ... }));
                  

                  希望这会有所帮助.

                  这篇关于Fabric.js - 绘制多个图像 zindex 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                  Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                  dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                  What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                  I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                  How to upload image into HTML5 canvas(如何将图像上传到 HTML5 画布中)

                      <small id='ZZ8sK'></small><noframes id='ZZ8sK'>

                      1. <i id='ZZ8sK'><tr id='ZZ8sK'><dt id='ZZ8sK'><q id='ZZ8sK'><span id='ZZ8sK'><b id='ZZ8sK'><form id='ZZ8sK'><ins id='ZZ8sK'></ins><ul id='ZZ8sK'></ul><sub id='ZZ8sK'></sub></form><legend id='ZZ8sK'></legend><bdo id='ZZ8sK'><pre id='ZZ8sK'><center id='ZZ8sK'></center></pre></bdo></b><th id='ZZ8sK'></th></span></q></dt></tr></i><div id='ZZ8sK'><tfoot id='ZZ8sK'></tfoot><dl id='ZZ8sK'><fieldset id='ZZ8sK'></fieldset></dl></div>
                          <tfoot id='ZZ8sK'></tfoot>
                          <legend id='ZZ8sK'><style id='ZZ8sK'><dir id='ZZ8sK'><q id='ZZ8sK'></q></dir></style></legend>
                            <tbody id='ZZ8sK'></tbody>
                          • <bdo id='ZZ8sK'></bdo><ul id='ZZ8sK'></ul>