如何在d3中使用mousedown获取鼠标坐标? [英] How do I get the mouse coordinates using mousedown in d3?

查看:1111
本文介绍了如何在d3中使用mousedown获取鼠标坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在D3中创建一个图形,您可以在其中绘制一个正方形以进行放大.现在,我正在尝试使mousedown功能起作用.我需要能够单击图形中的任意位置并获取坐标.这就是我现在拥有的:

I am trying to create a graph in D3 where you can draw a square to zoom in. Right now, I am trying to get the mousedown function to work. I need to be able to click anywhere in the graph and get the coordinates. This is what I have now:

svg.on("mousedown", mousedown)
function mousedown()
{
    console.log(event.clientX);
}

我知道这是不正确的,但是我似乎找不到如何访问鼠标坐标的方法.

I know this is not correct, but I can't seem to find how I can access the mouse's coordinates.

推荐答案

首先,您需要正确设置click事件.

firstly, you need to set up the click event properly.

svg.on('mousedown', function() { console.log(d3.event); } );

在您的情况下:

svg.on('mousedown', mousedown);

function mousedown() {
  console.log(d3.event.clientX);
  console.log(d3.event.clientY);
}

这篇关于如何在d3中使用mousedown获取鼠标坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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