mirror of
https://github.com/danog/2048.git
synced 2024-11-27 03:44:43 +01:00
Merge pull request #16 from chrisprice/master
Replace hammer with touch event handling
This commit is contained in:
commit
db82cf4952
9
js/hammer.min.js
vendored
9
js/hammer.min.js
vendored
File diff suppressed because one or more lines are too long
@ -57,20 +57,31 @@ KeyboardInputManager.prototype.listen = function () {
|
||||
retry.addEventListener("click", this.restart.bind(this));
|
||||
|
||||
// Listen to swipe events
|
||||
var gestures = [Hammer.DIRECTION_UP, Hammer.DIRECTION_RIGHT,
|
||||
Hammer.DIRECTION_DOWN, Hammer.DIRECTION_LEFT];
|
||||
|
||||
var touchStartClientX, touchStartClientY;
|
||||
var gameContainer = document.getElementsByClassName("game-container")[0];
|
||||
var handler = Hammer(gameContainer, {
|
||||
drag_block_horizontal: true,
|
||||
drag_block_vertical: true
|
||||
gameContainer.addEventListener("touchstart", function(event) {
|
||||
if (event.touches.length > 1) {
|
||||
return;
|
||||
}
|
||||
touchStartClientX = event.touches[0].clientX;
|
||||
touchStartClientY = event.touches[0].clientY
|
||||
event.preventDefault();
|
||||
});
|
||||
|
||||
handler.on("swipe", function (event) {
|
||||
event.gesture.preventDefault();
|
||||
mapped = gestures.indexOf(event.gesture.direction);
|
||||
|
||||
if (mapped !== -1) self.emit("move", mapped);
|
||||
gameContainer.addEventListener("touchmove", function(event) {
|
||||
event.preventDefault();
|
||||
});
|
||||
gameContainer.addEventListener("touchend", function(event) {
|
||||
if (event.touches.length > 0) {
|
||||
return;
|
||||
}
|
||||
var dx = event.changedTouches[0].clientX - touchStartClientX;
|
||||
var absDx = Math.abs(dx);
|
||||
var dy = event.changedTouches[0].clientY - touchStartClientY;
|
||||
var absDy = Math.abs(dy);
|
||||
if (Math.max(absDx, absDy) > 10) {
|
||||
self.emit("move", absDx > absDy ? (dx > 0 ? 1 : 3) :
|
||||
(dy > 0 ? 2 : 0)); // (right : left) : (down : up)
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user