I've made a large force layout using D3.v4 and canvas. Because of the size I can't really use SVG.
How can I make tooltips for the elements on the canvas?
At the very least, how can I identify where the mouse is on the canvas?
I have tried to use the answer from this question as inspiration to check where the mouse cursor is on the canvas and then use simulation.find(x,y,radius)
to identify nearby nodes. However, that's returning a lot of false positives and I have a feeling that the HTML event and simulation.find()
work differently and/or use different coordinate standards.
Does anyone have any ideas or solutions to this?
Edit: It was suggested to take a look at this question. I have tried to do this, and it works, somewhat. The problem now is that, as suspected, simulation.find(..)
doesn't seem to use these canvas coordinates to find nodes in the network.
For the curious, this is the current function I use to find the cursor and nearby nodes:
canvas = d3.select('canvas')
function getPos(e) {
rect = canvas.node().getBoundingClientRect();
scaleX = width / rect.width;
scaleY = height / rect.height;
x = (e.clientX - rect.left);
y = (e.clientY - rect.top);
node = simulation.find(x,y, 3);
return {
x : x,
y : y,
node : node
};
}