Adding Custom Delete (Back,toFront) Button to Controls

后端 未结 5 1789
悲哀的现实
悲哀的现实 2020-12-30 04:11

I would like to know if there is an easy way to add a delete, bring to front, bring to back function into the existing fabric.js object controls.

A

5条回答
  •  [愿得一人]
    2020-12-30 05:00

    You can overwrite the __onMouseDown function for example like this.

    the target object contains __corner element target.__corner

    Check if this is 'tr' (top right) and delete activeObject

        if (target.__corner === 'tr') {
            if(canvas.getActiveObject()){
                canvas.remove(canvas.getActiveObject());
            }
        }
    

    Full code:

    fabric.Canvas.prototype.__onMouseDown = function (e) {
    
        // accept only left clicks
        var isLeftClick  = 'which' in e ? e.which === 1 : e.button === 1;
        if (!isLeftClick && !fabric.isTouchSupported) {
            return;
        }
    
        if (this.isDrawingMode) {
            this._onMouseDownInDrawingMode(e);
            return;
        }
    
        // ignore if some object is being transformed at this moment
        if (this._currentTransform) {
            return;
        }
    
        var target = this.findTarget(e), 
        pointer = this.getPointer(e, true);
    
        if (target && target.__corner === 'tr') {
            if(this.getActiveObject()){
                this.remove(this.getActiveObject());
            }
        } else {
            // save pointer for check in __onMouseUp event
            this._previousPointer = pointer;
    
            var shouldRender = this._shouldRender(target, pointer),
              shouldGroup = this._shouldGroup(e, target);
    
            if (this._shouldClearSelection(e, target)) {
                this._clearSelection(e, target, pointer);
            } else if (shouldGroup) {
                this._handleGrouping(e, target);
                target = this.getActiveGroup();
            }
    
            if (target && target.selectable && !shouldGroup) {
            this._beforeTransform(e, target);
            this._setupCurrentTransform(e, target);
            }
            // we must renderAll so that active image is placed on the top canvas
            shouldRender && this.renderAll();
    
            this.fire('mouse:down', { target: target, e: e });
            target && target.fire('mousedown', { e: e });
        }
    };
    

提交回复
热议问题