vue3 antvX6的使用源码

news/2024/10/14 17:04:31
npm install --save @antv/x6
<template><div class="dashboard-container"><p>选择节点</p><button @click="save">保存</button><div class="antvBox"><div class="menu-list"><divv-for="item in moduleList":key="item.id"draggable="true"@dragend="handleDragEnd($event, item)"><img :src="item.image" alt="" /><p>{{ item.name }}</p></div></div><div class="canvas-card"><div id="container" /></div></div></div>
</template><script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { Graph } from '@antv/x6'import t1 from '../../assets/images/1.png'
import t2 from '../../assets/images/2.png'
import t3 from '../../assets/images/3.png'
import t4 from '../../assets/images/4.png'
import t5 from '../../assets/images/5.png'const moduleList = ref([{id: 1,name: '节点1',image: t1},{id: 8,name: '节点2',image: t2},{id: 2,name: '节点3',image: t3},{id: 3,name: '节点4',image: t4},{id: 4,name: '节点5',image: t5}
])const curSelectNode = ref(null)/*** 拖拽左侧* @param e* @param item*/
const handleDragEnd = (e, item) => {addHandleNode(e.pageX - 300, e.pageY - 200, new Date().getTime(), item.image, item.name)
}const graph = ref<Graph | null>(null)const initGraph = () => {const container = document.getElementById('container')if (container === null) {throw new Error('Container element not found')}graph.value = new Graph({container: container, // 画布容器width: container.offsetWidth, // 画布宽height: container.offsetHeight, // 画布高background: false, // 背景(透明)snapline: true, // 对齐线// 配置连线规则connecting: {snap: true, // 自动吸附allowBlank: false, // 是否允许连接到画布空白位置的点allowMulti: true, // 是否允许在相同的起始节点和终止之间创建多条边allowLoop: true, // 是否允许创建循环连线,即边的起始节点和终止节点为同一节点highlight: true, // 拖动边时,是否高亮显示所有可用的节点highlighting: {magnetAdsorbed: {name: 'stroke',args: {attrs: {fill: '#5F95FF',stroke: '#5F95FF'}}}},router: {// 对路径添加额外的点name: 'orth'},connector: {// 边渲染到画布后的样式name: 'rounded',args: {radius: 8}}},panning: {enabled: false},mousewheel: {enabled: true, // 支持滚动放大缩小zoomAtMousePosition: true,modifiers: 'ctrl',minScale: 0.5,maxScale: 3},grid: {type: 'dot',size: 20, // 网格大小 10pxvisible: true, // 渲染网格背景args: {color: '#a0a0a0', // 网格线/点颜色thickness: 2 // 网格线宽度/网格点大小}}})nodeAddEvent()
}
/*** 添加画布到节点* x坐标、y坐标、id节点唯一标识、image图片、name节点名称*/
//添加节点到画布
const addHandleNode = (x, y, id, image, name) => {graph.value.addNode({id: id,shape: 'image', // 指定使用何种图形,默认值为 'rect'x: x,y: y,width: 60,height: 60,imageUrl: image,attrs: {body: {stroke: '#ffa940',fill: '#ffd591'},label: {textWrap: {width: 90,text: name},fill: 'black',fontSize: 12,refX: 0.5,refY: '100%',refY2: 4,textAnchor: 'middle',textVerticalAnchor: 'top'}},ports: {groups: {group1: {position: [30, 30]}},items: [{group: 'group1',id: 'port1',attrs: {circle: {r: 6,magnet: true,stroke: '#ffffff',strokeWidth: 2,fill: '#5F95FF'}}}]},zIndex: 10})
}
/*** 鼠标移入节点再显示链接桩*/
const nodeAddEvent = () => {const container = document.getElementById('container')if (container === null) {throw new Error('Container element not found')}const changePortsVisible = visible => {const ports = container.querySelectorAll('.x6-port-body')for (let i = 0, len = ports.length; i < len; i = i + 1) {ports[i].style.visibility = visible ? 'visible' : 'hidden'}}graph.value.on('node:mouseenter', () => {changePortsVisible(true)})graph.value.on('node:mouseleave', () => {changePortsVisible(false)})// 节点绑定点击事件 删除节点// eslint-disable-next-line @typescript-eslint/no-unused-varsgraph.value.on('node:click', ({ e, x, y, node, view }) => {console.log('点击!!!', node)// 判断是否有选中过节点if (curSelectNode.value) {// 移除选中状态curSelectNode.value.removeTools()// 判断两次选中节点是否相同if (curSelectNode.value !== node) {node.addTools([{name: 'boundary',args: {attrs: {fill: '#16B8AA',stroke: '#2F80EB',strokeWidth: 1,fillOpacity: 0.1}}},{name: 'button-remove',args: {x: '100%',y: 0,offset: {x: 0,y: 0}}}])curSelectNode.value = node} else {curSelectNode.value = null}} else {curSelectNode.value = nodenode.addTools([{name: 'boundary',args: {attrs: {fill: '#16B8AA',stroke: '#2F80EB',strokeWidth: 1,fillOpacity: 0.1}}},{name: 'button-remove',args: {x: '100%',y: 0,offset: {x: 0,y: 0}}}])}})// 删除链接节点的线// 连线绑定悬浮事件graph.value.on('cell:mouseenter', ({ cell }) => {if (cell.shape == 'edge') {cell.addTools([{name: 'button-remove',args: {x: '100%',y: 0,offset: {x: 0,y: 0}}}])cell.setAttrs({line: {stroke: '#409EFF'}})cell.zIndex = 99 // 保证当前悬停的线在最上层,不会被遮挡}})graph.value.on('cell:mouseleave', ({ cell }) => {if (cell.shape === 'edge') {cell.removeTools()cell.setAttrs({line: {stroke: 'black'}})cell.zIndex = 1 // 保证未悬停的线在下层,不会遮挡悬停的线}})
}//保存画布,并提交
const save = () => {console.log(graph.value.toJSON(), 'graph')console.log(graph.value.getNodes(), 'node')
}
onMounted(() => {initGraph()
})
</script>
<style lang="scss" scoped>
/* @use ''; 引入css类 */
.dashboard-container {.antvBox {display: flex;width: 100%;height: 100%;color: black;padding-top: 20px;.menu-list {height: 100%;width: 300px;padding: 0 10px;box-sizing: border-box;display: flex;justify-content: space-between;align-content: flex-start;flex-wrap: wrap;> div {margin-bottom: 10px;border-radius: 5px;padding: 0 10px;box-sizing: border-box;cursor: pointer;color: black;width: 105px;display: flex;flex-wrap: wrap;justify-content: center;img {height: 50px;width: 50px;}P {width: 90px;text-align: center;}}}.canvas-card {width: 1700px;height: 750px;box-sizing: border-box;> div {width: 1400px;height: 750px;border: 2px dashed #2149ce;}}}
}
</style>

借鉴https://blog.csdn.net/wzy_PROTEIN/article/details/136305034

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/71513.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

程序员攻占小猿口算,炸哭小学生!

小学生口算 PK,已经演变为各大高校和程序员之间的算法学术交流竞赛!小学生万万没想到,做个加减乘除的口算练习题,都能被大学生、博士生、甚至是程序员大佬们暴打!最近这款拥有 PK 功能的《小猿口算》App 火了,谁能想到,本来一个很简单的小学生答题 PK,竟然演变为了第四…

注塑机接插件航空插头工厂数据采集产量监控系统

测试机柜温湿度01.温度7dp8dp 测试机柜温湿度01.湿度压接计数器02.产量计数 测试机柜温湿度02.温度测试机柜温湿度02.湿度3eftkf 压接计数器03.产量计数 压接计数器01.产量计数压接计数器04.产量计数200121 压接计数器05.产量计数5gd1qg 海天温湿度.温度6h0c2l 海天温湿度.湿度…

C#中使用Socket请求Web服务器过程

最开始我们需要明白一件事情,因为这是这篇文章的前提: HTTP协议只是一个应用层协议,它底层是通过TCP进行传输数据的。因此,浏览器访问Web服务器的过程必须先有“连接建立”的发生。而有人或许会问:众所周知,HTTP协议有两大特性,一个是“无连接”性,一个是“无状态”性。…

新蜀门+单机版安装教程+虚拟机一键端+GM

今天给大家带来一款单机游戏的架设:新蜀门。本版本支持win64位系统 另外:本人承接各种游戏架设(单机+联网) 本人为了学习和研究软件内含的设计思想和原理,带了架设教程仅供娱乐。 教程是本人亲自搭建成功的,绝对是完整可运行的,踩过的坑都给你们填上了。如果你是小白也没…

一文为你解读MySQL8.0 Instant DDL源码实现

MySQL 8.0.12版本引入了INSTANT(即时)算法,对部分ADD COLUMN操作,不再修改用户原有数据,只需对表元信息进行修改。一、背景介绍 数据库中每一行数据都被持久化存储在磁盘中。当我们对表进行ADD/DROP COLUMN操作时,磁盘中的数据也会相应地被修改,所需时间与对应表的大小成…

csp-s模拟11

E题面最暴力的做法,枚举连续段长度\(i\),然后暴力搜索,复杂度\(O(n^3)\)点击查看代码 #include <bits/stdc++.h> #define speed() ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); #define ll long long #define pb push_back #define ull unsigned long long #de…

高等数学 5.1 定积分的概念与性质

目录一、定积分的定义1.定义2.定积分的几何意义二、定积分的近似计算1.矩形法2.梯形法3.抛物线法三、定积分的性质 一、定积分的定义 1.定义定义 设函数 \(f(x)\) 在 \([a, b]\) 上有界,在 \([a, b]\) 中任意插入若干个分点 \[a = x_0 < x_1 < x_2 < \cdots < x_{…