Drawing of polylines on canvas

Next code is drawing polyline on canvas by clicking on it. Now i need help to upgrade this task to have option to conclude one polyline with doubleclick.  So i might then draw another from start.
And to include mouse move handler to draw line while moving of mouse over canvas.
(left click of mouse would be the beginning of line and then line would follow actual mouse position until mouse would be pressed  to get next point, and so on until doubleclick).

Thank you.

<!DOCTYPE html>
<html>
<head>
<title>HTML, CSS and JavaScript demo</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.2.1/fabric.js"></script>
</head>
<body>
<!-- Start your code here -->

<canvas id="c" width="1500" height="1500" ></canvas>
  <script>
    (function() {
var canvas = new fabric.Canvas('c');
var needFirstPoint = true;  
var a,b;  
function drawNextLine(ctx, x, y) {
    if (needFirstPoint) {
        ctx.lineWidth = 5;
        ctx.beginPath();
        ctx.moveTo(x, y);
        needFirstPoint = false;
    }
    else {
      
        ctx.lineTo(x, y);
        ctx.stroke();
      
    }
}


canvas.on('mouse:down', function(e) {        
        var offset = fabric.util.getElementOffset(canvas.lowerCanvasEl);
        var ctx = canvas.getContext('2d');
         a = e.e.clientX- offset.left;
         b = e.e.clientY- offset.top;
        
        canvas.on('mouse:up',function(ev){
        
         drawNextLine(ctx,a,b);        
        
    });
        
});
})();
  </script> 
<!-- End your code here -->
</body>
</html>

Open in new window

Ivan GolubarAsked:
Who is Participating?
 
Leonidas DosasConnect With a Mentor Commented:
Check this:
<!DOCTYPE html>
<html>
<head>
<title>HTML, CSS and JavaScript demo</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.2.1/fabric.js"></script>
</head>
<body>
<!-- Start your code here -->

<canvas id="c" width="1500" height="1500" ></canvas>
  <script>
    (function() {
var canvas = new fabric.Canvas('c');
var needFirstPoint = true;  
var a,b,flag;
var clicks=0;  
function drawNextLine(ctx, x, y) {
    if (needFirstPoint) {
        ctx.lineWidth = 5;
        ctx.beginPath();
        ctx.moveTo(x, y);
        needFirstPoint = false;
    }
    else {
      
        ctx.lineTo(x, y);
        ctx.stroke();
      
    }
}


canvas.on('mouse:down', function(e) {
if(flag!==false){
clicks++;
        setTimeout(function(){
		clicks=0;
		},200);		
        if(clicks===1){        
        var offset = fabric.util.getElementOffset(canvas.lowerCanvasEl);
        var ctx = canvas.getContext('2d');
         a = e.e.clientX- offset.left;
         b = e.e.clientY- offset.top;
        
        canvas.on('mouse:up',function(ev){
        
         drawNextLine(ctx,a,b);        
        
    });
	}else{
	flag=false;
	}
	}else{
	return;
	}
	
      
});

})();
  </script> 
<!-- End your code here -->
</body>
</html>

Open in new window

0
 
Ivan GolubarAuthor Commented:
It does not stops process of creating of one polyline, but of also any other . After double click app must be rady for drawing of another polyline.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.