鼠标在D3中的位置 [英] Mouse position in D3

查看:1163
本文介绍了鼠标在D3中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想使用D3通过使用下面的代码获得鼠标位置:

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

但我总是得到x = 0!
通过使用 console.log(),我可以看到 x function()但是它 x 得到其初始值= 0。

but what I always get x=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 initiate value=0.

如何保存 x 值,然后在我的应用程序中使用它?

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

推荐答案

您必须使用数组。将存储 x y 如:

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

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

这篇关于鼠标在D3中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆