crowds/js/lib/Mouse.js

69 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-03-27 17:39:08 +00:00
/////////////////////////////
// MOUSE ////////////////////
/////////////////////////////
var Mouse = {
x:0, y:0,
pressed:false
};
2018-03-28 14:57:19 +00:00
Mouse.ondown = function(event){
2018-03-27 17:39:08 +00:00
Mouse.pressed = true;
2018-03-28 14:57:19 +00:00
Mouse.onmove(event);
publish("mouse/down");
2018-03-27 17:39:08 +00:00
};
2018-03-28 14:57:19 +00:00
Mouse.onmove = function(event){
2018-03-27 17:39:08 +00:00
Mouse.x = event.clientX;
Mouse.y = event.clientY;
2018-03-28 14:57:19 +00:00
publish("mouse/move");
2018-03-27 17:39:08 +00:00
};
2018-03-28 14:57:19 +00:00
Mouse.onup = function(event){
2018-03-27 17:39:08 +00:00
Mouse.pressed = false;
2018-03-28 14:57:19 +00:00
publish("mouse/up");
2018-03-27 17:39:08 +00:00
};
Mouse.update = function(){
// Just pressed, or just released (one frame ago)
Mouse.justPressed = (!Mouse.lastPressed && Mouse.pressed);
Mouse.justReleased = (Mouse.lastPressed && !Mouse.pressed);
// The last frame's stuff
Mouse.lastX = Mouse.x;
Mouse.lastY = Mouse.y;
Mouse.lastPressed = Mouse.pressed;
};
// TOUCH.
function _touchWrapper(callback){
return function(event){
var _event = {};
_event.clientX = event.changedTouches[0].clientX;
_event.clientY = event.changedTouches[0].clientY;
event.preventDefault();
callback(_event);
};
}
// ALSO DON'T SCROLL WHEN TOUCH
/*document.addEventListener("touchstart", function(e){
e.preventDefault();
},false);
document.addEventListener("touchmove", function(e){
e.preventDefault();
},false);*/
2018-03-27 17:39:08 +00:00
// INIT
Mouse.init = function(target){
// Regular mouse
2018-03-28 14:57:19 +00:00
target.addEventListener("mousedown", Mouse.ondown);
target.addEventListener("mousemove", Mouse.onmove);
window.addEventListener("mouseup", Mouse.onup);
2018-03-27 17:39:08 +00:00
// Touch events
2018-03-28 14:57:19 +00:00
target.addEventListener("touchstart", _touchWrapper(Mouse.ondown), false);
target.addEventListener("touchmove", _touchWrapper(Mouse.onmove), false);
2018-03-27 17:39:08 +00:00
document.body.addEventListener("touchend", function(){
2018-03-28 14:57:19 +00:00
Mouse.onup();
2018-03-27 17:39:08 +00:00
}, false);
};