使用antv X6实现两点之间连线上圆点匀速移动的完整代码

  • 首先vue我使用的是vue3的写法,然后X6使用的1.31.0版本
  • 确保vue版本正确,x6安装好,直接复制过去就能看到效果
  • <template>
        <div class="app">
            <div id="container" class="area-center-container" />
        </div>
    </template>
    <script>
    import { defineComponent, onMounted } from 'vue';
    import { Graph, Timing } from "@antv/x6";
    
    export default defineComponent({
        setup() {
            let graph = null;
            const initGraph = () => {
                graph = new Graph({
                    container: document.getElementById('container'),
                    background: {
                        color: "#44546c"
                    },
                    connecting: {
                        allowBlank: true,
                    },
                    mousewheel: true,
                    panning: true,
                    grid: {
                        size: 10, 
                        visible: true 
                    },
                    snapline: {
                        enabled: true,
                        sharp: true
                    },
                    scroller: {
                        enabled: false,
                        pageVisible: false,
                        pageBreak: false,
                        pannable: false
                    },
                })
               
                const ball = graph.addNode({
                    shape: 'circle',
                    id: 'node999',
                    x: 0,
                    y: 40,
                    width: 60,
                    height: 60,
                    attrs: {
                        label: {
                            text: 'ball',
                            fontSize: 20
                        },
                        body: {
                            fill: '#FFFFFF',
                        }
                    },
                })
                const ball1 = graph.addNode({
                    shape: 'circle',
                    id: 'node1000',
                    x: 1000,
                    y: 40,
                    width: 60,
                    height: 60,
                    attrs: {
                        label: {
                            text: 'ball',
                            fontSize: 20
                        },
                        body: {
                            fill: '#FFFFFF',
                        }
                    },
                })
                const edge = graph.addEdge({
                    shape: 'edge', 
                    id: '1001',
                    source: ball,
                    target: ball1,
                })
    
                let edge1 = graph.getCellById('1001')
                let markup = edge1.getMarkup()
    
                markup.push({
                    tagName: 'circle',
                    selector: 'circle'
                })
                edge1.setMarkup(markup) 
                let attrOption = {
                    circle: {
                        r: 5,
                        fill: '#00FFFF',
                        atConnectionRatio: 0,
                        refCy: -10 
                    }
                }
                let options1 = {
                    delay: 0, 
                    duration: 3000, 
                    timing: Timing.linear, 
                    complete: () => {
                        edge1.setAttrs(attrOption) 
                        edge1.transition('attrs/circle/atConnectionRatio', 1, options1)
                    }, 
                }
                edge1.setAttrs(attrOption)
                edge1.transition('attrs/circle/atConnectionRatio', 1, options1)
            };
            onMounted(() => {
                initGraph()
            });
            return {
                initGraph
            }
        },
    });
    </script>
    <style sccoped>
    body {
        padding: 0;
        margin: 0;
    }
    
    .app {
        height: 100vh;
        width: 100vw;
        display: flex;
    }
    
    #container {
        flex: 1;
    }
    </style>
    

Vue2 中结合 AntV X6 使用自定义文本框节点,你可以创建一个组件来扩展 X6 的默认节点类型。首先,你需要安装必要的依赖并引入 AntV 和 X6。以下是一个简单的步骤: 1. 安装依赖: ```bash npm install antv/x6@latest @antv/x6-plugin-node-type-textbox vue@latest ``` 2. 创建一个名为 `CustomTextBox.vue` 的组件: ```html <template> <x6-graph :width="graphWidth" :height="graphHeight"> <!-- 其他X6配置... --> <x6-plugin-node-type type="custom-textbox" /> </x6-graph> </template> <script> import { Graph } from '@antv/x6'; import { NodeTypes } from '@antv/x6-plugin-node-type'; export default { name: 'CustomTextBox', props: { graphWidth: { type: Number, required: true }, graphHeight: { type: Number, required: true } }, mounted() { this.graph = new Graph({ container: 'container', // 渲染容器id modes: ['drag-node', 'resize-node'], autoZoom: false, width: this.graphWidth, height: this.graphHeight, }); const customNodeType = NodeTypes.register('custom-textbox', { draw: (cfg) => { // 在这里绘制自定义的文本框,可以使用AntV提供的画布API const { cx, cy, width, height } = cfg.position; const textConfig = { content: cfg.data.content || '', x: cx - width / 2, y: cy - height / 2, style: { fill: '#fff', lineWidth: 2, stroke: '#000', fontSize: '14px', textAlign: 'center', textBaseline: 'middle', }, }; return this.graph.stage.add('text', textConfig); }, update: (cfg) => { // 更新节点位置时更新文本框的位置 const node = this.graph.getNodeById(cfg.id); if (node) { const textNode = this.graph.get('text', { id: `${cfg.id}-content` }); textNode.attr({ x: node.cx - node.width / 2, y: node.cy - node.height / 2 }); } }, remove: () => { // 节点删除时移除文本框 const textNode = this.graph.get('text', { id: `${cfg.id}-content` }); if (textNode) { textNode.remove(); } }, }); // 添加自定义节点类型到图表 this.graph.use(customNodeType); }, beforeDestroy() { this.graph && this.graph.destroy(); }, }; </script> ``` 然后在你的父组件中使用这个自定义组件,并传入需要的宽度和高度属性: ```html <template> <div> <CustomTextBox :graphWidth="800" :graphHeight="600" /> </div> </template> <script> import CustomTextBox from './CustomTextBox.vue'; export default { components: { CustomTextBox, }, }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值