SlideShare a Scribd company logo
Advanced RxJS
Animations With Complex State
Don't mess with Switzerland
Ben Lesh
Software Engineer at Google
RxJS 5 Lead
Twitter:
@benlesh
GitHub:
github.com/benlesh
What is RxJS?
"Lodash for events"
What is RxJS?
Observables and "operators"
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
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
Observables Are Sets
(This is why they’re powerful)
Operators transform Sets
into new Sets
(They map, filter, combine, flatten, etc)
To "Think Reactively" is to think
in terms of transforming Sets
Animations
I always mention "animation" as
a big source of async
(but I never talk about it)
Different methods of animation
• CSS
• JavaScript
• Raw JavaScript
• Web Animation API (Some browsers)
• JQuery
• D3
• Greensock
• … so many more
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”
Animations are sets of puppets
over time!
Animations are sets of puppets
over time!
Animations are sets of positions
over time!
Moving
x = 0 x = 1 x = 2 x = 3
Observable.of(0, 1, 2, 3)
Rotating
r = 0 r = 90 r = 180 r = 270
Observable.of(0, 90, 180, 270)
Scale
width = 1 width = 2 width = 3 width = 4
Observable.of(1, 2, 3, 4)
Scale
width = 1 width = 2 width = 3 width = 4
height = 1 height = 2 height = 3 height = 4
Observable.of([1, 1], [2, 2], [3, 3], [4, 4])
Scale
width = 1 width = 2 width = 3 width = 4
height = 1 height = 2 height = 3 height = 4
Observable.of(1, 2, 3, 4);
Moving
x = 0 x = 1 x = 2 x = 3
y = 0 y = 1 y = 2 y = 3
Observable.of(0, 1, 2, 3);
We need values over time
(This is where it gets a little tricky)
Elements of time in animations
• Frame rate
• Duration or Velocity
Frame
A moment in time at which to adjust position and render
requestAnimationFrame
RxJS 5 has a Scheduler for that
What is a Scheduler in Rx?
Schedulers control timing around when events occur:
• next
• error
• complete
• subscription
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);
What is a Scheduler in Rx?
Used with special operators for control over when existing observables'
events occur:
• someObservable$.observeOn(scheduler);
• someObservable$.subscribeOn(scheduler);
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.
Endless stream of frames for animations
Endless stream of frames for animations
http://jsbin.com/libihaciwu/1/edit?js,output
requestAnimationFrame is
non-deterministic
(That means we don't know when it'll fire)
Velocity or Duration
(We should probably control this a little more)
Velocity is very simple math
velocity = distance / time
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.
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.
Building a useful frames
Observable
A set of frames with frame numbers isn't really useful.
Get an observable of time passed each frame
Take our observable of frame numbers
… and map it into changes in time!
Oops, we need to get `start` on subscribe
RxJS Trick: Wrap it in a `defer`
Higher-order function to provide scheduler
Now let's apply that to a velocity
Higher-order function to make it more useful
http://jsbin.com/pimiliqabi/1/edit?js,output
Recap: Velocity-based Animations
• Set of time differences on animation frame
• Map those time differences into position differences
• Simplest form of animations
Duration-based Animations
Generally more useful in apps
We could just take what we had here…
… and just use takeWhile
…but maybe that isn't the
best solution
(We can do better)
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
Let's treat them as
percentages
(From 0 -> 1)
Build a duration observable
We can pass through the scheduler
(just in case)
Giving us a range of values between 0 and 1
0… 0.1 ... 0.22 ... 0.43 ... 0.56 ... 0.67 ... 0.71 ... 0.77 ... 0.81 ... 0.88 ... 0.9 ... 0.97 ... 1
Moving over a distance
is simple multiplication
Make distance a higher-order function
http://jsbin.com/favipemefu/1/edit?js,output
Adding Easing
(bounce effects, elastic effects, etc)
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
elasticOut ease function
Add easing before distance is mapped!
http://jsbin.com/nojeboqixi/1/edit?js,output
Making animations more reusable
• Move rendering side effects to `do` block
• Allow passing of duration with higher-order function
Making animations more reusable
Making animations more reusable
http://jsbin.com/kumugizivi/1/edit?js,output
Even more reusable
Even more reusable
Advanced RxJS: Animations
Advanced RxJS: Animations
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
Animating state changes
http://jsbin.com/vejazipomo/1/edit?js,output
Using what we've built to "tween"
Using what we've built to "tween"
http://jsbin.com/curayibawa/1/edit?js,output
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
Thank you!
@benlesh
rxworkshop.com

More Related Content

PPTX
BST CATARACT FK UNPAD 2013
PPSX
HHS in Diabetic Person
PDF
Taşiaritmiler ve bradiaritmiler (fazlası için www.tipfakultesi.org )
PPTX
perioperatif anes aul.pptx
PDF
Gemalto MPCOS Version 0.1
PPTX
Visual pathway.pptx
PPTX
Pemeriksaan Palpebra.pptx
PPTX
RxJS Animations Talk - 2017
BST CATARACT FK UNPAD 2013
HHS in Diabetic Person
Taşiaritmiler ve bradiaritmiler (fazlası için www.tipfakultesi.org )
perioperatif anes aul.pptx
Gemalto MPCOS Version 0.1
Visual pathway.pptx
Pemeriksaan Palpebra.pptx
RxJS Animations Talk - 2017

Similar to Advanced RxJS: Animations (20)

KEY
Akka london scala_user_group
PPT
Google tools for webmasters
PPT
Reactive programming with examples
PDF
Introduction to Computer Animation using Computer Graphics Techniques
PDF
My internship presentation at WSO2
PDF
Choosing your animation adventure - Ffronteers Conf 2017
KEY
DjangoCon 2010 Scaling Disqus
PPTX
RxJS In-Depth - AngularConnect 2015
PDF
Monitoring your Python with Prometheus (Python Ireland April 2015)
PDF
Motion design in FIori
PDF
Building Sexy Real-Time Analytics Systems - Erlang Factory NYC / Toronto 2013
PDF
The hitchhiker’s guide to Prometheus
PDF
Prometheus monitoring
PDF
The hitchhiker’s guide to Prometheus
PDF
Springone2gx 2014 Reactive Streams and Reactor
PPTX
Practical LLM inference in modern Java.pptx
PPTX
Practical LLM inference in modern Java.pptx
PDF
RxJava@Android
PDF
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
PPTX
Developing for Leap Motion
Akka london scala_user_group
Google tools for webmasters
Reactive programming with examples
Introduction to Computer Animation using Computer Graphics Techniques
My internship presentation at WSO2
Choosing your animation adventure - Ffronteers Conf 2017
DjangoCon 2010 Scaling Disqus
RxJS In-Depth - AngularConnect 2015
Monitoring your Python with Prometheus (Python Ireland April 2015)
Motion design in FIori
Building Sexy Real-Time Analytics Systems - Erlang Factory NYC / Toronto 2013
The hitchhiker’s guide to Prometheus
Prometheus monitoring
The hitchhiker’s guide to Prometheus
Springone2gx 2014 Reactive Streams and Reactor
Practical LLM inference in modern Java.pptx
Practical LLM inference in modern Java.pptx
RxJava@Android
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
Developing for Leap Motion
Ad

Recently uploaded (20)

PPTX
Introduction to Artificial Intelligence
PDF
top salesforce developer skills in 2025.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
history of c programming in notes for students .pptx
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Transform Your Business with a Software ERP System
PDF
System and Network Administraation Chapter 3
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
System and Network Administration Chapter 2
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
L1 - Introduction to python Backend.pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Introduction to Artificial Intelligence
top salesforce developer skills in 2025.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Odoo Companies in India – Driving Business Transformation.pdf
history of c programming in notes for students .pptx
Understanding Forklifts - TECH EHS Solution
Transform Your Business with a Software ERP System
System and Network Administraation Chapter 3
Odoo POS Development Services by CandidRoot Solutions
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PTS Company Brochure 2025 (1).pdf.......
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
System and Network Administration Chapter 2
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
2025 Textile ERP Trends: SAP, Odoo & Oracle
L1 - Introduction to python Backend.pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Ad

Advanced RxJS: Animations

Editor's Notes

  • #52: github.com/mattdesl/eases