最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON.stringify() Unable to save Object ID Fabric JS - Stack Overflow

programmeradmin2浏览0评论

I am setting an id attribute for a Group Object

group.id = "N";
canvas.add(group);
canvas.renderAll();

Now, When I am serializing the canvas using

JSON.stringify(canvas);

And then reloading the Canvas using the Json Saved

canvas.loadFromJSON(canvas_json , canvas.renderAll.bind(canvas) , function(o , obj)
{
    if(obj.type == 'group')
    {
       console.log('OBJ ID -'+obj.id);
    }
});

I get OBJ ID -undefined

I checked a few solutions some involve subclassing, but since I am pretty deep in the application it won't be the best solution for me.

I tried something like

fabric.Object.prototype.toObject = (function (toObject) {
return function () {
    return fabric.util.object.extend(toObject.call(this), {
        id: this.id
    });
};

But it was in vain. Do I have to loop through each object in the canvas and manually add the id attribute to the json? I am sure there must be a better solution than that.

Any help in this regards would be very helpful.

I am setting an id attribute for a Group Object

group.id = "N";
canvas.add(group);
canvas.renderAll();

Now, When I am serializing the canvas using

JSON.stringify(canvas);

And then reloading the Canvas using the Json Saved

canvas.loadFromJSON(canvas_json , canvas.renderAll.bind(canvas) , function(o , obj)
{
    if(obj.type == 'group')
    {
       console.log('OBJ ID -'+obj.id);
    }
});

I get OBJ ID -undefined

I checked a few solutions some involve subclassing, but since I am pretty deep in the application it won't be the best solution for me.

I tried something like

fabric.Object.prototype.toObject = (function (toObject) {
return function () {
    return fabric.util.object.extend(toObject.call(this), {
        id: this.id
    });
};

But it was in vain. Do I have to loop through each object in the canvas and manually add the id attribute to the json? I am sure there must be a better solution than that.

Any help in this regards would be very helpful.

Share Improve this question asked Jun 7, 2016 at 17:35 Genocide_HoaxGenocide_Hoax 8532 gold badges22 silver badges43 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

Check out the function toObject(propertiesToInclude) on the canvas object (fabric.js doc). You should use this method to "export" the canvas before using JSON.stringify to serializing. The function allows you to provide a list of additional properties that should be included in the serialization.

When deserializing you just use the reviver function to set the needed property from o to obj (obj.id = o.id).

Putting it all together:

var canvas, group; // your canvas and group objects
group.id = 'N';
canvas.add(group)
canvas.renderAll();

var canvasJson = JSON.stringify(canvas.toObject(['id']));

canvas.loadFromJSON(canvasJson, canvas.renderAll.bind(canvas), function(jsonObject, fabricObject) {
    // jsonObject is the object as represented in the canvasJson
    // fabricObject is the object that fabric created from this jsonObject
    if (fabricObject.type === 'group') {
        fabricObject.id = jsonObject.id
    }
});
发布评论

评论列表(0)

  1. 暂无评论