Created
July 20, 2017 18:54
-
-
Save efthemiosprime/974f01f79a8a723dc24b8bc06616b77b to your computer and use it in GitHub Desktop.
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
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