有没有办法使用 Javascript 判断 Cystoscape 节点是否位于特定坐标?

Is there a way to tell if a Cystoscape node is at a certain coordinates using Javascript?

我有一个应用程序,用户可以在其中使用 Cytoscape JS 创建图形。要添加节点,用户单击 Cytoscape 容器,然后在单击位置添加一个节点。最近,我添加了自己的对齐网格方法,我想知道是否有任何方法可以检查某个节点是否已存在于特定坐标处,以防止用户创建重叠节点。我已经搜索并搜索了 Cytoscape JS 文档,但没有找到任何有用的东西。未来感谢任何帮助

如果您正在使用事件侦听器,则应根据您的目标实施它们。单击节点、边或 canvas 都可以在事件监听器中实现自己的逻辑 (cy.on()):

// unbind click events first
cy.off('click');

// click event for nodes
cy.on('click', 'node', function(evt){
   ...
});

// click event for edges
cy.on('click', 'edge', function(evt){
   ...
});

// click event for the canvas
cy.on('tap', function(event){
  if( event.target === cy ){
    ...
  }
});

有了这个,您可以将节点添加到 canvas,跳过在节点和边上添加它们,或者做一些其他的事情,比如向用户发出警报,提供一些关于为什么他们应该选择另一个位置的信息。

如果你想手动检查一个位置是否为空,你可以像这样天真地做:

cy.nodes().each(function(node) {
    // yourPosition should be look like this: { x: x-pos, y: y-pos }
    // where x-/y-pos are the grid positions x and y coordinates
    if(node.position() == yourPosition) { 
        // do something 
    }
});