Skip to content

Instantly share code, notes, and snippets.

@Alex1990
Created October 22, 2014 14:06
Show Gist options
  • Save Alex1990/d462cb9f90ed2b1a9390 to your computer and use it in GitHub Desktop.
Save Alex1990/d462cb9f90ed2b1a9390 to your computer and use it in GitHub Desktop.
Convert mouse events into touch events.
/**
* 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);
}
@zanderwar
Copy link

zanderwar commented Jul 30, 2021

initMouseEvent is deprecated
https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/initMouseEvent

Use this instead, note that both this and the original can cause very weird behaviour with select elements on iOS, ensure you only use it where appropriate and don't blanket every touch event in your app with it:

Not supported in IE

function touchHandler(event) {
	var touch = event.changedTouches[0];
	var simulatedEvent = new MouseEvent({
		touchstart: "mousedown",
		touchmove: "mousemove",
		touchend: "mouseup"
	}[event.type], {
		bubbles: true, cancelable: true, view: window, detail: 1,
		screenX: touch.screenX, screenY: touch.screenY, clientX: touch.clientX, clientY: touch.clientY,
		ctrlKey: false, altKey: false, shiftKey: false, metaKey: false, button: 0, relatedTarget: null
	});
	touch.target.dispatchEvent(simulatedEvent);
}
function init() {
        // I suggest you be far more specific than "document"
	document.addEventListener("touchstart", touchHandler, true);
	document.addEventListener("touchmove", touchHandler, true);
	document.addEventListener("touchend", touchHandler, true);
	document.addEventListener("touchcancel", touchHandler, true);
}

@jseparator
Copy link

((win, doc) => {
  /**
   * @param e {MouseEvent}
   */
  function mouseHandler(e) {
    e.preventDefault()
    let type = {
      mousedown: "touchstart",
      mousemove: "touchmove",
      mouseup: "touchend"
    }[e.type]

    let touch = new Touch({
      identifier: e.button,
      clientX: e.clientX,
      clientY: e.clientY,
      screenX: e.screenX,
      screenY: e.screenY,
      target: e.target,
      touchType: "direct"
    })
    let te = new TouchEvent(type, {
      bubbles: true,
      cancelable: true,
      touches: [touch],
      changedTouches: [touch],
      targetTouches: [touch],
    })
    e.target.dispatchEvent(te)
  }

  doc.addEventListener("mousedown", mouseHandler, true)
  doc.addEventListener("mousemove", mouseHandler, true)
  doc.addEventListener("mouseup", mouseHandler, true)
})(window, document)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment