|
| 1 | +# Animation Worklet Design Principles and Goals |
| 2 | +***for rich interactive effects on the Web Platform*** |
| 3 | + |
| 4 | + |
| 5 | +## Problem and Motivation |
| 6 | + |
| 7 | +**Fact**: Fact: It is difficult to build smooth rich interactive effects on the web. |
| 8 | + |
| 9 | +**Why do we care?** |
| 10 | +Silky smooth rich interactive user interfaces are now a [basic user expectation][performance] on |
| 11 | +modern computing platforms. For web platform to remain competitive it should be capable of high |
| 12 | +fidelity, smooth, responsive UI. |
| 13 | + |
| 14 | + |
| 15 | +**Where is the difficulty?** |
| 16 | +Three key aspects of rich interactive effects are: smoothness, responsiveness to input (a.k.a. R & A |
| 17 | +of [RAILS model][rails]) and their rich interaction model. |
| 18 | + |
| 19 | +The two main methods for creating animations on the web fall short in at least one of these aspects: |
| 20 | + |
| 21 | +- CSS (Web) Animations: Aimed at supporting stateless declarative time-driven effects. The |
| 22 | + expressiveness is sufficient for common time-based effects. The resulting animation can be smooth |
| 23 | + <sup>[1](#footnote1)</sup>. However it's unclear how this model can be scaled to handle the |
| 24 | + multi-dimensional inputs, conditional values, and statefulness required by the use cases below. |
| 25 | +- requestAnimationFrame: Aimed at creating scripted animation effects. It can support rich |
| 26 | + interaction models but it is difficult to make smooth or responsive. This expressiveness of |
| 27 | + Javascript coupled with access to all input methods, application state and dom makes this API |
| 28 | + capable of building rich interactive effects. However these can only run on main thread alongside |
| 29 | + all other scripts<sup>[2](#footnote2)</sup> which severely hampers their responsiveness and |
| 30 | + smoothness. Chrome [studies](https://tdresser.github.io/input-latency-deep-reports/) have shown that script is the main culprit to user responsiveness issues. |
| 31 | + |
| 32 | +Animation Worklet aims to help bridge the gap between these two. |
| 33 | + |
| 34 | +## Animation Worklet Vision |
| 35 | + |
| 36 | +[Animation Worklet][specification] aims to rectify this shortcomings by enabling animations that can |
| 37 | +be: |
| 38 | + |
| 39 | +* rich (imperative, stateful) |
| 40 | +* fast-by-default (isolated from main thread) |
| 41 | +* respond to rich input e.g., touch, gesture, scroll. |
| 42 | + |
| 43 | +Animation Worklet is a primitive in the [extensible web](https://extensiblewebmanifesto.org/) spirit. |
| 44 | +It exposes browser's fast path to applications in a way that it was never before and reduces browser |
| 45 | +magic. |
| 46 | + |
| 47 | +Examples of rich interactive effects that are (or will be made) possible with Animation Worklet: |
| 48 | + |
| 49 | + |
| 50 | +* Scroll driven effects: |
| 51 | + * [Hidey-bar](https://googlechromelabs.github.io/houdini-samples/animation-worklet/twitter-header/): animation depends on both time and scroll input. |
| 52 | + * [Parallax](https://googlechromelabs.github.io/houdini-samples/animation-worklet/parallax-scrolling/): Simplest scroll-drive effect. |
| 53 | + * [Custom paginated slider](http://aw-playground.glitch.me/amp-scroller.html). |
| 54 | + * Pull-to-refresh: animation depends on both touch and time inputs. |
| 55 | + * Custom scrollbars. |
| 56 | + * [More examples](https://github.com/w3c/css-houdini-drafts/blob/master/scroll-customization-api/UseCases.md) of scroll-driven effects. |
| 57 | +* Gesture driven effects: |
| 58 | + * [Image manipulator](https://github.com/w3c/csswg-drafts/issues/2493#issuecomment-422153926) that scales, rotates etc. |
| 59 | + * Swipe to dismiss. |
| 60 | + * Drag-N-Drop. |
| 61 | + * Tiled panning e.g., Google maps. |
| 62 | +* Stateful script driven effects: |
| 63 | + * [Spring-based emulations](https://googlechromelabs.github.io/houdini-samples/animation-worklet/spring-timing/). |
| 64 | + * [Spring-Sticky effect](http://googlechromelabs.github.io/houdini-samples/animation-worklet/spring-sticky/). |
| 65 | + * Touch-driven physical environments. |
| 66 | + * [Expando](http://googlechromelabs.github.io/houdini-samples/animation-worklet/expando/): Procedural animations with multiple elements. |
| 67 | +* Animated scroll offsets: |
| 68 | + * Having multiple scrollers scroll in sync e.g. diff viewer keeping old/new in sync when you |
| 69 | + scroll either ([demo](https://googlechromelabs.github.io/houdini-samples/animation-worklet/sync-scroller/)) |
| 70 | + * Custom smooth scroll animations (e.g., physic based fling curves) |
| 71 | + |
| 72 | + |
| 73 | +## First Principle - Richness |
| 74 | + |
| 75 | +Animation Worklet enables developers to create custom animations by providing an `animate` function |
| 76 | +that runs inside animation worklet global scope. The animation logic can take advantage of the full |
| 77 | +expressive power of JavaScript, maintain local state, modify and coordinated across many elements. |
| 78 | +This new extension point in browser animation system enables richer effects that go well beyond what |
| 79 | +can be achieved today with Web Animations and closer to what is possible with requestAnimationFrame. |
| 80 | + |
| 81 | +**Further explorations in this direction:** Allow richer access to scrolling machinery (e.g., scroll |
| 82 | +customization), custom paint worklets, and outputs beyond existing KeyframeEffect interface. |
| 83 | + |
| 84 | + |
| 85 | +## Second Principle - Performance |
| 86 | + |
| 87 | +Animation Worklet is designed to be thread agnostic. In particular, it can run off main thread |
| 88 | +keeping its performance isolated from main thread (also reducing main thread load). |
| 89 | + |
| 90 | +Animation Worklet API encourages the developers to isolate their critical UI work inside limited |
| 91 | +worklet scope with well-specified input and output. This allows user-agent to make much better |
| 92 | +scheduling decision about this work and in particular, be much better at maintaining a strict |
| 93 | +frame-budget to successfully run these animations on its fast path. Presently the above performance |
| 94 | +guarantees are only accessible to a limited set of declarative time-based effects. |
| 95 | + |
| 96 | +**Further explorations in this direction**: Introduce more sophisticated per-animation scheduling |
| 97 | +where a slow animation may run at slower frame-rate without affecting other well-behaved animations, |
| 98 | +experiment with translating animation code to native code or even GL shaders moving the computation |
| 99 | +to GPU for even stronger performance guarantees! |
| 100 | + |
| 101 | + |
| 102 | +## Third Principle - Interactivity |
| 103 | + |
| 104 | +Animation Worklet is designed to enable support for animations whose input goes beyond just time, a |
| 105 | +single-dimensional variable. |
| 106 | + |
| 107 | +Web animation timing model is stateless and driven by a single dimensional variable, time. |
| 108 | + |
| 109 | +Although this model works well for declarative time-based animation, it falls short when it comes to |
| 110 | +interactive input-driven effects that are inherently stateful. While it is possible to map simple |
| 111 | +forms of input (e.g., [single dimensional scroll](https://wicg.github.io/scroll-animations/#intro)) |
| 112 | +into time, it is much more difficult (almost impossible) to do so for multi-dimensional stateful |
| 113 | +input such as multi-touch and gesture input. |
| 114 | + |
| 115 | +Animation Worklet has the necessary expressive power and richness to easily accommodate the full |
| 116 | +richness of multi-dimensional input such as touch, gesture, scroll etc. For example it is trivial to |
| 117 | +react to scroll phase change, pointer state change, addition/removal of new pointer or state, |
| 118 | +calculate pointer velocity, acceleration and other computed values inside an animation worklet. |
| 119 | + |
| 120 | +**Further explorations in this direction:** Expose pointer and gesture as input to animation |
| 121 | +([current proposal](https://github.com/w3c/csswg-drafts/issues/2493#issuecomment-422109535)) |
| 122 | + |
| 123 | + |
| 124 | +# Appendix |
| 125 | + |
| 126 | + |
| 127 | +## Footnotes |
| 128 | + |
| 129 | +* <a name="footnote1">1</a>: If authors limit themselves to cheap-to-update properties. In Chrome |
| 130 | + these are composited properties e.g., transform, opacity, filter but other engines may have a |
| 131 | + slightly difference subset. |
| 132 | + |
| 133 | + |
| 134 | +[performance]: https://paul.kinlan.me/what-news-readers-want/ |
| 135 | +[rails]: https://developers.google.com/web/fundamentals/performance/rail#goals-and-guidelines |
| 136 | +[specification]: https://github.com/w3c/css-houdini-drafts/tree/master/css-animationworklet |
0 commit comments