Fabric Js -- iText field not exported by JSON.stringify(canvas)

Viewed 190

I met some problems while trying to export an IText-field by JSON.stringify(canvas).
As it appears to me, the properties 'text', 'fontStyle', 'fontSize' and 'fontFamily' seem not to be exported by the JSON.stringify(canvas) function, while the style attributes (underline, superscript and so on) are exported correctly.
Especially the missing 'text' property throws the TypeError: undefined is not an object (evaluating 't.split'), if I try to restore the canvas with the loadFromJSON function.

I defined the IText object in the following way:

    var myText  = new fabric.IText('', {
        left:        pointer.x,
        top:         pointer.y,
        originX:     'center',
        originY:     'center',
        fontFamily:  'Arial',
        fontSize:     16,
        editable:     true,
        selectable:   true,
        hasBorders:   true,
        hasControls:  false,
        treeNodeName: newNode
    });

A work around like the following solves the problem:

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

Now I'm wondering whether this is the right approach or whether these properties shouldn't be dealt with by the JSON export function.

0 Answers
Related