Search code examples
javascriptfabricjsdrawrectangle

How to stop drawing with Fabric.js


I copied a code that draws a rectangle with the movement of the mouse, using fabric.js. But I need that when the user choose the option, the drawing stops.

I used this fiddle http://jsfiddle.net/a7mad24/aPLq5/

var canvas = new fabric.Canvas('c', { selection: false });

var rect, isDown, origX, origY;

canvas.on('mouse:down', function (o) {
    isDown = true;
    var pointer = canvas.getPointer(o.e);
    origX = pointer.x;
    origY = pointer.y;
    var pointer = canvas.getPointer(o.e);
    rect = new fabric.Rect({
        left: origX,
        top: origY,
        originX: 'left',
        originY: 'top',
        width: pointer.x - origX,
        height: pointer.y - origY,
        angle: 0,
        fill: 'rgba(255,0,0,0.5)',
        transparentCorners: false
    });
    canvas.add(rect);
});

canvas.on('mouse:move', function (o) {
    if (!isDown) return;
    var pointer = canvas.getPointer(o.e);

    if (origX > pointer.x) {
        rect.set({ left: Math.abs(pointer.x) });
    }
    if (origY > pointer.y) {
        rect.set({ top: Math.abs(pointer.y) });
    }

    rect.set({ width: Math.abs(origX - pointer.x) });
    rect.set({ height: Math.abs(origY - pointer.y) });


    canvas.renderAll();
});

canvas.on('mouse:up', function (o) {
    isDown = false;
});

Solution

  • You mean something like a checkbox to enable/disable user input? Try the code snippet below to see what I mean.

    var canvas = new fabric.Canvas('c', { selection: false });
    
    var rect, isDown, origX, origY;
    
    canvas.on('mouse:down', function(o) {
        isDown = true;
        var pointer = canvas.getPointer(o.e);
        origX = pointer.x;
        origY = pointer.y;
        var pointer = canvas.getPointer(o.e);
        
        rect = new fabric.Rect({
            left: origX,
            top: origY,
            originX: 'left',
            originY: 'top',
            width: pointer.x-origX,
            height: pointer.y-origY,
            angle: 0,
            fill: 'rgba(255,0,0,0.5)',
            transparentCorners: false
        });
        canvas.add(rect);
    });
    
    canvas.on('mouse:move', function(o){
        if (!isDown) return;
        var pointer = canvas.getPointer(o.e);
        
        if(origX>pointer.x){
            rect.set({ left: Math.abs(pointer.x) });
        }
        if(origY>pointer.y){
            rect.set({ top: Math.abs(pointer.y) });
        }
        
        rect.set({ width: Math.abs(origX - pointer.x) });
        rect.set({ height: Math.abs(origY - pointer.y) });
        
        
        canvas.renderAll();
    });
    
    canvas.on('mouse:up', function(o){
      isDown = false;
    });
    .container {
      position: relative;
      pointer-events: auto;
    }
    #checkBox:checked ~ .container {
      pointer-events: none;
    }
    <script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
    Disable: <input id="checkBox" type="checkbox">
    <div class="container disabled">
      <canvas id="c" width="500" height="500" style="border:1px solid #ccc"></canvas>
    </div>