Antv/g6 自定义节点后拖动不流畅问题解决

1 篇文章 1 订阅
1 篇文章 0 订阅

初次使用G6,用的是官网推荐的Workflow Designer。

使用后发现里面的自定义节点有时候会出现拖动失灵的情况,需要点击节点,按下许久再拖动,才不会出现拖不动的情况,这个问题困扰了很久,严重影响操作交互体验。

后面发现凡是点击到自定义添加的图形图标和文字上都会出现拖不动的问题。

自定义节点如下:


drawLabel:(cfg,group)=>{
	const description = group.addShape('text',{
		  attrs: {
		    text: cfg.description,
		    x: 0,
		    y: 10,
		    fontSize: 14,
		    textAlign: 'left',
		    textBaseline: 'middle',
		    fill: '#0000D9',
  }})
}

drawLogoIcon:(cfg,group)=>{
  const logoIcon = group.addShape('image', {
	  attrs: {
	    x: 0,
	    y: 0,
	    img:
	      'https://g.alicdn.com/cm-design/arms-trace/1.0.155/styles/armsTrace/images/TAIR.png',
	  },
});
}

添加完一个文字节点和一个图像节点后,能正常显示,但鼠标拖动这两个节点时经常会出现拖动失灵。
在谷歌和Workflow Designer github issues 查找无果。
后面去翻阅官方文档发现有个API:enableCapture(enable)


enableCapture(enable)
//是否拾取及触发该元素的交互事件。

// 不允许元素响应事件
item.enableCapture(false);

// 允许元素响应事件
item.enableCapture(true);

试着用上面自定的节点调用该函数。


description.enableCapture(false);
logoIcon.enableCapture(false);
// 报错,提示group.logoIcon 没有enableCapture属性;

// 接着在 setState(name,value,item){} 里发现item是该节点的实例,并且有enableCapture方法。
// 试着在setState函数里面调用 item.enableCapture(false); 
// 没有报错,但也没有效果.....
// 心塞!可能是我对这个方法的使用方式不对。。 

后面把官网上示例上的modelRect节点,单独拉个项目跑起来,发现拖动啥的都很流畅,完全没有出现卡顿和拖不动的情况。

找到modelRect原码,对比了modelRect源码上添加节点的操作,发现多了个自定义完了之后,多了一个xxx.set(‘capture’,false) 步骤。
搞过来一试,果然可以了,节点拖动时流畅度瞬间就上来了。

最后的解决方法:
drawLogoIcon:(cfg,group)=>{
  const logoIcon = group.addShape('image', {
	  attrs: {
	    x: 0,
	    y: 0,
	    img:
	      'https://g.alicdn.com/cm-design/arms-trace/1.0.155/styles/armsTrace/images/TAIR.png',
	  },
});
	logoIcon.set('capture',false);
}


我使用的是 3.2版本的 g6,解决方法是 .set(‘capture’,false);

3.7版本的在创建节点时添加属性 draggable: true,

drawLogoIcon:(cfg,group)=>{
  const logoIcon = group.addShape('image', {
	  attrs: {
	    x: 0,
	    y: 0,
	    img:
	      'https://g.alicdn.com/cm-design/arms-trace/1.0.155/styles/armsTrace/images/TAIR.png',
	  },
	  draggable: true,
});
}
  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Vue 3是Vue.js的最新版本,它具有更好的性能、更好的TypeScript支持、更好的开发者体验等特点。Vite是一个轻量级的Web应用程序构建工具,它可以快速地构建Vue项目,同时支持TypeScript。AntV X6是一个强大的图形可视化库,它可以帮助开发人员快速地创建各种类型的图表和流程图。 下面是一个使用Vue 3、Vite、TypeScript和AntV X6的自定义节点元素案例代码: ``` <template> <div class="app"> <x6-graph :graph="graph" :width="800" :height="600"> <template #default="{ node }"> <x6-rect v-if="node.data.type === 'rect'" :node="node" :width="node.getSize().width" :height="node.getSize().height" :fill="node.getStyle().fillColor" :stroke="node.getStyle().strokeColor" /> <x6-circle v-else-if="node.data.type === 'circle'" :node="node" :r="node.getSize().width / 2" :fill="node.getStyle().fillColor" :stroke="node.getStyle().strokeColor" /> </template> </x6-graph> </div> </template> <script lang="ts"> import { defineComponent, ref } from 'vue' import { Graph, Node, Rect, Circle } from '@antv/x6' import { useUndoRedo } from '@antv/x6-vue' export default defineComponent({ setup() { const graph = ref<Graph>() useUndoRedo({ graph }) const nodes = [ { type: 'rect', x: 100, y: 100, width: 100, height: 50, style: { fillColor: '#ffffff', strokeColor: '#000000', }, }, { type: 'circle', x: 300, y: 100, width: 100, height: 100, style: { fillColor: '#ffffff', strokeColor: '#000000', }, }, ] graph.value = new Graph({ container: document.querySelector('.app')!, }) nodes.forEach((node) => { const shape = node.type === 'rect' ? Rect : Circle const newNode = new Node({ x: node.x, y: node.y, width: node.width, height: node.height, shape, data: node, }) newNode.setStyle(node.style) graph.value.addNode(newNode) }) return { graph } }, }) </script> ``` 此代码演示了如何在Vue 3项目中使用AntV X6创建自定义节点元素。在这个示例中,我们创建了两个不同类型的节点:一个矩形和一个圆形。我们通过给每个节点添加一个"type"属性来区分它们的类型。然后,我们在Vue模板中使用了`x6-rect`和`x6-circle`组件来渲染节点元素,这些组件分别对应于`Rect`和`Circle`节点形状。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值