Let user delete a selected fabric js object

后端 未结 5 761
青春惊慌失措
青春惊慌失措 2021-02-05 02:03

I have a simple fabric js based application where I will let users add shapes connect them and animate them.

Following is my JS

var canvas; 
window.newA         


        
5条回答
  •  野的像风
    2021-02-05 02:45

    I am using Fabric JS 2.3.6.

    I wanted to allow the user to select one or more objects on the canvas and delete them by clicking the delete button.

    Removed methods from old versions

    The following methods are no longer available since the introduction of ActiveSelection:

    setActiveGroup(group);
    getActiveGroup();
    deactivateAll();
    discardActiveGroup();
    deactivateAllWithDispatch();
    

    Here is my code that works great for me and hopefully you as well.

    $('html').keyup(function(e){
            if(e.keyCode == 46) {
                deleteSelectedObjectsFromCanvas();
            }
    });    
    
    function deleteSelectedObjectsFromCanvas(){
        var selection = canvas.getActiveObject();
        if (selection.type === 'activeSelection') {
            selection.forEachObject(function(element) {
                console.log(element);
                canvas.remove(element);
            });
        }
        else{
            canvas.remove(selection);
        }
        canvas.discardActiveObject();
        canvas.requestRenderAll();
    }
    

提交回复
热议问题