Skip to content

Commit f4b09a2

Browse files
committed
[css-animationworklet] Update README to remove old information
1 parent 3c1cd1b commit f4b09a2

File tree

1 file changed

+59
-41
lines changed

1 file changed

+59
-41
lines changed

css-animationworklet/README.md

+59-41
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,13 @@ timeline.
4949
- Efficient Expando ([demo](http://googlechromelabs.github.io/houdini-samples/animation-worklet/expando/), [more info](https://developers.google.com/web/updates/2017/03/performant-expand-and-collapse))
5050
- Compositing growing / shrinking box with border (using 9 patch)
5151

52+
* Sophisticated effects which involves complex coordination across multiple animations.
5253

53-
These usecases are enabled by the current proposed but [additional usecases](principles.md#animation-worklet-vision) are going to be addressed by extension of the API.
54+
55+
56+
These usecases are enabled by the current proposed but [additional usecases](principles.md
57+
#animation-worklet-vision) including input-driven animations are going to be addressed by extension
58+
of the API.
5459

5560
***Note***: Demos work best in the latest Chrome Canary with the experimental
5661
web platform features enabled (`--enable-experimental-web-platform-features`
@@ -78,14 +83,14 @@ Note that worklet animations expose same API surface as other web animations and
7883
created, played, paused, inspected, and generally controlled from main document scope. Here is how
7984
various methods roughly translate:
8085

81-
- cancel(): cancels the animation and the corresponding animator instance is removed.
82-
- play(): starts the animation and the corresponding animator instance may get its `animate` function
83-
called periodically as a result of changes in its timelines.
86+
- `cancel()`: cancels the animation and the corresponding animator instance is removed.
87+
- `play()`: starts the animation and the corresponding animator instance gets constructed and
88+
may get its `animate` function called periodically as a result of changes in its timelines.
8489
- pause(): pauses the animation and the corresponding animator instance no longer receives
8590
`animate` calls.
86-
- finish(): invokes `finish` on the corresponding animator instance.
87-
- reverse() or mutating playbackRate: invokes `playbackRateChanged` on the corresponding
88-
animator instance.
91+
- finish(), reverse() or mutating playbackRate: these affect the currentTime which is seens by
92+
the animator instance. (We are considering possiblity of having a `onPlaybackRateChanged`
93+
callback)
8994

9095
## ScrollTimeline
9196
[ScrollTimeline](https://wicg.github.io/scroll-animations/#scrolltimeline) is a concept introduced in
@@ -106,56 +111,71 @@ flexible scheduling model by making it possible to to set children effect's loca
106111
other words we allow arbitrary start time for child effects. This is something that needs to be
107112
added to level 2 spec.
108113

109-
## Multiple Timelines
114+
## ~~Multiple Timelines~~
110115
Unlike typical animations, worklet animations can be attached to multiple timelines. This is
111116
necessary to implement key usecases where the effect needs to smoothly animate across different
112117
timelines (e.g., scroll and wall clock).
113118

114-
### Primary Timeline
115-
The first timeline is considered the *primary timeline*. The only purpose of the primary timeline is
116-
to make integration with existing web animation machinery easier, in particular the primary timeline
117-
time will be used anywhere the web animation API needs to expose a time value, for example in
118-
[event timeline time](https://w3c.github.io/web-animations/level-2/#event-timeline-time), or
119-
[event current time](https://w3c.github.io/web-animations/level-2/#event-current-time).
119+
**NOTE**: We have decided to drop this piece in favor of alternative ideas. Most recent
120+
[promising idea](https://docs.google.com/document/d/1byDy6IZqvaci-FQoiRzkeAmTSVCyMF5UuaSeGJRHpJk/edit#heading=h.dc7o68szgx2r)
121+
revolves around allowing worklet and workers to receive input events directly. (here are some
122+
earlier alternative design: [1](https://docs.google.com/document/d/1-APjTs9fn4-E7pFeFSfiWV8tYitO84VpmKuNpE-25Qk/edit), [2](https://github.com/w3c/csswg-drafts/issues/2493), [3](https://github.com/w3c/csswg-drafts/issues/2493#issuecomment-422109535)
123+
124+
125+
## Statefull and Statelss Animators
120126

121127

122-
**TODO**: We are considering API designs that can make it possible for an animation to observe multiple
123-
timelines but only gets activated on a (dynamic) subset of them. This ensures we can be more
124-
efficient when updating the animation.
128+
Sometimes animation effects require maintaining internal state (e.g., when animation needs to depend
129+
on velocity). Such animators have to explicitly declare their statefulness but by inheritting from
130+
`StatefulAnimator` superclass.
125131

126-
## Animator Migration
127132
The animators are not guaranteed to run in the same global scope (or underlying thread) for their
128-
lifetime duration. For example, a user agents is free to initially run the animator on main thread
129-
but later decide to migrate it off main thread to get certain performance optimizations. To allow
130-
worklet animators to keep their state across migrations, the API provides the following lifetime
131-
hooks:
133+
lifetime duration. For example user agents are free to initially run the animator on main thread
134+
but later decide to migrate it off main thread to get certain performance optimizations or to tear
135+
down scopes to save resources.
136+
137+
138+
Animation Worklet helps stateful animators to maintain their state across such migration events.
139+
This is done through a state() function which is called and animator exposes its state. Here is
140+
an example:
132141

133142
```js
134143
// in document scope
135-
new WorkletAnimation('animation-with-local-state', [], [], {value: 1});
144+
new WorkletAnimation('animation-with-local-state', keyframes);
136145
```
137146

138-
139147
```js
140-
registerAnimator('animation-with-local-state', class {
141-
constructor(options, state) {
142-
// |options| may be either:
143-
// - The user provided options bag passed into the WorkletAnimation constructor on first initialization i.e, {value: 1}.
144-
// - The object returned by |destroy| after each migration i.e. {value: 42}.
145-
this.options_ = options;
146-
this.state_ = state || {value: Math.random()};
148+
registerAnimator('animation-with-local-state', class FoorAnimator extends StatefulAnimator {
149+
constructor(options, state = {velocity: 0, acceleration: 0}) {
150+
// state is either undefined (first time) or the state after an animator is migrated across
151+
// global scope.
152+
this.velocity = state.velocity;
153+
this.acceleration = state.acceleration;
147154
}
148155

149-
animate(timelines, effect) {
150-
this.state_.value += 0.1;
151-
effect.localTime = this.state_.value;
156+
animate(time, effect) {
157+
if (this.lastTime) {
158+
this.velocity = time - this.prevTime;
159+
this.acceleration = this.velocity - this.prevVelocity;
160+
}
161+
this.prevTime = time;
162+
this.prevVelocity = velocity;
163+
164+
effect.localTime = curve(velocity, acceleration, currentTime);
165+
}
166+
167+
state() {
168+
// Invoked before any migration attempts. The returned object must be structure clonable
169+
// and will be passed to constructor to help animator restore its state after migration to the
170+
// new scope.
171+
return {
172+
this.velocity,
173+
this.acceleration
174+
};
152175
}
153176

154-
destroy() {
155-
// Invoked before each migration attempts.
156-
// The returned object must be structure clonable and will be passed to constructor to help
157-
// animator restore its state after migration to the new scope.
158-
return this.state_;
177+
curve(velocity, accerlation, t) {
178+
return /* compute some physical movement curve */;
159179
}
160180
});
161181
```
@@ -386,8 +406,6 @@ partial interface AnimationEffectReadOnly {
386406
```
387407
388408
389-
390-
391409
# Specification
392410
The [draft specification](https://drafts.css-houdini.org/css-animationworklet) is
393411
the most recent version.

0 commit comments

Comments
 (0)