Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

ignore touch event if it does not originate from this joystick instance #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions joy.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ var JoyStick = (function(container, parameters, callback)
var movedX=centerX;
var movedY=centerY;

var lastTouchPositionX = undefined;
var lastTouchPositionY = undefined;

// Check if the device support the touch or not
if("ontouchstart" in document.documentElement)
{
Expand Down Expand Up @@ -186,6 +189,10 @@ var JoyStick = (function(container, parameters, callback)
{
movedX = event.targetTouches[0].pageX;
movedY = event.targetTouches[0].pageY;

lastTouchPositionX = movedX
lastTouchPositionY = movedY

// Manage offset
if(canvas.offsetParent.tagName.toUpperCase() === "BODY")
{
Expand Down Expand Up @@ -215,6 +222,15 @@ var JoyStick = (function(container, parameters, callback)

function onTouchEnd(event)
{
if (!lastTouchPositionX ||
!lastTouchPositionY ||
(event.changedTouches.length > 0 &&
(event.changedTouches[0].pageX - lastTouchPositionX > 10 ||
event.changedTouches[0].pageY - lastTouchPositionY > 10)))
return

lastTouchPositionX = undefined
lastTouchPositionY = undefined
pressed = 0;
// If required reset position store variable
if(autoReturnToCenter)
Expand Down