Draw a polyline by clicking on canvas

I am trying to draw polyline by clicking on canvas. I am using library from  fabrics.js. But no result with next code.
Actually i would reader have an example with  "real" polyline because i need a polyline which is one element and not sum of many lines.
Also there  should be also a  way how to define end of one polyline.

 var canvas = new fabric.Canvas('c');
var needFirstPoint = true;

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');
        var x = e.e.clientX- offset.left;
        var y = e.e.clientY- offset.top;
       drawNextLine(ctx, x, y);
}

Open in new window

Ivan GolubarAsked:
Who is Participating?
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:
Something like 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;

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');
        var x = e.e.clientX- offset.left;
        var y = e.e.clientY- offset.top;
        canvas.on('mouse:up',function(ev){       
         drawNextLine(ctx, x, y);
    });
         
});
})();
  </script>
<!-- End your code here -->
</body>
</html>

Open in new window

0
Ivan GolubarAuthor Commented:
Thank you.

When i click for  4.  point i get also connection between  1. and 3. point.
How to get ride of this?
0
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;  
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

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:
Ok.
Thank you.  
Just last thing. If is needed i will open another topic.
What would be the simplest option to finish one polyline? (so then you could start of drawing of another)
0
Leonidas DosasCommented:
I am thinking with a double click event or right click event
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.

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.