简体   繁体   English

鼠标按下时X和y坐标

[英]X and y coordinates while mousedown

I am currently making a game that requires the player to drag around an object on a touch screen device. 我目前正在制作一款游戏,要求玩家在触摸屏设备上的某个物体周围拖动。

I've used the event listener mousemove , and it has worked on my Intel XDK emulator, but now that I move it to a touch screen device, I need an touch event that is mousemove and mousedown , and I am not sure how to do that. 我已经使用了事件侦听器mousemove ,并且它已经在我的Intel XDK模拟器上运行了,但是现在我将其移动到触摸屏设备上,我需要一个mousemovemousedown的触摸事件,而且我不确定该怎么做。那。

Example of ideal code: 理想代码示例:

canvas.addEventListener("mousemove" && "mousedown", function(e){  
    pointerX = e.pageX;
    pointerY = e.pageY;
}

So this ideal code spits out x and y when the mouse is down, and it has moved. 因此,当鼠标向下移动时,此理想代码会吐出x和y。

If anyone knows the legit syntax or a different method of doing this, it would be a great help. 如果有人知道合法的语法或执行此操作的其他方法,那将是很大的帮助。

I don't want to incorporate JQuery into this, just pure JS. 我不想将JQuery合并到其中,而只是纯JS。

Thanks! 谢谢! :) :)

Edit: Let me rephrase this so it is all straight forward. 编辑:让我改写一下,这样就很简单了。 While the user is dragging an object I need a function that spits out x and y of the pointer the entire drag. 当用户拖动对象时,我需要一个函数,该函数会在整个拖动过程中吐出指针的x和y。

Per MDN: 每个MDN:

The EventTarget.addEventListener() method registers the specified listener on the EventTarget it's called on. EventTarget.addEventListener()方法在调用它的EventTarget上注册指定的侦听器。 The event target may be an Element in a document, the Document itself, a Window, or any other object that supports events (such as XMLHttpRequest). 事件目标可以是文档中的Element,文档本身,Window或支持事件的任何其他对象(例如XMLHttpRequest)。

Note the "eventTarget" above. 注意上面的“ eventTarget”。 The AddEventListener method takes only one string which represents EventType. AddEventListener方法仅采用一个表示EventType的字符串。 You'll need to write a custom function in order to iterate multiple events: 您需要编写一个自定义函数来迭代多个事件:

 const target = document.getElementById("myDiv"); ['mousedown', 'mousemove'].forEach(eventType => { target.addEventListener(eventType, (event) => { target.innerText = `x: ${event.pageX} y: ${event.pageY}`; }); }); 
 #myDiv { width: 100px; height: 100px; background-color: orange; color: white; } 
 <div id="myDiv"></div> 

For touch devices, you should be looking into touchstart , touchend , touchcancel and touchmove . 对于触摸设备,您应该查看touchstarttouchendtouchcanceltouchmove

Touch events are similar to mouse events except they support simultaneous touches and at different locations on the touch surface. 触摸事件与鼠标事件类似,不同之处在于它们支持同时触摸并且在触摸表面的不同位置上。

https://developer.mozilla.org/en-US/docs/Web/API/Touch_events ` https:// developer.mozilla.org/ zh- CN/ docs/ Web/ API/ Touch_events`

MDN has a great post on touch events. MDN在触摸事件方面有很好的帖子。 The posts are not just full of quality information but also there are lots of javascript code shown which will provide you with a good resource for touch events. 这些帖子不仅充满了质量信息,而且还显示了许多JavaScript代码,它们将为您提供有关触摸事件的良好资源。

Interfaces 介面

TouchEvent 触控事件

Represents an event that occurs when the state of touches on the surface changes. 表示在表面上的触摸状态更改时发生的事件。

Touch 触摸

Represents a single point of contact between the user and the touch surface. 表示用户和触摸表面之间的单点接触。

TouchList 触控列表

Represents a group of touches; 代表一组接触; this is used when the user has, for example, multiple fingers on the surface at the same time. 例如,当用户同时在表面上有多个手指时,将使用此功能。


How does this solve my current problem? 这如何解决我当前的问题?

The information above provides greater detail for all the types of events you may want to look into. 上面的信息为您可能需要研究的所有类型的事件提供了更详细的信息。 However, for your specific problem, you should look into touchmove & touchend events. 但是,对于您的特定问题,您应该研究touchmovetouchend事件。

Side note - check touchmove & touchend links for compatibility concerns. 旁注 -检查touchmovetouchend链接是否存在兼容性问题。

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

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