Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect draggable divs

I have some divs that I clone and can drag and drop in a area, now, I want to connect by lines the divs and if I move the divs, this lines must move too. Something like a flow diagram, I clone the divs using drag and drop, but still don't know how to do this lines.

Thanks!

like image 863
rafaeltn Avatar asked Jan 13 '11 19:01

rafaeltn


People also ask

Can Div be draggable?

Draggable Div is a kind of element that you can drag anywhere. Here I have created a Draggable profile card.

How do you make a div draggable in HTML?

To make an object draggable set draggable=true on that element. Just about anything can be drag-enabled: images, files, links, files, or any markup on your page.

How do I use draggable JS?

By default, only image and text can be draggable. To drag an image, you simply hold the mouse button down and then move it. To drag the text, you need to highlight some text and drag it in the same way as you would drag an image.

Can any HTML element be draggable?

In HTML, any element can be dragged and dropped.


1 Answers

you can use jsplumb library to achieve this. if you have two divs, div1 and div2,

var endpointOptions = { isSource:true, isTarget:true }; 
var div1Endpoint = jsPlumb.addEndpoint('div1', { anchor:"TopCenter" }, endpointOptions );  
var div2Endpoint = jsPlumb.addEndpoint('div2', { anchor:"BottomCenter" }, endpointOptions );  
jsPlumb.connect({ 
    source:div1Endpoint,
    target:div2Endpoint,
    connector: [ "Bezier", 175 ],
    paintStyle:{ lineWidth:5, strokeStyle:'red' }
});

this should establish the connectors. if your divs are movables, then onmove, make a call to jsPlumb.repaint()

Link to jsPlumb demo - https://jsplumbtoolkit.com Jsplumb seems to have become paid now, (revised link above). However they do have a Community Edition

Community Edition This is the open source jsPlumb project hosted on GitHub that was first created in early 2010. It is a view layer technology that provides you with an API to establish connectivity between DOM elements, both programmatically and via mouse/touch events.

The Community Edition is free, and is licensed with an MIT or GPL2 license; you choose whichever suits your needs.

Toolkit Edition The Toolkit Edition wraps the Community edition with a focus on the underlying data model, as well as several useful UI features such as layouts, and a widget that offers pan/zoom functionality. It provides a fast way of building applications with visual connectivity at their core. To get a better feel for the capabilities of the Toolkit Edition, take a look through some of the demos or peruse the documentation.

The Toolkit Edition has a commercial, per-developer, license with optional access to email support (plus updates to new released versions for a year). License terms are available here. Feel free to jump in and buy a license right now using this form!

Roadmaps for both editions can be viewed here.

like image 115
Krishna Avatar answered Sep 29 '22 01:09

Krishna