Skip to content

Instantly share code, notes, and snippets.

@efthemiosprime
Created July 20, 2017 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save efthemiosprime/974f01f79a8a723dc24b8bc06616b77b to your computer and use it in GitHub Desktop.
Save efthemiosprime/974f01f79a8a723dc24b8bc06616b77b to your computer and use it in GitHub Desktop.
import Rx from "rxjs";
import r from "ramda";
export class Swipe {
constructor(dom) {
let xDown = null;
let yDown = null;
this.observer = new Rx.Subject();
const touchStart = Rx.Observable.fromEvent(dom, "touchstart");
touchStart.subscribe( e => {
xDown = e.touches[0].clientX;
yDown = e.touches[0].clientY;
});
const touchMove = Rx.Observable.fromEvent(dom, "touchmove");
touchMove.subscribe(e=> {
if (!xDown || !yDown) return;
let xUp = e.touches[0].clientX;
let yUp = e.touches[0].clientY;
let xDiff = xDown - xUp;
let yDiff = yDown - yUp;
if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/
if ( xDiff > 0 ) {
/* left swipe */
console.log("swipe: left");
// this.observer.onNext("swipe:left");
} else {
/* right swipe */
console.log("swipe: right");
// this.observer.onNext("swipe:right");
}
} else {
if ( yDiff > 0 ) {
/* up swipe */
// console.log("swipe: up");
} else {
/* down swipe */
// console.log("swipe: down");
}
}
/* reset values */
xDown = null;
yDown = null;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment