Skip to content

Undo Redo remove my object's attributes #54

@leacumont

Description

@leacumont

Hello there,
I'm using fabric-js v5.2.4. and the latest version of fabric-history.
I added objects on my canvas, adding some attributes like an id, then constraint like lockration:true.

let rect = new fabric.Rect({
    fill: '#FF5733',
    id: "rect",
    width: fabric.util.parseUnit('100mm'),
    height: fabric.util.parseUnit('50mm'),
    lockRotation: true,
});

let square = new fabric.Rect({
    fill: '#FFC300',
    id: "square",
    width: fabric.util.parseUnit('50mm'),
    height: fabric.util.parseUnit('50mm'),
    lockRotation: true
});

let circle = new fabric.Circle({
    fill: '#0AD6C3',
    id: "circle",
    radius: fabric.util.parseUnit('50mm'),
    lockRotation: true
});

I'm using id for specifying a square and a rectangle
Then I'm setting their constraints like this:
rect.setControlsVisibility({mtr: false}); square.setControlsVisibility({mtr: false, mt: false, mr: false, mb: false, ml: false}); circle.setControlsVisibility({mtr: false});

But my constraints disappear when I use the function undo or redo, as long as my 'id' on each object.
Here's the code I'm using fo undo and redo functions.

document.addEventListener("keydown", function(e) {
    if ( e.ctrlKey && e.keyCode == 90 ) {
        canvas.toObject();
        canvas.undo();
        redefineRules();
    }
    else if( e.ctrlKey && e.keyCode == 89 ) {
        canvas.toObject();
        canvas.redo();
        redefineRules();
    }
})

function redefineRules() {
    canvas.getObjects().forEach(obj => {
        obj.set({
            lockRotation: true,
        });
        console.log(obj);
        switch (obj.id) {
            case "rect" || "circle":
                obj.setControlsVisibility({mtr: false});
                break;
            case "square":
                obj.setControlsVisibility({mtr: false, mt: false, mr: false, mb: false, ml: false});
                break;
            default:
                break;
        }
    });
}

But of course on my redefineRules() function, obj.id doesn't exist.

Can someone help me please ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions