Save canvas to image with all its object and back to canvas with all objects back for editing them

大城市里の小女人 提交于 2019-12-07 12:03:30

You can save canvas as JSON and then can load it back from JSON

var myJson = JSON.stringify(canvas.toJSON(['left', 'top', 'lockMovementX', 'lockMovementY']));
canvas.loadFromJSON(myJson, function () {
    //render the canvas
    canvas.renderAll();
});

if i am not mistaken you say that the font property is missing?

if this is you problem please take a look , fabric DOES NOT convert all the object properties to Json when you convert the canvas with canvas.toJSON() , so when you are going to get back your canvas , the objects will not be complete (your custom properties will missing),

for example : myObj.material , myObj.size, myObj.clotheType etc.

you have to say to the prototype toObject function which properties you want to be converted also with canvas.toJSON(), in your code, the following is a snippet of my code:

        fabric.Object.prototype.toObject = (function(toObject) {
            return function() {
                return fabric.util.object.extend(toObject.call(this), {
                    objectId: this.objectId,//my custom property
                    _controlsVisibility:this._getControlsVisibility(),//i want to keep controlsVisibility for my reasons
                    typeTable:this.typeTable,//my custom property
                    txtType:this.txtType, //my custom property
                    customOptions:this.customOptions//my custom object property
                });
            };

i hope that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!