If you are using event listeners, you should implement them according to your goals. Clicking on nodes, edges or the canvas can all implement their own logic within the event listener (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 ){
...
}
});
With this, you can add nodes to the canvas, skip adding them on nodes and edges or do some other stuff like an alert for the user with some information about why they should choose another location.
If you want to manually check if a location is empty, you can do it naively like this:
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
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…