Adjust event listeners to mobile devices

This commit is contained in:
Marcel
2018-05-13 11:43:09 +02:00
parent abf094da4d
commit 9dc90621ab
3 changed files with 54 additions and 31 deletions

51
game.js
View File

@@ -269,8 +269,7 @@ class Game {
} }
} }
initEventListeners() { clickEventHandler(e) {
this.layer2Canvas.addEventListener("click", (e) => {
if (this.isDragging) if (this.isDragging)
return; return;
@@ -287,6 +286,26 @@ class Game {
this.victoryEvent(); this.victoryEvent();
clicked(e); clicked(e);
}
rightClickEventHandler(e) {
if (this.isDragging)
return;
e.preventDefault();
const pos = this.getPosition(e);
this.tileFlag(pos.x, pos.y);
this.updateBombs();
this.victoryEvent();
}
initEventListeners() {
this.layer2Canvas.addEventListener("click", (e) => {
this.clickEventHandler(e);
}); });
this.layer2Canvas.addEventListener("dblclick", (e) => { this.layer2Canvas.addEventListener("dblclick", (e) => {
@@ -301,18 +320,7 @@ class Game {
}); });
this.layer2Canvas.addEventListener("contextmenu", (e) => { this.layer2Canvas.addEventListener("contextmenu", (e) => {
if (this.isDragging) this.rightClickEventHandler(e);
return;
e.preventDefault();
const pos = this.getPosition(e);
this.tileFlag(pos.x, pos.y);
this.updateBombs();
this.victoryEvent();
}); });
window.addEventListener("keyup", (e) => { window.addEventListener("keyup", (e) => {
@@ -358,6 +366,13 @@ class Game {
document.addEventListener("mousedown", (e) => { document.addEventListener("mousedown", (e) => {
if(isTouchDevice) {
e.preventDefault();
this.pressTimer = window.setTimeout(function () {
game.rightClickEventHandler(e);
}, 1000)
}
if (e.button === 0) { if (e.button === 0) {
this.hasClicked = true; this.hasClicked = true;
this.startClientX = e.clientX; this.startClientX = e.clientX;
@@ -367,7 +382,13 @@ class Game {
} }
}); });
document.addEventListener("mouseup", () => { document.addEventListener("mouseup", (e) => {
if(isTouchDevice) {
e.preventDefault();
clearTimeout(this.pressTimer);
this.clickEventHandler(e);
}
this.hasClicked = false; this.hasClicked = false;
if (this.isDragging) { if (this.isDragging) {
setTimeout(() => { setTimeout(() => {

View File

@@ -4,8 +4,8 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Minesweeper.js</title> <title>Minesweeper.js</title>
<link rel="stylesheet" href="css/style.css"> <link rel="stylesheet" href="css/style.css">
<link rel="shortcut icon" type="image/x-icon" href="icons/icon.ico"> <!--<link rel="shortcut icon" type="image/x-icon" href="icons/icon.ico">-->
<link rel="icon" type="image/png" href="icons/icon.png" sizes="32x32"> <!--<link rel="icon" type="image/png" href="icons/icon.png" sizes="32x32">-->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
</head> </head>
<body> <body>

View File

@@ -11,6 +11,8 @@ const colors = {
6: "pink" 6: "pink"
}; };
const isTouchDevice = 'ontouchstart' in document.documentElement;
function animateBackground(ctx, x, y, width, height, curOpacity, finalOpacity, startTime, duration, color) { function animateBackground(ctx, x, y, width, height, curOpacity, finalOpacity, startTime, duration, color) {
const time = (new Date()).getTime() - startTime; const time = (new Date()).getTime() - startTime;