2013-10-13 77 views
1

我發佈的問題是,在給定的示例程序中,當我點擊第一個按鈕時,我做了兩個按鈕比我畫線和當我點擊第二個按鈕時,我畫rectangle.The問題是,當我點擊比線無一不被吸引simultaneously.Here矩形的第二個按鈕是我的代碼在Fabric.js中同時繪製兩個對象

<!DOCTYPE html> 
<html> 
<head> 
    <title>Final layout</title> 
    <script type="text/javascript" src="fabric.min.js"></script> 
    <script type="text/javascript" src="jquery-2.0.2.js"></script> 
    <script type="text/javascript"> 
     //Start when the document is loaded 
     $(document).ready(function(){ 
      //Getting the canvas 
      var canvas1= new fabric.Canvas('canvas'); 
      //Setting the canvas properties 
      canvas1.setHeight(400); 
      canvas1.setWidth(1300); 
      canvas1.setBackgroundColor('lightgrey'); 
      canvas1.renderAll(); 
      //End of canvas1 


      //Binding the functions to button_1 
      $('#1').click(function(){ 

       console.log("button 1 clicked"); 
       canvas1.isDrawingMode=true; // **Enables line drawing 
       canvas1.freeDrawingBrush.width=3; 
       canvas1.freeDrawingBrush.color='blue'; 

       //Adding code to alter line properties 

       canvas1.on('path:created',function(e){ 
        var line1= e.path; 
        line1.lockScalingX; 
        line1.lockScalingY; 
       }); 
      }); 


      //Binding the functions to button_2 
      $('#2').click(function(){ 

       console.log("Button 2 cilcked"); 

       //Declaring the variables 
       var isMouseDown=false; 
       var OriginX=new Array(); 
       var OriginY= new Array(); 
       var refRect; 

       //Setting the mouse events 
       canvas1.on('mouse:down',function(event){ 
        //Defining the procedure 
        isMouseDown=true; 
        OriginX=[]; 
        OriginY=[]; 

        //Getting yhe mouse Co-ordinates 
        var posX=event.e.clientX; 
        var posY=event.e.clientY; 
        OriginX.push(posX); 
        OriginY.push(posY); 

        //Creating the rectangle object 
        var rect=new fabric.Rect({ 
         left:OriginX[0], 
         top:OriginY[0], 
         width:0, 
         height:0, 
         stroke:'red', 
         strokeWidth:3, 
         fill:'white' 
        }); 
        canvas1.add(rect); 

        refRect=rect; //**Reference of rectangle object 

       }); 

       canvas1.on('mouse:move', function(event){ 
        // Defining the procedure 

        if(!isMouseDown) 
        { 
         return; 
        } 
        //Getting yhe mouse Co-ordinates 
        var posX=event.e.clientX; 
        var posY=event.e.clientY; 

        refRect.setWidth(Math.abs((posX-refRect.get('left')))); 
        refRect.setHeight(Math.abs((posY-refRect.get('top')))); 
        canvas1.renderAll(); 


       }); 

       canvas1.on('mouse:up',function(){ 
        //alert("mouse up!"); 
        isMouseDown=false; 
       }); 




      }); 
}); 
    </script> 
</head> 
<body> 
<canvas id="canvas" style="border: 2px solid darkblue"></canvas> 
<div style="position: relative; width: 1300px; height:30px; border: 2px solid black; margin-top: 5px"> 
    <input type="button" id="1" value="pencil"> 
    <input type="button" id="2" value="rectangle"> 
</div> 
</body> 

請讓我知道什麼是這個問題,我應該吃什麼一次繪製一個形狀。

回答

1

您需要將isDrawingMode設置爲false才能停止自由繪製模式。