Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add IE touch support via pointer events #43

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 44 additions & 17 deletions js/keyboard_input_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,58 @@ KeyboardInputManager.prototype.listen = function () {
// Listen to swipe events
var touchStartClientX, touchStartClientY;
var gameContainer = document.getElementsByClassName("game-container")[0];
gameContainer.addEventListener("touchstart", function(event) {
if (event.touches.length > 1) return;

touchStartClientX = event.touches[0].clientX;
touchStartClientY = event.touches[0].clientY;
event.preventDefault();
});
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 touchFinish = function (x, y) {
var dx = x - touchStartClientX;
var absDx = Math.abs(dx);

var dy = event.changedTouches[0].clientY - touchStartClientY;
var dy = y - touchStartClientY;
var absDy = Math.abs(dy);

if (Math.max(absDx, absDy) > 10) {
// (right : left) : (down : up)
self.emit("move", absDx > absDy ? (dx > 0 ? 1 : 3) : (dy > 0 ? 2 : 0));
}
});
}

if (window.PointerEvent) {
gameContainer.addEventListener("pointerdown", function (event) {
if (event.isPrimary) {
touchStartClientX = event.clientX;
touchStartClientY = event.clientY;
event.preventDefault();
}
});
gameContainer.addEventListener("pointermove", function (event) {
if (event.isPrimary) {
event.preventDefault();
}
});
gameContainer.addEventListener("pointerup", function (event) {
if (event.isPrimary) {
touchFinish(event.clientX, event.clientY);
}
});
}
else {
gameContainer.addEventListener("touchstart", function (event) {
if (event.touches.length > 1) {
return;
}

touchStartClientX = event.touches[0].clientX;
touchStartClientY = event.touches[0].clientY;
event.preventDefault();
});
gameContainer.addEventListener("touchmove", function (event) {
event.preventDefault();
});
gameContainer.addEventListener("touchend", function (event) {
if (event.touches.length > 0) {
return;
}
touchFinish(event.changedTouches[0].clientX, event.changedTouches[0].clientY);
});
}
};

KeyboardInputManager.prototype.restart = function (event) {
Expand Down
16 changes: 4 additions & 12 deletions style/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ h1.title {
100% {
top: -50px;
opacity: 0; } }

@-moz-keyframes move-up {
0% {
top: 25px;
Expand All @@ -39,7 +38,6 @@ h1.title {
100% {
top: -50px;
opacity: 0; } }

@keyframes move-up {
0% {
top: 25px;
Expand All @@ -48,7 +46,6 @@ h1.title {
100% {
top: -50px;
opacity: 0; } }

.score-container {
position: relative;
float: right;
Expand Down Expand Up @@ -116,26 +113,25 @@ hr {

100% {
opacity: 1; } }

@-moz-keyframes fade-in {
0% {
opacity: 0; }

100% {
opacity: 1; } }

@keyframes fade-in {
0% {
opacity: 0; }

100% {
opacity: 1; } }

.game-container {
margin-top: 40px;
position: relative;
padding: 15px;
cursor: default;
-ms-user-select: none;
touch-action: none;
-webkit-touch-callout: none;
-webkit-user-select: none;
-moz-user-select: none;
Expand Down Expand Up @@ -361,7 +357,6 @@ hr {
opacity: 1;
-webkit-transform: scale(1);
-moz-transform: scale(1); } }

@-moz-keyframes appear {
0% {
opacity: 0;
Expand All @@ -372,7 +367,6 @@ hr {
opacity: 1;
-webkit-transform: scale(1);
-moz-transform: scale(1); } }

@keyframes appear {
0% {
opacity: 0;
Expand All @@ -383,7 +377,6 @@ hr {
opacity: 1;
-webkit-transform: scale(1);
-moz-transform: scale(1); } }

.tile-new {
-webkit-animation: appear 200ms ease 100ms;
-moz-animation: appear 200ms ease 100ms;
Expand All @@ -402,7 +395,6 @@ hr {
100% {
-webkit-transform: scale(1);
-moz-transform: scale(1); } }

@-moz-keyframes pop {
0% {
-webkit-transform: scale(0);
Expand All @@ -415,7 +407,6 @@ hr {
100% {
-webkit-transform: scale(1);
-moz-transform: scale(1); } }

@keyframes pop {
0% {
-webkit-transform: scale(0);
Expand All @@ -428,7 +419,6 @@ hr {
100% {
-webkit-transform: scale(1);
-moz-transform: scale(1); } }

.tile-merged {
z-index: 20;
-webkit-animation: pop 200ms ease 100ms;
Expand Down Expand Up @@ -468,6 +458,8 @@ hr {
position: relative;
padding: 10px;
cursor: default;
-ms-user-select: none;
touch-action: none;
-webkit-touch-callout: none;
-webkit-user-select: none;
-moz-user-select: none;
Expand Down
2 changes: 2 additions & 0 deletions style/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ hr {
padding: $grid-spacing;

cursor: default;
-ms-user-select: none;
touch-action: none;
-webkit-touch-callout: none;
-webkit-user-select: none;
-moz-user-select: none;
Expand Down