f0ck-fe-old/test4/js/swipe.js

67 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2021-12-31 06:18:41 +00:00
import { scroll } from './events.js';
import rt from './runtimes.js';
const eventtmp = {
touchTracker: []
};
const copyTouch = touch => {
return {
identifier: touch.identifier,
screenX: touch.screenX,
screenY: touch.screenY
};
};
const getTrackedTouchIndex = identifier => {
for(let i = 0; i < eventtmp.touchTracker.length; i++) {
if(eventtmp.touchTracker[i].identifier == identifier)
return i;
}
return -1;
};
const removeTouch = identifier => {
let index = getTrackedTouchIndex(ct.item(i).identifier);
if(index === -1)
return index;
eventtmp.touchTracker.splice(index, 1);
};
const touchstart = e => {
if(!rt.events.scroll) {
const ct = e.changedTouches;
for(let i = 0; i < e.changedTouches.length; i++)
eventtmp.touchTracker.push(copyTouch(ct.item(i)));
}
};
const touchmove = e => {
e.preventDefault();
};
const touchend = e => {
e.preventDefault();
if(!rt.events.scroll) {
let ct = e.changedTouches;
for(let i = 0; i < ct.length; i++) {
let index = getTrackedTouchIndex(ct.item(i).identifier);
if(index !== -1) {
let t = copyTouch(eventtmp.touchTracker[index]);
if(Math.abs(Math.atan((ct.item(i).screenX - t.screenX) / (ct.item(i).screenY - t.screenY))) < 30)
scroll({ deltaY: t.screenY > ct.item(i).screenY?1:-1 });
}
}
}
};
const touchcancel = e => {
e.preventDefault();
const ct = e.changedTouches;
ct.forEach(e => removeTouch(ct.item(ct).identifier));
};
window.addEventListener("touchstart", touchstart);
window.addEventListener("touchmove", touchmove);
window.addEventListener("touchend", touchend);
window.addEventListener("touchcancel", touchcancel);