Skip to content

Instantly share code, notes, and snippets.

@albertywu
Last active August 16, 2016 04:50
Show Gist options
  • Select an option

  • Save albertywu/17defcd0710edd103101812f22c517dc to your computer and use it in GitHub Desktop.

Select an option

Save albertywu/17defcd0710edd103101812f22c517dc to your computer and use it in GitHub Desktop.
// 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