6. RxJS and Observables
RxJS 5 on Github:
https://github.com/reactivex/rxjs
RxJS5 docs:
http://reactivex.io/rxjs
TC39 Observable Proposal
https://github.com/tc39/proposal-observable
7. Observable (Quick Version)
• A set of values over time
• On subscription, ties and observer to a producer of values
• On completion or unsubscription, executes tear down
12. I always mention "animation" as
a big source of async
(but I never talk about it)
13. Different methods of animation
• CSS
• JavaScript
• Raw JavaScript
• Web Animation API (Some browsers)
• JQuery
• D3
• Greensock
• … so many more
14. Animation (defined)
”The technique of photographing successive drawings or positions of
puppets or models to create an illusion of movement when the movie
is shown as a sequence”
29. What is a Scheduler in Rx?
Schedulers control timing around when events occur:
• next
• error
• complete
• subscription
30. What is a Scheduler in Rx?
Used in Observable most creation methods if provided as the last
argument:
• Observable.of(1, 2, 3, scheduler);
• Observable.from(['foo', 'bar'], scheduler);
• Observable.range(0, 10, scheduler);
• Observable.timer(1000, scheduler);
31. What is a Scheduler in Rx?
Used with special operators for control over when existing observables'
events occur:
• someObservable$.observeOn(scheduler);
• someObservable$.subscribeOn(scheduler);
32. RxJS 5 Schedulers*
• queue – During the same "job" but on a queue. Aka "breadth first".
• asap – Next "job" aka "microtask".
• async – Next "timeout".
• animationFrame – Next requestAnimationFrame
* if scheduled with zero delay.
39. Animate by velocity
• Move by V units every T time units (e.g. 5 pixels per ms)
• Used for never-ending animations
• Useful in games, loading spinners, etc.
40. Animate by duration
• Move to position X over T time units
• Used for animations occurring over a specific amount of time
• Useful for data visualizations, transitions, etc.
41. Building a useful frames
Observable
A set of frames with frame numbers isn't really useful.
51. Recap: Velocity-based Animations
• Set of time differences on animation frame
• Map those time differences into position differences
• Simplest form of animations
56. What if we could treat all duration-based
animations the same?
• We know it has a start point
• We know it's going to end
• It's all numbers so we can scale it to any form we like
65. Duration observables are always 0 to 1
• No matter how long the duration
• 0 to 1 can also represent the distance travelled (0% to 100%)
• We can create an easing function that transforms the 0 to 1 duration
values to a different 0 to 1 distance value
• github.com/mattdesl/eases
77. Recap: Duration based animations
• Create a duration observable that emits values 0 to 1
• map it to a 0 to 1 percentage of distance if you want to use easing
functions
• Use simple multiplication to manage distance
• Try to use higher-order functions to promote reusability
83. Animations with RxJS
• Observables are sets of values over time
• Animations are sets of values over time
• You need to deal with two forms of time
• Frames
• Duration/Velocity
• Use animationFrame scheduler and interval
• Use higher order functions to keep things reusable
• The rest is basic math and composition