Mouse position in D3

前端 未结 4 1741
别跟我提以往
别跟我提以往 2020-12-04 08:54

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];               


        
4条回答
  •  孤街浪徒
    2020-12-04 09:01

    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().getBoundingClientRect().x + 10
    var y = d3.event.pageY - document.getElementById().getBoundingClientRect().y + 10
    

提交回复
热议问题