Deserialize a JSON object in Fabric.js

后端 未结 1 1367
执念已碎
执念已碎 2021-01-11 13:21

I am developping a collaborative whiteboard using fabricjs. When a user creates a new fabric object , I serialize it and send it to all other users.

var rec         


        
1条回答
  •  抹茶落季
    2021-01-11 13:29

    You can use fabric.util.enlivenObjects to deserialize json objects. After all objects are deserialized you have to add them:

    objects.forEach(function(o) {
      canvas.add(o);
    });
    

    Here is the complete example - replace obj1, obj2 with your objects. Example is also available on jsfiddle.

    fabric.util.enlivenObjects([obj1, obj2], function(objects) {
      var origRenderOnAddRemove = canvas.renderOnAddRemove;
      canvas.renderOnAddRemove = false;
    
      objects.forEach(function(o) {
        canvas.add(o);
      });
    
      canvas.renderOnAddRemove = origRenderOnAddRemove;
      canvas.renderAll();
    });
    

    0 讨论(0)
提交回复
热议问题