How do I change a value for a Fabric.js object?

后端 未结 2 1983
刺人心
刺人心 2021-01-21 04:28

I have a Fabric.js canvas. I also have a javascript that has a function that gets called when a button is pressed. I know how to get the active object, canvas

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-21 05:08

    Apparently setters are removed in the latest version of Fabric.js (using version 2.7.0). You can now simply set the properties on the object directly:

    // Set your new property values
    object.width = val;
    object.height = val;
    object.strokeWidth = val;
    object.left = val;
    object.top = val;
    
    // Then you mark the object as "dirty" and render the canvas:
    object.dirty = true;
    canvas.renderAll();
    

    Alternatively you can set all properties at once using the set method an object like this:

    // Set all properties at once using the set method
    object.set({ 
        width: val, 
        height: val,
        strokeWidth: val,
        left: val,
        top: val
    });
    
    // In this case the setter marks the object as "dirty" so you only need to call renderAll
    canvas.renderAll();
    

提交回复
热议问题