I want to display a moving cross hairs with coordinates when the cursor is moved over a particular DIV containing an SVG.
On mouseenter
I can successfully create a rect
displaying the coordinates (and remove it on mouseout
), however, moving the cursor over the newly created rect
or text itself fires a mouseout
mouseenter
event cycle.
I've tried d3.event.stopPropagation()
in several places, but none seem to work.
The picture shows if you carefully move the mouse onto the grey "screen" - the rect & text is created and stays in one place.
But if you move the cursor to touch "bobo" or the green rectangle, it starts moving.
var infoBox = null;
var theSVG = d3.select("#theScreen")
.append("svg")
.attr("width", 250)
.attr("height", 250);
// Register mouse events
theSVG
.on("mouseenter", mouseEnter)
.on("mouseout", mouseExit);
function mouseEnter()
{
if (infoBox !== null)
return;
var coord = d3.mouse(d3.event.currentTarget);
x1 = parseInt(coord[0]);
y1 = parseInt(coord[1]);
console.log("mouseEnter", x1, y1, infoBox);
infoBox = theSVG.append("g")
.attr('class', 'ssInfoBox');
var rectItem = infoBox.append("rect")
.attr('x', x1)
.attr('y', y1)
.attr('width', 30)
.attr('height', 20);
var textItem = infoBox.append("text")
.attr('x', x1)
.attr('y', y1)
.text("bobo");
}
function mouseExit()
{
if (infoBox === null)
return;
console.log("mouseExit", infoBox);
infoBox.remove()
infoBox = null;
}
The code doesn't implement the moving yet. To start, I just want the rect/text created and destroyed on mouseenter
and mouseout
.
How do I do that?