Adjust event listeners to mobile devices
This commit is contained in:
51
game.js
51
game.js
@@ -269,8 +269,7 @@ class Game {
|
||||
}
|
||||
}
|
||||
|
||||
initEventListeners() {
|
||||
this.layer2Canvas.addEventListener("click", (e) => {
|
||||
clickEventHandler(e) {
|
||||
if (this.isDragging)
|
||||
return;
|
||||
|
||||
@@ -287,6 +286,26 @@ class Game {
|
||||
this.victoryEvent();
|
||||
|
||||
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) => {
|
||||
@@ -301,18 +320,7 @@ class Game {
|
||||
});
|
||||
|
||||
this.layer2Canvas.addEventListener("contextmenu", (e) => {
|
||||
if (this.isDragging)
|
||||
return;
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
const pos = this.getPosition(e);
|
||||
|
||||
this.tileFlag(pos.x, pos.y);
|
||||
|
||||
this.updateBombs();
|
||||
|
||||
this.victoryEvent();
|
||||
this.rightClickEventHandler(e);
|
||||
});
|
||||
|
||||
window.addEventListener("keyup", (e) => {
|
||||
@@ -358,6 +366,13 @@ class Game {
|
||||
|
||||
|
||||
document.addEventListener("mousedown", (e) => {
|
||||
if(isTouchDevice) {
|
||||
e.preventDefault();
|
||||
this.pressTimer = window.setTimeout(function () {
|
||||
game.rightClickEventHandler(e);
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
if (e.button === 0) {
|
||||
this.hasClicked = true;
|
||||
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;
|
||||
if (this.isDragging) {
|
||||
setTimeout(() => {
|
||||
|
@@ -4,8 +4,8 @@
|
||||
<meta charset="UTF-8">
|
||||
<title>Minesweeper.js</title>
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
<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="shortcut icon" type="image/x-icon" href="icons/icon.ico">-->
|
||||
<!--<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">
|
||||
</head>
|
||||
<body>
|
||||
|
@@ -11,6 +11,8 @@ const colors = {
|
||||
6: "pink"
|
||||
};
|
||||
|
||||
const isTouchDevice = 'ontouchstart' in document.documentElement;
|
||||
|
||||
function animateBackground(ctx, x, y, width, height, curOpacity, finalOpacity, startTime, duration, color) {
|
||||
const time = (new Date()).getTime() - startTime;
|
||||
|
||||
|
Reference in New Issue
Block a user