0

please i want to know how to draw a line between two icons with canvas that will be draggable and resizable with the movement of icons?? I have a list of icons and I'll make a schema with these icons. When I'll drag these icons from the list to another div I want to connect them with a line that must be resizable and draggable with the movement of icons. here is the complete code to draw a line with canvas, but it misses how this line will be resizable and draggable

jsbin.com/hasuqehupo/1/edit?html,js,output

<!DOCTYPE html>
<html>
<head>
<title>test Line</title>
<style type="text/css">
canvas {
    border: 5px dashed rgb(205, 200, 200);
}
</style>
</head>
<body>
<canvas id="canvas" width="1000" height="500"></canvas>
<!--ScriptDrawLine-->
<script type="text/javascript" id="Line">
var clicks = 0;
var lastClick = [0, 0];

document.getElementById('canvas').addEventListener('click', drawLine, false);

function getCursorPosition(e) {
    var x;
    var y;

    if (e.pageX != undefined && e.pageY != undefined) {
        x = e.pageX;
        y = e.pageY;
    } else {
        x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }

    return [x, y];
    }

function drawLine(e) {
    context = this.getContext('2d');

    x = getCursorPosition(e)[0] - this.offsetLeft;
    y = getCursorPosition(e)[1] - this.offsetTop;

    if (clicks != 1) {
        clicks++;
    } 
    else {
        context.beginPath();
        context.moveTo(lastClick[0], lastClick[1]);
        context.lineTo(x, y, 6);
        context.strokeStyle = '#000000';
        context.stroke();
        clicks = 0;
    }

        lastClick = [x, y];
    };
</script>
<!--ScriptDrawLine-->

</body>
</html>
  • Can you provide a working code such as snippet or bin? – Mosh Feu May 30 '16 at 15:28
  • Sorry i dont understand your request – Radhi BRIK May 30 '16 at 15:47
  • I mean you can those tools to create working demo of your code so we could better understand the problem and what you have done so far. See the links: [snippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/), [bin](http://jsbin.com/?html,js,output) – Mosh Feu May 30 '16 at 15:48
  • http://jsbin.com/hasuqehupo/1/edit?html,js,output – Radhi BRIK May 30 '16 at 15:56
  • My goal is to connect two icons with a line, but since the icons are draggable I want to know how the line will be drawn resizable and draggable with icons – Radhi BRIK May 30 '16 at 15:58

0 Answers0