Created
October 22, 2014 14:06
-
-
Save Alex1990/d462cb9f90ed2b1a9390 to your computer and use it in GitHub Desktop.
Convert mouse events into touch events.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Convert mouse events into touch events. | |
* From: http://ross.posterous.com/2008/08/19/iphone-touch-events-in-javascript/ | |
*/ | |
function touchHandler(event) { | |
var touch = event.changedTouches[0]; | |
var simulatedEvent = document.createEvent("MouseEvent"); | |
simulatedEvent.initMouseEvent({ | |
touchstart: "mousedown", | |
touchmove: "mousemove", | |
touchend: "mouseup" | |
}[event.type], true, true, window, 1, | |
touch.screenX, touch.screenY, | |
touch.clientX, touch.clientY, false, | |
false, false, false, 0, null); | |
touch.target.dispatchEvent(simulatedEvent); | |
event.preventDefault(); | |
} | |
function init() { | |
document.addEventListener("touchstart", touchHandler, true); | |
document.addEventListener("touchmove", touchHandler, true); | |
document.addEventListener("touchend", touchHandler, true); | |
document.addEventListener("touchcancel", touchHandler, true); | |
} |
jseparator
commented
Jan 13, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment