Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
182
rated 0 times [  187] [ 5]  / answers: 1 / hits: 103047  / 11 Years ago, mon, may 27, 2013, 12:00:00

I just wanted to get the mouse position using D3 by using the following code:



var x = 0;

svg.on('mousemove', function () {
x = d3.mouse(this)[0];
});


but x is always equal to 0. By using console.log(), I can see that x value is getting changed just inside the function() but out of it x got its initial value of 0.



How can I save the x value and use it later in my application?


More From » d3.js

 Answers
3

You have to use an array. That will store x and y like:



var coordinates= d3.mouse(this);
var x = coordinates[0];
var y = coordinates[1];

// D3 v4
var x = d3.event.pageX - document.getElementById(<id-of-your-svg>).getBoundingClientRect().x + 10
var y = d3.event.pageY - document.getElementById(<id-of-your-svg>).getBoundingClientRect().y + 10

[#78002] Saturday, May 25, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gerardamosy

Total Points: 600
Total Questions: 116
Total Answers: 102

Location: Ukraine
Member since Tue, May 30, 2023
1 Year ago
gerardamosy questions
;