I'm using d3.js to draw a rectangle with it's top-left corner at (10,10).
Then I move that rectangle by doing a translate(200, 200)
.
Then I attach an on-mouseout event that simply prints out the coordinates of the mouse pointer. To my surprise the coordinates it reports indicate the boundaries of the rectangle before it was translated. Why???
How can I make it report the actual mouse-coordinates, not the mouse coordinates of where the rectangle used to be before it was translated?
Here is the code:
var myApp = angular.module('myApp', []);
myApp.controller('MyController', function() {
var self = this;
self.svgContainer = d3.select("#my_svg_widget");
var my_rect = self.svgContainer.append("rect")
.attr("x", 10)
.attr("y", 10)
.attr("width", 60)
.attr("height", 60);
my_rect.attr("transform", "translate(200, 200)");
my_rect.on("mouseover", function() {
var coordinates = [0, 0];
coordinates = d3.mouse(this);
var mouseX6 = Number(coordinates[0]);
var mouseY6 = Number(coordinates[1]);
console.log('mouseX6 = ', mouseX6, typeof mouseX6);
console.log('mouseY6 = ', mouseY6, typeof mouseY6);
}
);
}
);
Here is the output:
mouseX6 = 13 number
mouseY6 = 9 number
Here is the Plunker: https://plnkr.co/edit/VSTzUhehdsVLFgmZqzHi?p=preview