首页 文章

从Fabric.js中的JSON加载加载默认属性

提问于
浏览
2

我有画布,其中包含 centeredRotation:true 的对象和其他对象 selectable:false 当我将此画布转换为JSON并重新加载它时 . 对象具有默认属性 . 即没有居中的旋转和物体是可选择的 . 我可以知道为什么会这样吗?显然,像centralRotation,selectable这样的属性不包含在JSON中 .

{“type”:“rect”,“originX”:“left”,“originY”:“top”,“left”:92,“top”:53,“width”:150,“height”:150, “填充”: “#778899”, “中风”:NULL, “strokeWidth”:1, “strokeDashArray”:NULL, “strokeLineCap”: “对接”, “strokeLineJoin”: “斜切”, “strokeMiterLimit”:10,”将scaleX “:1,” 的scaleY “:1,” 角度 “:0,” flipX “:假的,” flipY “:假的,” 不透明度 “:0.4,” 影子 “:空,” 可见 “:真实的,” clipTo” :空 “的backgroundColor”: “”, “RX”:0, “RY”:0, “×”:0, “Y”:0}], “背景”: “”}

如何在加载对象时设置这些?

2 回答

  • 6

    您需要在 toJSON 期间包含它们:

    canvas.toJSON([ 'centeredRotation', 'selectable' ]);
    

    请参阅documentation for toJSON,其中描述了此"propertiesToInclude"参数并有一些示例 .

  • 0

    您还可以在创建新对象之前在主对象上指定此项 . 这保证了我下次从json加载对象时,selectable将保存在“toObject”/“toJSON”函数中 .

    // Add 'selectable' to every object via the main class "Object"
    fabric.Object.prototype.toObject = (function (toObject) {
        return function () {
            return fabric.util.object.extend(toObject.call(this), {
                selectable: this.selectable
            });
        };
    })(fabric.Object.prototype.toObject);
    

    我不明白为什么默认情况下这不存在,但我想这是因为它更像是一个州与一个 property ?

相关问题