Created
March 11, 2014 19:02
-
-
Save irae/9492655 to your computer and use it in GitHub Desktop.
Touch Simulation with steps
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
function touchSequence(params) { | |
var step = 0, | |
config = { | |
target: params.target || document, | |
startX: params.startX || 300, | |
startY: params.startY || 300, | |
endX: params.endX || 300, | |
endY: params.endY || 300, | |
steps: params.steps || 4, | |
interval: params.interval || 25 | |
}, | |
stepX = (config.endX - config.startX) / config.steps, | |
touchId = +new Date() + Math.random(); | |
function doTouch() { | |
var evt = document.createEvent('TouchEvent'), | |
type = (step === 0) ? 'touchstart' : | |
(step === config.steps) ? 'touchend' : | |
'touchmove', | |
curX = config.startX + stepX * step, | |
touch = document.createTouch(window, config.target, touchId, curX, config.startY, curX, config.startY), | |
touchList = document.createTouchList(touch); | |
if (type === 'touchend') { | |
touchList = []; | |
} | |
evt.initTouchEvent(type, true, true, window, 0, | |
curX, params.startY, curX, params.startY, | |
false, false, false, false, | |
touchList, touchList, touchList, | |
1, 0); | |
config.target.dispatchEvent(evt); | |
if (step !== config.steps) { | |
step++; | |
setTimeout(doTouch, config.interval); | |
} | |
} | |
doTouch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment