首页 > 解决方案 > FabricJS - 在添加项目/组时设置 zorder

问题描述

我正在尝试将一组项目添加到画布,但希望在添加时将组设置在 zorder 堆栈的底部。这是我想要做的代码:

    //Reload the SVG now
    var site_url =  "/components/<?=$designObj->svg_filename?>";
    fabric.loadSVGFromURL(site_url, function(objects, options) {
        let componentObj = fabric.util.groupSVGElements(objects, options);
        componentObj.setControlVisible(false);
        componentObj.sendToBack();   //THIS DOESNT WORK - THROWS TYPE ERROR
        componentObj.selectable = false;
        componentObj.jdeation_comp_id = "base";
        componentObj.jdeation_base_svg = "<?=$designObj->svg_filename?>";
        canvas.add(componentObj).renderAll();
    });

调用 sendToBack() 时出现以下错误:

TypeError: Cannot read property 'sendToBack' of undefined

不确定我是否完全理解这里发生的事情,似乎 sendToBack 期望类型不是组或其他东西。有一个更好的方法吗?

标签: fabricjs

解决方案


您不能 .sendToBack()在尚未添加到画布的对象上。使用canvas.insertAt(componentObj, 0)而不是canvas.add(componentObj)指定应将对象添加到堆栈的底部。


推荐阅读