Skip to content

Instantly share code, notes, and snippets.

@gabrielmontagne
Created December 3, 2018 13:42
Show Gist options
  • Save gabrielmontagne/f3e4b170edf8219080e5d6e524ea4f93 to your computer and use it in GitHub Desktop.
Save gabrielmontagne/f3e4b170edf8219080e5d6e524ea4f93 to your computer and use it in GitHub Desktop.
Quick and dirty RxJS 5 animation interpolator
function createTransition$(start, duration=1000, interval=250, interpolator=linear) {
const ticks = duration / interval
let current = start
const to$ = new Subject()
const out$ = to$.switchMap(
t => {
const int = interpolator(current, t)
return Observable.interval(interval).take(ticks).map(t => (t + 1)/ticks)
.startWith(0)
.map(int)
.do(v => current = v)
}
)
return Subject.create(to$, out$)
}
a$ = createTransition$(10, 2000, 500, linear)
if (s)
s.unsubscribe()
s = a$.subscribe(log('FINAL'))
a$.next(100)
function linear(from, to) {
return i => (to - from) * i + from
}
function log(label) {
return {
next: n => console.log(`%cN ${label}>`, 'color: green', n),
error: e => console.log(`%cE ${label}>`, 'color: orange', e),
complete: () => console.info(`%cC ${label}>`, 'color: blue')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment