javascript - How to track the position of the mouse on X-axis in JavaScript?
问题描述
I want to know how to track the position of the mouse on X-axis on the screen. Based on limited knowledge of Java/Processing, it is something similar to mouseX in Java. In addition, I want to rotate an image element based on the position of the mouse on X-axis. It would be nice to receive some pieces of advice.
解决方案
一个流行的类似于 Processing 的库/框架是 P5.js(由同一个人开发,但用于 javascript),它可以为您处理这个
但在 vanilla javascript 中,你需要一个事件监听器
var mouse = {
x: undefined,
y:undefined
};
window.addEventListener("mousemove", function(event) {
mouse.x = event.x;
mouse.y = event.y;
});
它的作用是监听鼠标移动,然后将其记录到对象
然后取mouseX位置你可以写
var mouseX = mouse.x;
或者你可以直接从
mouse.x;
//still the same
推荐阅读
- html - 规则之间没有空格是否有任何指导方针?
- vue.js - AOS(滚动动画)可以与 vue 3.0 一起使用吗?
- flask - 使用带有 swagger 2.0 的授权标头进行无限加载
- python - Python pandas Dataframe 列到行操作
- javascript - 访问 AWS 凭证:错误:在 TCPConnectWrap.afterConnect 处连接 ECONNREFUSED 169.254.169.254:80
- r - R 和 RGL:如何为球体添加标签?
- mongodb - 无法从 docker init 脚本配置 Mongo 副本集
- python - Moviepy - 移动音频
- amazon-web-services - AWS KMS KEY - CSV 对象未获取更改
- java - 浏览器未在客户端使用 MSSQL JDBC 驱动程序 MFA 身份验证打开