简体   繁体   English

Fabric.js行可选属性未保存在画布的JSON中

[英]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. 我有一个画布,我使用fabric.js的线条绘制方法绘制网格线。 I am defining selection property of those lines as false while drawing which works fine as expected. 我将这些行的选择属性定义为false,同时绘制时可以正常工作。 Now when I stringify the canvas and retrieve the JSON output I noticed that rest of the properties were extracted except the selection property. 现在,当我对画布进行字符串化并检索JSON输出时,我注意到除了选择属性之外,其他属性都被提取了。 What do I need to do to keep the selection property intact in the JSON? 我需要做些什么来保持JSON中的选择属性不变? I am using fabric.js-1.4.10 我正在使用fabric.js-1.4.10

You can go with following code and you will have all additional properties when you will call toJSON() method : 您可以使用以下代码,当您调用toJSON()方法时,您将拥有所有其他属性:

                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. 实际上,正如@Abhishek在评论中说的,而不是使用fabric的toJSON()方法或任何其他用于导出svg的方法,只会导出fabric对象的默认属性。 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. 如果你想在导出列表中获得自定义添加的属性,而不是像我在上面的代码中那样覆盖toJSON方法,那么在使用toJSON()方法之后,你将在导出的JSON中使用customproperty。

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

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