后续的更新会直接在github上,如需查看最新文档,请直接访问原始仓库。
另外用了七牛的测试域名作图片托管,如今发现不少图片都没法显示了,建议直接参看git仓库里的, 仓库中的示例图片不是用的七牛云,都能正常显示。
仓库地址:https://github.com/wangduandu...css
你有没有想过在你的网站上展现图表或者甚至在浏览器应用程序中使用它?用jsPlumb你能够!它是彻底免费的,并根据MIT许可证提供。您能够直接从jsPlumb github网站下载框架。html
该项目主要由Simon Porritt开发,他在澳大利亚西德尼担任网络开发人员。 jsPlumb由他积极开发。做为许多优秀的开发人员,他彷佛更喜欢开发代码而不是编写教程,这就是为何我提供一个简单的入门教程。node
那么若是你应该使用它取决于你想用jsPlumb作什么。该框架适用于必须绘制图表的Web应用程序,例如相似于Visio的应用程序或工做流程设计器等。因为图表项目和链接的全部参数都是很是精细可控的,所以您能够绘制您能够想到的任何类型的图表的!jquery
注意:点击标题便可查看demo的在线效果
webpack
demo: https://wdd.js.org/jsplumb-ch...git
jsPlumb.ready方法和jquery的ready方法差很少的功能,jsPlumb.connect用于创建连线github
<div id="diagramContainer"> <div id="item_left" class="item"></div> <div id="item_right" class="item" style="margin-left:50px;"></div> </div> <script src="https://cdn.bootcss.com/jsPlumb/2.6.8/js/jsplumb.min.js"></script> <script> /* global jsPlumb */ jsPlumb.ready(function () { jsPlumb.connect({ source: 'item_left', target: 'item_right', endpoint: 'Dot' }) }) </script>
参数说明:
jsPlumb.connect(config) return connectionweb
参数 | 参数类型 | 是否必须 | 说明 |
---|---|---|---|
source | String,Object,Endpoint | 是 | 连线源的标识,能够是id, element, 或者Endpoint |
target | String,Object,Endpoint | 是 | 连线目标的标识,能够是id, element, 或者Endpoint |
endpoint | String | 可选 | 端点类型,形状 |
>>> connect方法详情segmentfault
demo: https://wdd.js.org/jsplumb-ch...api
使用draggable可让节点被拖动,draggable方法参考
<div id="diagramContainer"> <div id="item_left" class="item"></div> <div id="item_right" class="item" style="left:150px;"></div> </div> <script src="https://cdn.bootcss.com/jsPlumb/2.6.8/js/jsplumb.min.js"></script> <script> /* global jsPlumb */ jsPlumb.ready(function () { jsPlumb.connect({ source: 'item_left', target: 'item_right', endpoint: 'Rectangle' }) jsPlumb.draggable('item_left') jsPlumb.draggable('item_right') }) </script>
demo: https://wdd.js.org/jsplumb-ch...
能够经过connector去设置连接线的形状,如直线或者曲线之类的。anchor能够去设置锚点的位置。
<div id="diagramContainer"> <div id="item_left" class="item"></div> <div id="item_right" class="item" style="left:150px;"></div> </div> <script src="https://cdn.bootcss.com/jsPlumb/2.6.8/js/jsplumb.min.js"></script> <script> /* global jsPlumb */ jsPlumb.ready(function () { jsPlumb.connect({ source: 'item_left', target: 'item_right', endpoint: 'Rectangle', connector: ['Bezier'], anchor: ['Left', 'Right'] }) jsPlumb.draggable('item_left') jsPlumb.draggable('item_right') }) </script>
demo: https://wdd.js.org/jsplumb-ch...
不少连线都是相同设置的状况下,能够将配置抽离出来,做为一个单独的变量,做为connect的第二个参数传入。实际上connect的第二个参数会和第一个参数merge,做为一个总体。
<script> /* global jsPlumb */ jsPlumb.ready(function () { var common = { endpoint: 'Rectangle', connector: ['Bezier'], anchor: ['Left', 'Right'] } jsPlumb.connect({ source: 'item_left', target: 'item_right' }, common) jsPlumb.draggable('item_left') jsPlumb.draggable('item_right') }) </script>
demo: https://wdd.js.org/jsplumb-ch...
例如给连线设置不一样的颜色,设置不一样的粗细之类的。
jsPlumb.connect({ source: 'item_left', target: 'item_right', paintStyle: { stroke: 'lightgray', strokeWidth: 3 }, endpointStyle: { fill: 'lightgray', outlineStroke: 'darkgray', outlineWidth: 2 } }, common)
demo: https://wdd.js.org/jsplumb-ch...
箭头其实是经过设置overlays
去设置的,能够设置箭头的长宽以及箭头的位置,location 0.5表示箭头位于中间,location 1表示箭头设置在连线末端。 一根连线是能够添加多个箭头的。
jsPlumb.connect({ source: 'item_left', target: 'item_right', paintStyle: { stroke: 'lightgray', strokeWidth: 3 }, endpointStyle: { fill: 'lightgray', outlineStroke: 'darkgray', outlineWidth: 2 }, overlays: [ ['Arrow', { width: 12, length: 12, location: 0.5 }] ] }, common)
demo: https://wdd.js.org/jsplumb-ch...
addEndpoint方法能够用来增长端点,具体使用请看
jsPlumb.ready(function () { jsPlumb.addEndpoint('item_left', { anchors: ['Right'] }) })
demo: https://wdd.js.org/jsplumb-ch...
若是你将isSource
和isTarget
设置成true,那么久能够用户在拖动时,自动建立连接。
jsPlumb.ready(function () { jsPlumb.setContainer('diagramContainer') var common = { isSource: true, isTarget: true, connector: ['Straight'] } jsPlumb.addEndpoint('item_left', { anchors: ['Right'] }, common) jsPlumb.addEndpoint('item_right', { anchor: 'Left' }, common) jsPlumb.addEndpoint('item_right', { anchor: 'Right' }, common) })
通常来讲拖动建立的连接,能够再次拖动,让连接断开。若是不想触发这种行为,能够设置。
jsPlumb.importDefaults({ ConnectionsDetachable: false })
demo: https://wdd.js.org/jsplumb-ch...
经过设置各类 *Style
来改变连接或者端点的样式。
jsPlumb.ready(function () { jsPlumb.setContainer('diagramContainer') var common = { isSource: true, isTarget: true, connector: 'Straight', endpoint: 'Dot', paintStyle: { fill: 'white', outlineStroke: 'blue', strokeWidth: 3 }, hoverPaintStyle: { outlineStroke: 'lightblue' }, connectorStyle: { outlineStroke: 'green', strokeWidth: 1 }, connectorHoverStyle: { strokeWidth: 2 } } jsPlumb.addEndpoint('item_left', { anchors: ['Right'] }, common) jsPlumb.addEndpoint('item_right', { anchor: 'Left' }, common) jsPlumb.addEndpoint('item_right', { anchor: 'Right' }, common) })
demo: https://wdd.js.org/jsplumb-ch...
jsplumb实际上不支持改变节点大小,实际上只能经过jquery ui resizable 方法去改变。
<div id="diagramContainer"> <div id="item_left" class="item"></div> <div id="item_right" class="item" style="left:150px;"></div> </div> <script src="https://code.jquery.com/jquery-1.11.3.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script src="./lib/jquery.jsplumb.js"></script> <script> /* global jsPlumb, $ */ $('.item').resizable({ resize: function (event, ui) { jsPlumb.repaint(ui.helper) } }) jsPlumb.ready(function () { jsPlumb.connect({ source: 'item_left', target: 'item_right', endpoint: 'Rectangle' }) jsPlumb.draggable('item_left') jsPlumb.draggable('item_right') }) </script>
demo: https://wdd.js.org/jsplumb-ch...
默认状况下,节点能够被拖动到区域外边,若是想只能在区域内拖动,须要设置containment,这样节点只能在固定区域内移动。
jsPlumb.setContainer('area-id')
demo: https://wdd.js.org/jsplumb-ch...
网格对齐其实是设置了grid
属性,使移动只能按照固定的尺寸移动。而后用一张图做为背景铺开做为网格来实现的。
#diagramContainer { padding: 20px; width: 80%; height: 400px; border: 1px solid gray; background-image: url(http://p3alsaatj.bkt.clouddn.com/20180227163310_1bVYeW_grid.jpeg); background-repeat: repeat; } jsPlumb.draggable('item_left', { containment: 'parent', grid: [10, 10] })
// 单点击了链接线, jsPlumb.bind('click', function (conn, originalEvent) { if (confirm('肯定删除所点击的连接吗?')) { jsPlumb.detach(conn) } })
// nodeId为节点id, remove方法能够删除节点以及和节点相关的连线 jsPlumb.remove(nodeId)
注意remove方法有些状况下是没法删除干净连线的,详情
初始化数据后,给节点加上了endPoint, 若是想编码让endPoint连接上。须要在addEndpoint时,就给该断点加上一个uuid, 而后经过connect()方法,将两个断点连接上。建议使用node-uuid给每一个断点都加上惟一的uuid, 这样之后连接就方便多了。
jsPlumb.addEndpoint(id, { anchors: 'Top', uuid: uuid() // 这里须要惟一的一个Id, }, config) jsPlumb.connect({ uuids: [fromId, toId] })
默认状况下,maxConnections的值是1,也就是一个端点最多只能拉出一条连线。
你也能够设置成其余值,例如5,表示最多能够有5条连线。
若是你想不限制连线的数量,那么能够将该值设置为-1
参见 https://wdd.js.org/jsplumb-ch...
var common = { isSource: true, isTarget: true, connector: ['Straight'], maxConnections: -1 } jsPlumb.addEndpoint('item_left', { anchors: ['Right'] }, common)
项目地址:https://github.com/wangduandu... ,将views目录下的drag-drop-demo.html拖放到浏览器中,就能够愉快的玩耍了。
从该demo中除了能够学到基本的jsplumb的api, 也能够学到其余的关于拖放的知识点。其中目前只作了语音节点的拖放,其余的还时间作。该demo没有使用webpack打包,代码写的有点乱,你们凑合着看,有问题能够提issue, 或者评论。
项目地址:https://github.com/wangduandu... 该项目还在开发完善中,不过已经具有基本功能。
该项目是用webpack打包的项目,全部文件都在src目录下。
扫码订阅个人微信公众号:洞香春天
。天天一篇技术短文,让知识再也不高冷。