vue、jsPlum连线题demo

 

<!-- 连线题 -->
  <div class="question-line">
    <div
      class="item"
      v-for="(list, index) in data.line_group_a"
      :key="index"
    >
      <div class="item-left">
        <div class="text" v-if="list.text" :id="`node-a-${index}`">
          {{ list.text }}
        </div>
        <div class="img-box" v-else :id="`node-a-${index}`">
          <a-image :src="list.image" :alt="list.rename" />
        </div>
      </div>
      <div class="item-right">
        <div
          class="text"
          :id="`node-b-${index}`"
          v-if="data.line_group_b[index].text"
        >
          {{ data.line_group_b[index].text }}
        </div>
        <div class="img-box" v-else :id="`node-b-${index}`">
          <a-image
            :src="data.line_group_b[index].image"
            :alt="data.line_group_b[index].rename"
          />
        </div>
      </div>
    </div>
    <div v-if="answerResult" class="connector-line" ref="connectorLine"></div>
  </div>
<script setup>
import { ref, onMounted, onUnmounted, nextTick } from 'vue'
import { cloneDeep } from 'lodash-es'
import { jsPlumb } from 'jsplumb'
const connectorLine = ref(null)
let jsPlumbInstance = ref(null)
const data =ref({
  line_group_a: [
    {
      text: '',
      image:
        'https://images-towin.tao2bao.com/resource/image/5/DFQStQd1qnaf8233b4a4cdaddf6f3932168be1af9c.jpeg',
      rename: 'DFQStQd1qnaf8233b4a4cdaddf6f3932168be1af9c.jpeg',
      line: '1',
    },
    {
      text: '',
      image:
        'https://images-towin.tao2bao.com/resource/image/5/DICfx3QD2f1bc08e6840aeeb64dec90110921e3769.jpg',
      rename: 'DICfx3QD2f1bc08e6840aeeb64dec90110921e3769.jpg',
      line: '3',
    },
    {
      text: '',
      image:
        'https://images-towin.tao2bao.com/resource/image/5/15rjpgMf042479056ea49dd44168cd96ac25a85dc7.jpeg',
      rename: '15rjpgMf042479056ea49dd44168cd96ac25a85dc7.jpeg',
      line: '2',
    },
    {
      text: '',
      image:
        'https://images-towin.tao2bao.com/resource/image/5/hu6EQSWvMZapple.jpg',
      rename: 'hu6EQSWvMZapple.jpg',
      line: '0',
    },
  ],
  line_group_b: [
    {
      text: 'orange',
      image: '',
      rename: '',
      line: '1',
    },
    {
      text: 'apple',
      image: '',
      rename: '',
      line: '0',
    },
    {
      text: 'durian',
      image: '',
      rename: '',
      line: '3',
    },
    {
      text: 'banana',
      image: '',
      rename: '',
      line: '2',
    },
  ],})

onMounted(() => {
  nextTick(() => {
    setTimeout(() => {
      if (props.answerResult) {
        initJsPlumb()
        createConnections()
      }
    }, 1000)
  })
})
const initJsPlumb = () => {
  jsPlumbInstance.value = jsPlumb.getInstance({
    Connector: 'Straight',
    Endpoint: 'Dot',
    EndpointStyle: { radius: 2, fill: '#1bd17b' },
    PaintStyle: { stroke: '#1bd17b', strokeWidth: 1 },
    Anchors: ['Right', 'Left'],
  })
}

const createConnections = () => {
  // 需要确保connectorLine的dom元素已经渲染完毕
  // if (connectorLineElement) {
  //   const rectA = jsPlumbInstance.value
  //     .getContainer(lastNodeA)
  //     .getBoundingClientRect()
  //   const rectB = jsPlumbInstance.value
  //     .getContainer(firstNodeB)
  //     .getBoundingClientRect()
  //
  //   // 设置connectorLine的样式以连接两个节点
  //   connectorLineElement.style.position = 'absolute'
  //   connectorLineElement.style.left = `${rectA.right}px`
  //   connectorLineElement.style.top = `${
  //     Math.min(rectA.bottom, rectB.top) - 5
  //   }px` // 减5是为了让线稍微偏上一点,看起来更美观
  //   connectorLineElement.style.width = `${Math.abs(rectB.left - rectA.right)}px`
  //   connectorLineElement.style.height = '2px' // 线的宽度
  //   connectorLineElement.style.backgroundColor = '#ff0000' // 线的颜色
  // }

  const groupA = data.line_group_a
  const groupB = data.line_group_b

  if (connectorLine?.value) {
    groupA.forEach((nodeA, index) => {
      groupB.forEach((nodeB, count) => {
        if (nodeA.line === nodeB.line) {
          jsPlumbInstance.value.connect({
            source: `node-a-${index}`,
            target: `node-b-${count}`,
            endpoint: 'Dot',
            paintStyle: { stroke: '#1bd17b', strokeWidth: 3 },
            anchors: ['Right', 'Left'],
          })
        }
      })
    })
  }
}
onUnmounted(() => {
  if (jsPlumbInstance.value) {
    jsPlumbInstance.value.deleteEveryEndpoint()
    jsPlumbInstance.value.unbind()
    jsPlumbInstance.value = null
  }
})
</script>

效果图:

借鉴文章地址:三、jsPlumb实现流程图配置--Endpoint详细参数 - Bencakes - 博客园

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值