vue項(xiàng)目中使用jsplumb總結(jié)

vue項(xiàng)目中使用jsplum總結(jié)
先介紹一下背景,最近項(xiàng)目中有個(gè)需求(各節(jié)點(diǎn)與節(jié)點(diǎn)之間的連線(xiàn)),網(wǎng)上搜刮了一波關(guān)于連線(xiàn)的開(kāi)源庫(kù),發(fā)現(xiàn)jsplumb的文章應(yīng)該是最多的,功能也比較豐富,最終就選擇了jsplumb來(lái)實(shí)現(xiàn)項(xiàng)目需求。
下面開(kāi)始我的vue+jsplumb踩坑之旅。
在線(xiàn)DOME預(yù)覽

image.png

一、安裝 jsplumb

1、vue 項(xiàng)目中安裝 jsPlumb 模塊

npm install jsplumb  --save

2、在main.js 中引入 jsPlumb(全局引入,也可以局部引入)

import jsPlumb from 'jsplumb'
Vue.prototype.$jsPlumb = jsPlumb.jsPlumb
二、使用jsplumb

nodeConnection.vue組件

// 初始化JSPlumb樣式
    initJSPlumb() {
      this.jsPlumb = this.$jsPlumb.getInstance({
        Container: "efContainer", //容器id
        //  Endpoint:['Image', {
        //    src: 'static/images/def_endpoint.png',
        //  }] , // 端點(diǎn)的形狀
        Anchor: [0.5, 1, 0, 1, 0, 50],
        Anchors: ["Right", "Left"], //連線(xiàn)的source和target 
        EndpointStyle: { radius: 6, fill: "#818FB4" }, //端點(diǎn)默認(rèn)樣式
        PaintStyle: { stroke: "#818FB4", strokeWidth: 1, fill: 'red' }, // 連線(xiàn)樣式
        HoverPaintStyle: { stroke: "#498FFF" }, // 默認(rèn)懸停樣式
        // EndpointHoverStyle: {src: 'static/images/endpoint.png', },
        EndpointHoverStyle: { fill: "#498FFF" }, // 端點(diǎn)懸停樣式
        ConnectionOverlays: [
          [
            "Arrow",
            {
              // 箭頭
              location: 1,
              visible: false,
              paintStyle: {
                stroke: "#ccc",
                fill: "#ccc"
              }
            }
          ]
        ],
        Connector: ["Straight", { gap: 5 }], //要使用的默認(rèn)連接器的類(lèi)型:折線(xiàn),流程等
        DrapOptions: { cursor: "crosshair", zIndex: 2000 },
        Scope: "jsPlumb_DefaultScope"
      });
      
    // 連接斷開(kāi)時(shí)觸發(fā)
      this.jsPlumb.bind('connectionDetached', evt => {
      })

      // 連接建立時(shí)觸發(fā)
      this.jsPlumb.bind("connection", evt => {
      });

      // 改變線(xiàn)的連接節(jié)點(diǎn)
      this.jsPlumb.bind("connectionMoved", evt => {
      });

      // 當(dāng)鏈接建立前
      this.jsPlumb.bind("beforeDrop", evt => { 
      });
      // 連接斷開(kāi)前
      this.jsPlumb.bind("beforeDetach", evt => {
      });
    }
三、默認(rèn)的參數(shù)配置(此處只列了部分參數(shù)配置,更多配置請(qǐng)移步官方文檔)
{
  Anchor : "BottomCenter",//端點(diǎn)的定位點(diǎn)的位置聲明(錨點(diǎn)):left,top,bottom等
  Anchors : [ null, null ],//多個(gè)錨點(diǎn)的位置聲明
  ConnectionsDetachable   : true,//連接是否可以使用鼠標(biāo)默認(rèn)分離
  ConnectionOverlays  : [],//附加到每個(gè)連接的默認(rèn)重疊
  Connector : "Bezier",//要使用的默認(rèn)連接器的類(lèi)型:折線(xiàn),流程等
  Container : null,//設(shè)置父級(jí)的元素,一個(gè)容器
  DoNotThrowErrors  : false,//如果請(qǐng)求不存在的Anchor,Endpoint或Connector,是否會(huì)拋出
  DragOptions : { },//用于配置拖拽元素的參數(shù)
  DropOptions : { },//用于配置元素的drop行為的參數(shù)
  Endpoint : "Dot",//端點(diǎn)(錨點(diǎn))的樣式聲明(Dot)
  Endpoints : [ null, null ],//多個(gè)端點(diǎn)的樣式聲明(Dot)
  EndpointOverlays : [ ],//端點(diǎn)的重疊
  EndpointStyle : { fill : "#456" },//端點(diǎn)的css樣式聲明
  EndpointStyles : [ null, null ],//同上
  EndpointHoverStyle : null,//鼠標(biāo)經(jīng)過(guò)樣式
  EndpointHoverStyles : [ null, null ],//同上
  HoverPaintStyle : null,//鼠標(biāo)經(jīng)過(guò)線(xiàn)的樣式
  LabelStyle : { color : "black" },//標(biāo)簽的默認(rèn)樣式。
  LogEnabled : false,//是否打開(kāi)jsPlumb的內(nèi)部日志記錄
  Overlays : [ ],//重疊
  MaxConnections : 1,//最大連接數(shù)
  PaintStyle : { lineWidth : 8, stroke : "#456" },//連線(xiàn)樣式
  ReattachConnections : false,//是否重新連接使用鼠標(biāo)分離的線(xiàn)
  RenderMode : "svg",//默認(rèn)渲染模式
  Scope : "jsPlumb_DefaultScope"http://范圍,標(biāo)識(shí)
}
四、jsPlumb的相關(guān)方法
  • 添加連線(xiàn)
// 此處參數(shù)sourceId,targetId一般是從后臺(tái)獲取后再添加連線(xiàn)
const params={source: item.sourceId, target: item.targetId};
// 為自定義的jsPlumb設(shè)置,也可以不傳入
const  connectOptions = {
   isSource: true,
   isTarget: true,
   // 動(dòng)態(tài)錨點(diǎn)、提供了4個(gè)方向 Continuous、AutoDefault
   anchor: 'Continuous',
   // 設(shè)置連線(xiàn)上面的label樣式
   labelStyle: {
     cssClass: 'flowLabel'
   },
  connectorStyle: {
     stroke: '#30BF78'
   },
}
this.jsPlumb.connect(params, connectOptions);
  • 刪除連線(xiàn)
//  這個(gè)方法刪除指定的節(jié)點(diǎn)和連線(xiàn),傳入的node為節(jié)點(diǎn)元素
this.jsPlumb.removeAllEndpoints(node)
// 這個(gè)方法刪除所有連線(xiàn),不需要傳入?yún)?shù) 
this.jsPlumb.deleteEveryConnection();
// 這個(gè)方法刪除所有節(jié)點(diǎn),不需要傳入?yún)?shù)
this.jsPlumb.deleteEveryEndpoint();
// 刪除制定的連線(xiàn),傳入節(jié)點(diǎn)ID
this.jsPlumb.deleteConnectionsForElement('t001',{});
  • 重繪連線(xiàn)
// 重繪連線(xiàn)
this.jsPlumb.repaintEverything()
// 這里第二個(gè)參數(shù)的true,會(huì)使整個(gè)jsPlumb立即重繪。
this.jsPlumb.setSuspendDrawing(false,true);

最后附上一個(gè)在線(xiàn)DOME預(yù)覽
https://qinghub.net/jsplumb

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容