# HG changeset patch # User Oleksandr Gavenko # Date 1485826867 -7200 # Node ID d1b12b602915e2eb08d005f20653426dfc37a535 # Parent 44cb6fbad74d019d2f819af44c7484735f61cb78 Add swipe support for mobile devises. Fix bug after renaming. diff -r 44cb6fbad74d -r d1b12b602915 2048.html --- a/2048.html Sun Aug 07 23:24:28 2016 +0300 +++ b/2048.html Tue Jan 31 03:41:07 2017 +0200 @@ -2,7 +2,7 @@ 2048 AI - + @@ -610,6 +610,41 @@ } }, false); + ui.swipe = {startX: 0, startY: 0, endX: 0, endY: 0}; + ui.swipe.direction = function(x, y) { + var sensitivityThreshold = boardDom.width * boardDom.height / 10; + if (x*x + y*y < sensitivityThreshold) + return null; + if (Math.abs(x) < Math.abs(y)) { + if (y > 0) + return "down"; + else + return "up"; + } else { + if (x > 0) + return "right"; + else + return "left"; + } + } + boardDom.addEventListener("touchstart", function(ev) { + ui.swipe.startX = ev.touches[0].pageX; + ui.swipe.startY = ev.touches[0].pageY; + ev.preventDefault(); + }, false); + boardDom.addEventListener("touchmove", function(ev) { + ui.swipe.endX = ev.touches[0].pageX; + ui.swipe.endY = ev.touches[0].pageY; + ev.preventDefault(); + }, false); + boardDom.addEventListener("touchend", function(ev) { + var dir = ui.swipe.direction(ui.swipe.endX - ui.swipe.startX, ui.swipe.endY - ui.swipe.startY); + if (dir) { + ui.action[dir](event); + ev.preventDefault(); + } + }, false); + document.getElementById("test").addEventListener("click", function() { event.preventDefault(); event.stopPropagation(); board.move.replaceByBoardJS(ui.brdEngine); @@ -1069,7 +1104,7 @@ ui.game.checkGameOver(); } catch (ex) { } } else { - ui.action.start(); + ui.action.reset(); }