Skip to content

Instantly share code, notes, and snippets.

@brandonpapworth
Created July 11, 2013 14:29
Show Gist options
  • Save brandonpapworth/5975955 to your computer and use it in GitHub Desktop.
Save brandonpapworth/5975955 to your computer and use it in GitHub Desktop.
Just an example of simulating touch events using the CustomEvent api. Sloppily used jQuery, but this can be easily switched to use addEventListener.
if (!('ontouchstart' in window)) {
// create synthetic touch events
var idCounter = 0,
emptyArray = [];
$(document).on('mousedown',function (e) {
console.log('faking touchstart');
var evt = new CustomEvent('touchstart',{
bubbles : true,
cancelable : true
});
evt.altKey = e.originalEvent.altKey;
evt.ctrlKey = e.originalEvent.ctrlKey;
evt.shiftKey = e.originalEvent.shiftKey;
evt.metaKey = e.originalEvent.metaKey;
evt.charCode = e.originalEvent.charCode;
evt.charCode = e.originalEvent.charCode;
evt.clientX = e.clientX;
evt.clientY = e.clientY;
evt.pageX = e.pageX;
evt.pageY = e.pageY;
evt.screenX = e.screenX;
evt.screenY = e.screenY;
evt.target = e.target;
evt.touches = evt.changedTouches = [
{
clientX : evt.clientX,
clientY : evt.clientY,
identifier : idCounter++,
pageX : evt.pageX,
pageY : evt.pageY,
screenX : evt.screenX,
screenY : evt.screenY,
target : evt.target,
webkitForce : 1,
webkitRadiusX : 1,
webkitRadiusY : 1,
webkitRotationAngle : 0
}
];
e.originalEvent.target.dispatchEvent(evt);
})
.on('mousemove',function (e) {
var evt = new CustomEvent('touchmove',{
bubbles : true,
cancelable : true
});
evt.altKey = e.originalEvent.altKey;
evt.ctrlKey = e.originalEvent.ctrlKey;
evt.shiftKey = e.originalEvent.shiftKey;
evt.metaKey = e.originalEvent.metaKey;
evt.charCode = e.originalEvent.charCode;
evt.charCode = e.originalEvent.charCode;
evt.pageX = e.pageX;
evt.pageY = e.pageY;
evt.screenX = e.screenX;
evt.screenY = e.screenY;
evt.target = e.target;
evt.touches = evt.changedTouches = [
{
clientX : evt.clientX,
clientY : evt.clientY,
identifier : idCounter++,
pageX : evt.pageX,
pageY : evt.pageY,
screenX : evt.screenX,
screenY : evt.screenY,
target : evt.target,
webkitForce : 1,
webkitRadiusX : 1,
webkitRadiusY : 1,
webkitRotationAngle : 0
}
];
e.originalEvent.target.dispatchEvent(evt);
})
.on('mouseup',function (e) {
console.log('faking touchend');
var evt = new CustomEvent('touchend',{
bubbles : true,
cancelable : true
});
evt.altKey = e.originalEvent.altKey;
evt.ctrlKey = e.originalEvent.ctrlKey;
evt.shiftKey = e.originalEvent.shiftKey;
evt.metaKey = e.originalEvent.metaKey;
evt.charCode = e.originalEvent.charCode;
evt.charCode = e.originalEvent.charCode;
evt.pageX = e.pageX;
evt.pageY = e.pageY;
evt.screenX = e.screenX;
evt.screenY = e.screenY;
evt.target = e.target;
evt.touches = emptyArray;
evt.changedTouches = [
{
clientX : evt.clientX,
clientY : evt.clientY,
identifier : idCounter++,
pageX : evt.pageX,
pageY : evt.pageY,
screenX : evt.screenX,
screenY : evt.screenY,
target : evt.target,
webkitForce : 1,
webkitRadiusX : 1,
webkitRadiusY : 1,
webkitRotationAngle : 0
}
];
e.originalEvent.target.dispatchEvent(evt);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment