简体   繁体   English

无法通过javascript获取鼠标坐标

[英]Can't get mouse coordinates through javascript

Recently I have been trying to get a grasp of javascript.最近我一直在尝试掌握 javascript。 I've been experimenting through the past couple of months with different code to see what all is possible through javascript.在过去的几个月里,我一直在尝试使用不同的代码,以了解通过 javascript 实现的一切。 Recently I've been trying to find various ways to capture the mouse coordinates through javascript with a canvas.最近我一直在尝试寻找各种方法来通过带有画布的 javascript 捕获鼠标坐标。 Here's my attempt: http://jsfiddle.net/f8n2one4/这是我的尝试: http : //jsfiddle.net/f8n2one4/

As you can see, there's a MouseHandler which is supposed to capture the mouse coordinates through the ClientX and ClientY variables.如您所见,有一个 MouseHandler 应该通过 ClientX 和 ClientY 变量捕获鼠标坐标。 However, when I try to display these variables, nothing comes up.但是,当我尝试显示这些变量时,什么也没有出现。 Why is that?这是为什么?

document.getElementById("test").innerHTML = "x: " + x + " y: " + y;

Shouldn't it show the x and y values?它不应该显示 x 和 y 值吗?

The x and y variable are out of the scope of your draw() method. x 和 y 变量超出了 draw() 方法的范围。

try the following:尝试以下操作:

function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    player();
    MouseHandler.init(document);
    var clientPosition = MouseHandler.getPos();
    document.getElementById("test").innerHTML = "x: " + clientPosition.x + " y: " + clientPosition.y;   
}

See it working here.看到它在这里工作。

I would recommend not to initialize the whole MouseHandler every interval, just getting the position on every interval.我建议不要在每个间隔初始化整个 MouseHandler,只在每个间隔获取位置。

Also if you are animating use .requestanimationframe() instead of setInterval, you'll get better performance.此外,如果您正在制作动画,请使用 .requestanimationframe() 而不是 setInterval,您将获得更好的性能。

In your example are you are initializing the mouse handler (with MouseHandler.init(element) ) but the x and y values have to be accessed using the getPos() method:在您的示例中,您正在初始化鼠标处理程序(使用MouseHandler.init(element) ),但必须使用getPos()方法访问xy值:

document.getElementById("test").innerHTML = 
    "x: " + MouseHandler.getPos().x + 
    " y: " + MouseHandler.getPos().y;

You can see a working update to your fiddle here你可以在这里看到你的小提琴的工作更新

The x and y do not exist. xy不存在。 You need to use:您需要使用:

document.getElementById("test").innerHTML = "x: " + MouseHandler.getPos().x + " y: " + MouseHandler.getPos().y;

Fiddle: http://jsfiddle.net/f8n2one4/3/小提琴: http : //jsfiddle.net/f8n2one4/3/

It's actually really simple, your x, y are out of scope.其实很简单,你的 x, y 超出了范围。

You could access them via your MouseHandler variable :您可以通过 MouseHandler 变量访问它们:

MouseHandler.getPos().x
MouseHandler.getPos().y

Or try OOP Javascript and instantiating the MouseHandler with a 'new' operator或者尝试 OOP Javascript 并使用“新”运算符实例化 MouseHandler

var MouseHandler = function() {
    this.x = 0;
    this.y = 0;
}

var myMouseHandler = new MouseHandler();

console.log(myMouseHandler.x + myMouseHandler.y);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM