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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.