Mouse position in D3

前端 未结 4 1725
别跟我提以往
别跟我提以往 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:04

    V3:

    var svg = d3.select('body').append('svg')
        .attr('width', 600)
        .attr('height', 600)
        .on('mousemove', function() {
          console.log( d3.mouse(this) ) // log the mouse x,y position
        });
    

    V4 and V5:

    var svg = d3.select('body').append('svg')
        .attr('width', 600)
        .attr('height', 600)
        .on('mousemove', function() {
          console.log( d3.event.pageX, d3.event.pageY ) // log the mouse x,y position
        });
    

提交回复
热议问题