Repository navigation
监听节点dragend事件,将节点一拖动到节点二上,能直接获取节点二的ID吗? #7613
Unanswered
hugangyi-110
asked this question in
Q&A
Replies: 1 comment
|
1. 监听 import { NodeEvent } from '@antv/g6';
let draggingId;
graph.on(NodeEvent.DRAG_START, (e) => { draggingId = e.target.id; });
graph.on(NodeEvent.DROP, (e) => {
const targetId = e.target.id;
if (targetId !== draggingId) console.log(`${draggingId} 被放到了 ${targetId} 上`);
});
2. 在 graph.on('node:dragend', (e) => {
const draggedId = e.target.id;
const [x, y] = [e.canvas.x, e.canvas.y]; // 松手位置(画布坐标)
const hit = graph.getNodeData().find(({ id }) => {
if (id === draggedId) return false;
const { min, max } = graph.getElementRenderBounds(id);
return x >= min[0] && x <= max[0] && y >= min[1] && y <= max[1];
});
console.log(hit?.id); // 节点二的 ID,没有则为 undefined
});方式 2 判断的是"指针是否落在节点二的包围盒内"。如果想判断"两个节点是否有重叠",把指针坐标换成节点一的包围盒,再做一次相交判断即可。 |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
监听节点dragend事件,将节点一拖动到节点二上,能直接获取节点二的ID吗?
All reactions