D3 中的鼠标位置 [英] Mouse position in D3
本文介绍了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 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
.
如何保存 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= 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
这篇关于D3 中的鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文