Fabricjs getObjects返回对象,但长度为0

时间:2017-02-24 15:28:44

标签: javascript fabricjs

这是我的js代码:

for (tagTemplateImage in tagTemplateImages) {
    var templates = tagTemplateImages[tagTemplateImage];
    for (templateId in templates) {
        $('.template-container').prepend('<div class="canvas col-lg-12" data-template-id="' + templateId + '"><canvas id="c-' + canvasIndex + '" data-index="' + canvasIndex + '" data-template-id="' + templateId + '" width="433" height="433" style="border:1px solid #ccc"></canvas></div>');
        canvas[canvasIndex] = new fabric.Canvas('c-' + canvasIndex, { preserveObjectStacking: true });
        canvas[canvasIndex].loadFromJSON(templates[templateId], canvas[canvasIndex].renderAll.bind(canvas[canvasIndex]));
        var canvastObjects = canvas[canvasIndex].getObjects();
        console.log(canvastObjects); //--> objects with properties
        console.log(canvastObjects.length); //--> 0
        for (o in canvastObjects) {
            console.log(o);//--> nothing shows here
        }
    }
}

在for循环部分,它不会记录任何内容。

Here is the image for the logging of objects

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

试试这个:

console.log(canvastObjects[o]);//--> something here now

由于o是一个计数器,您可能希望将其设为i

更新,我认为控制台应该显示这样的东西。所以我不确定你是否真的要收回这些物品。你能扩展一个吗?

enter image description here