Last active
August 16, 2016 04:50
-
-
Save albertywu/17defcd0710edd103101812f22c517dc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Drag n Drop, using streaming paradigm | |
| const fromEvent = Rx.Observable.fromEvent | |
| const widget = document.getElementById('widget') | |
| const container = document.getElementById('container') | |
| // streams | |
| const mouseDowns = fromEvent(widget, 'mousedown') | |
| const mouseUps = fromEvent(container, 'mouseup') | |
| const mouseMoves = fromEvent(container, 'mousemove') | |
| // drag stream is simply a composite stream! | |
| const drags = mouseDowns.flatMap(evt => mouseMoves.takeUntil(mouseUps)) // flatMap to "merge" branch into trunk | |
| // listen to drag stream and react | |
| drags.subscribe(evt => reposition(widget, evt)) | |
| reposition = (el, evt) => { | |
| el.style.left = evt.clientX + "px" | |
| el.style.top = evt.clientY + "px" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment