赞 | 54 |
VIP | 0 |
好人卡 | 3 |
积分 | 46 |
经验 | 31992 |
最后登录 | 2024-11-7 |
在线时间 | 1206 小时 |
Lv3.寻梦者
- 梦石
- 0
- 星屑
- 4598
- 在线时间
- 1206 小时
- 注册时间
- 2016-4-7
- 帖子
- 982
|
- Input = {
- init: function () {
- this.mouseScroll = 0;
- this.keyStauts = {};
- this._mouseKey = [
- 'MouseLeft',
- 'MouseMiddle',
- 'MouseRight'
- ];
- this.x = 0;
- this.y = 0;
- this._setupEventHandlers();
- },
- _setupEventHandlers: function () {
- let self = this;
- document.addEventListener('mousemove', (event)=>{
- self.x = event.clientX;
- self.y = event.clientY;
- });
- document.addEventListener('mousewheel', (event)=>{
- self.mouseScroll = event.wheelDelta > 0 ? 1 : -1;
- });
- document.addEventListener('mousedown', (event)=>{
- let key = this._mouseKey[event.button];
- self.keyStauts[key] = 1
- });
- document.addEventListener('mouseup', (event)=>{
- let key = this._mouseKey[event.button];
- self.keyStauts[key] = 0
- });
- document.addEventListener('keydown', (event)=>{
- self.keyStauts[event.keyCode == 32 ? 'Space' : event.key] = 1;
- });
- document.addEventListener('keyup', (event)=>{
- self.keyStauts[event.keyCode == 32 ? 'Space' : event.key] = 0
- });
- },
- update: function () {
- this.mouseScroll = 0;
- },
- clear: function () {
- this.mouseScroll = 0;
- this.x = 0;
- this.y = 0;
- this.keyStauts = {};
- },
- keyDown: function (keyName) {
- if(!this.keyStauts[keyName]) return false;
- return this.keyStauts[keyName] == 1
- },
- keyPress: function (keyName) {
- if(!this.keyStauts[keyName]) return false;
- return this.keyStauts[keyName] == -1
- },
- keyUp: function (keyName) {
- if(!this.keyStauts[keyName]) return true;
- return this.keyStauts[keyName] == 0
- },
- mousePos : function () {
- return {'x': this.x,'y': this.y}
- }
- };
复制代码
然后 input.mousePos()就能取当前的鼠标坐标。然后就是判断点是否在rect上了。 |
|