简体   繁体   English

将JSON加载到画布后无法获取对象-Fabric JS

[英]Having trouble with getting Objects after loading JSON to canvas - Fabric JS

I am building a template tool with Fabric.JS . 我正在用Fabric.JS构建模板工具。 I have been able to successful serialize a canvas into DatalessJSON , and then deserialize the DatalessJSON back onto the canvas. 我已经能够成功地将画布序列化为DatalessJSON ,然后将DatalessJSON反序列DatalessJSON回到画布上。 However, I am having an issue trying to work with the objects once the JSON has been loaded. 但是,一旦加载了JSON尝试使用对象就会遇到问题。

 function loadTemplate() {


 var template = $('#JSONserial').val();
 canvas.clear();
 canvas.loadFromDatalessJSON(template);
 canvas.renderAll();

 var allObjects = canvas.getObjects();

 console.log(allObjects);

So here, The canvas is definitely loaded with the JSON successfully, and all of the objects appear no problem. 因此,在这里,画布肯定已成功加载了JSON ,并且所有对象似乎都没有问题。 However, when I try the getObjects function, the console.log statement returns "[]" 但是,当我尝试使用getObjects函数时, console.log语句返回"[]"

I have extended the toObjects function with the 'name' attribute so that I can identify each of the JSON loaded objects, but I can't even get that far as I am unable to pull the objects at all. 我已使用'name'属性扩展了toObjects函数,以便可以识别每个JSON加载的对象,但是我什至无法做到,因为我根本无法拉出对象。

Any ideas? 有任何想法吗?

loadFromDatalessJson is asyncronous. loadFromDatalessJson是异步的。

loadFromDatalessJSON(json, callback, reviver)

Use the callback to run code at the end of object loading. 使用回调在对象加载结束时运行代码。

 function loadTemplate() {
 var template = $('#JSONserial').val();
 canvas.clear();
 var allObjects = [];
 canvas.loadFromDatalessJSON(template, function(){
   allObjects = canvas.getObjects();
   console.log(allObjects);
   canvas.renderAll();
 });

}

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

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