Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fabric.js line selectable property not getting saved in JSON of the canvas

I have a canvas where I am drawing grid lines using the line drawing method of fabric.js. I am defining selection property of those lines as false while drawing which works fine as expected. Now when I stringify the canvas and retrieve the JSON output I noticed that rest of the properties were extracted except the selection property. What do I need to do to keep the selection property intact in the JSON? I am using fabric.js-1.4.10

like image 983
Abhishek Avatar asked Aug 20 '14 11:08

Abhishek


1 Answers

You can go with following code and you will have all additional properties when you will call toJSON() method :

                var obj = fabric.util.groupSVGElements(objects, options).set({
                    top: 0,
                    left: 0
                });
                obj.toJSON = (function (toJSON) {
                    return function () {
                        return fabric.util.object.extend(toJSON.call(this), {
                            customProperties: this.customProperties
                        });
                    };
                })(obj.toJSON);

Actually as @Abhishek has said in comment than fabric's toJSON() method or any other method that is used to export svg will export only default properties of fabric object. If you want to get your custom added property in export list than you need to override toJSON method as i did in above code, than after when you will use toJSON() method, you will have customproperty in exported JSON.

like image 122
Innodel Avatar answered Oct 21 '22 15:10

Innodel