vue连线 插件_使用jsPlumb插件实现动态连线功能

这周去看了两天的羽毛球亚锦赛,工作有提前晚上加班做一些,但是技术文章却拉下了。

这段时间一直在寻找可以实现前端元素动态连线的功能,找了好几个库,考虑过用d3或者原生svg和canvas来实现,最后和同项目的同事商量后决定使用jsPlumb插件库来做。

jsPlumb是一个强大的JavaScript连线库,它可以将html中的元素用箭头、曲线、直线等连接起来,适用于开发Web上的图表、建模工具等,其实jsPlumb可能主要是用来做流程图的,它在实现这方面的功能上非常强大,我在项目中只使用了它少部分功能,来实现项目中连线的效果。

连线效果

initJSPlumb = () => {

this.jsp = jsPlumb.getInstance({

//锚点位置;对任何没有声明描点的Endpoint设置锚点,用于source及target节点

Anchor: ["Right", "Left"],

Anchors: ["Right", "Left"], //连线的source和target Anchor

ConnectionsDetachable: false, //连线是否可用鼠标分离

ConnectionOverlays: [ //连线的叠加组件,如箭头、标签

["Arrow", { //箭头参数设置

location: 1,

visible: true,

width: 11,

length: 11,

id: "ARROW",

events: {

click: function () {

}

}

}],

["Label", { //标签参数设置

location: 0.1,

id: "label",

cssClass: "aLabel", //hover时label的样式名

events: {

tap: function () {

}

},

visible: true

}]

],

Connector: "Bezier", //连线的类型,流程图(Flowchart)、贝塞尔曲线等

//父级元素id;假如页面元素所在上层不同,最外层父级一定要设置

Container: "module",

//如果请求不存在的Anchor、Endpoint或Connector,是否抛异常

DoNotThrowErrors: false,

//通过jsPlumb.draggable拖拽元素时的默认参数设置

DragOptions: {cursor: 'pointer', zIndex: 2000},

DropOptions: {}, //target Endpoint放置时的默认参数设置

Endpoint: "Dot", //端点(锚点)的样式声明

//用jsPlumb.connect创建连接时,source端点和target端点的样式设置

Endpoints: [null, null],

EndpointOverlays: [], //端点的叠加物

//端点的默认样

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现点击添加连线,首先需要安装 jsplumbvue2 的相关依赖包。 然后,可以通过以下步骤实现点击添加连线功能: 1. 在 Vue2 中创建一个画布(canvas)元素,用于绘制连线。 2. 在画布上绑定鼠标点击事件,当用户点击画布时,获取当前鼠标的位置,作为起始点。 3. 接着,绑定鼠标移动事件,当用户拖动鼠标时,获取当前鼠标的位置,作为结束点。同时,实时更新起始点和结束点之间的连线。 4. 最后,绑定鼠标释放事件,当用户释放鼠标时,将起始点和结束点之间的连线保存到数据中,以便后续使用。 下面是一个简单的实现示例代码: ```html <template> <div> <div ref="canvas" @mousedown="handleMouseDown" @mousemove="handleMouseMove" @mouseup="handleMouseUp"></div> </div> </template> <script> import jsPlumb from 'jsplumb' export default { mounted() { // 初始化 jsPlumb 实例 this.jsPlumbInstance = jsPlumb.getInstance() // 设置连线样式 this.jsPlumbInstance.registerConnectionType('basic', { anchor: 'Continuous', connector: 'StateMachine' }) }, methods: { handleMouseDown(e) { // 获取当前鼠标的位置,作为起始点 this.startPoint = { x: e.clientX, y: e.clientY } // 创建一个临时的连线元素 this.tempConnection = this.jsPlumbInstance.connect({ source: 'canvas', target: 'canvas', type: 'basic', paintStyle: { strokeWidth: 2, stroke: '#000' } }) }, handleMouseMove(e) { if (this.tempConnection) { // 获取当前鼠标的位置,作为结束点 const endPoint = { x: e.clientX, y: e.clientY } // 更新连线的起始点和结束点 this.jsPlumbInstance.setSuspendDrawing(true) this.jsPlumbInstance.deleteConnection(this.tempConnection) this.tempConnection = this.jsPlumbInstance.connect({ source: this.startPoint, target: endPoint, type: 'basic', paintStyle: { strokeWidth: 2, stroke: '#000' } }) this.jsPlumbInstance.setSuspendDrawing(false, true) } }, handleMouseUp(e) { if (this.tempConnection) { // 将起始点和结束点之间的连线保存到数据中 const endPoint = { x: e.clientX, y: e.clientY } this.connections.push({ source: this.startPoint, target: endPoint }) // 销毁临时的连线元素 this.jsPlumbInstance.deleteConnection(this.tempConnection) this.tempConnection = null } } } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值