@@ -155,32 +155,23 @@ and each one is associated with a {{WorkletAnimation}} instance. An animator can
155
155
instantiated by construction of a {{WorkletAnimation}} .
156
156
157
157
158
- Two animators types are supported: {{StatelessAnimator}} and {{StatefulAnimator}} each
158
+ Two animators types are supported: <a>Stateless Animator</a> and <a>Stateful Animator</a> each
159
159
providing a different state management strategy.
160
160
161
161
162
- StatelessAnimator Interface {#stateless-animator-desc}
162
+ Stateless Animator {#stateless-animator-desc}
163
163
---------------------------
164
164
165
- This interface represents a stateless animator. This type of animator does not depend on any local
166
- state either stored on the instance or global scope. Effectively, the animate function of an
167
- {{StatelessAnimator}} can be treated as a pure function with the expectation that given the same
168
- input, it produces the same output.
169
-
170
-
171
-
172
- <xmp class='idl'>
173
- [Exposed=AnimationWorklet, Global=AnimationWorklet, Constructor (optional any options)]
174
- interface StatelessAnimator {
175
- };
176
- </xmp>
177
-
165
+ A <dfn>Stateless Animator</dfn> is a type of animator does not depend on any local state either
166
+ stored on the instance or global scope. Effectively, the animate function of an <a>Stateless
167
+ Animator</a> can be treated as a pure function with the expectation that given the same input, it
168
+ produces the same output.
178
169
179
170
180
171
<div class='note'>
181
- This is how the class should look.
172
+ This is how an stateless animator class should look.
182
173
<pre class='lang-javascript'>
183
- class FooAnimator extends StatelessAnimator {
174
+ class FooAnimator {
184
175
constructor(options) {
185
176
// Called when a new animator is instantiated.
186
177
}
@@ -193,14 +184,14 @@ interface StatelessAnimator {
193
184
194
185
Note: The statelessness allows an animation worklet to perform optimization such as producing
195
186
multiple animation frames in parallel and performing very cheap teardown and setup. Using
196
- StatelessAnimator is highly recommended to enable such optimizations.
187
+ <a>Stateless Animator</a> is highly recommended to enable such optimizations.
197
188
198
- StatefulAnimator Interface {#stateful-animator-desc}
189
+ Stateful Animator {#stateful-animator-desc}
199
190
---------------------------
200
191
201
- This interface represents a stateful animator. This animator can have local state and animation
202
- worklet guarantees that it maintains this state as long as the stateful animator fulfills the
203
- contract required by this interface as described following.
192
+ A <dfn>Stateful Animator</dfn> is a type of animator that can have local state and animation worklet
193
+ guarantees that it maintains this state as long as the stateful animator fulfills the contract
194
+ required by its interface as described following.
204
195
205
196
206
197
<a>Animation worklet</a> maintains a set of {{WorkletGlobalScope}} s which may exist across different
@@ -210,29 +201,20 @@ effect is mutable only in a certain thread). Animation worklet guarantees that a
210
201
instance's state is maintained even if the instance is respawned in a different global scope.
211
202
212
203
The basic mechanism for maintaining the state is that the animation worklet snapshots the local
213
- state that is exposed via the {{StatefulAnimator/ state}} function and then reifies it so that it can
214
- be passed into the constructor when the animator instance is respawned at a later time in a
215
- potentially different global scope. The <a>migrate an animator instance</a> algorithm specifies this
216
- process in details.
204
+ state that is exposed via the <a> state function</a> and then reifies it so that it can be passed
205
+ into the constructor when the animator instance is respawned at a later time in a potentially
206
+ different global scope. The <a>migrate an animator instance</a> algorithm specifies this process in
207
+ details.
217
208
218
209
A user-defined stateful animator is expected to fulfill the required contract which is that its
219
210
state function returns an object representing its state that can be serialized using structured
220
211
serialized algorithm and that it can also recreate its state given that same object passed to
221
212
its constructor.
222
213
223
- <xmp class='idl'>
224
- [Exposed=AnimationWorklet, Global=AnimationWorklet,
225
- Constructor (optional any options, optional any state)]
226
- interface StatefulAnimator {
227
- any state();
228
- };
229
- </xmp>
230
-
231
-
232
214
<div class='note'>
233
- This is how the class should look.
215
+ This is how a stateful animator class should look.
234
216
<pre class='lang-javascript'>
235
- class BarAnimator extends StatefulAnimator {
217
+ class BarAnimator {
236
218
constructor(options, state) {
237
219
// Called when a new animator is instantiated (either first time or after being respawned).
238
220
this.currentVelocity = state ? state.velocity : 0;
@@ -264,8 +246,13 @@ animation as needed by {{AnimationWorkletGlobalScope}}. It consists of:
264
246
265
247
- An <dfn>animate function</dfn> which is a <a>Function</a> <a>callback function</a> type.
266
248
267
- - A <dfn>stateful flag</dfn>
249
+ - A <dfn>state function</dfn> which is a <a>Function</a> <a>callback function</a> type.
250
+
251
+ - A <dfn>stateful flag</dfn> .
252
+
268
253
254
+ A <dfn>stateful animator definition</dfn> is an <a>animator definition</a> whose <a>stateful
255
+ flag</a> is <b> true</b> .
269
256
270
257
271
258
Registering an Animator Definition {#registering-animator-definition}
@@ -285,6 +272,9 @@ callback AnimatorInstanceConstructor = any (any options, optional any state);
285
272
286
273
</xmp>
287
274
275
+ Note that to register a <a>stateful animator definition</a> it is simply enough for the registered
276
+ class to have a state function.
277
+
288
278
<div algorithm="register-animator">
289
279
290
280
When the <dfn method for=AnimationWorkletGlobalScope>registerAnimator(|name|, |animatorCtor|)</dfn>
@@ -304,24 +294,27 @@ following steps:
304
294
4. Let |prototype| be the result of <a>Get</a> (|animatorCtor|, "prototype").
305
295
306
296
307
- 5. If <a>SameValue</a> (|prototype|, {{StatelessAnimator}} ) is <b> true</b> set |stateful| to be
308
- <b> false</b> , otherwise if <a>SameValue</a> (|prototype|, {{StatefulAnimator}} ) is
309
- <b> true</b> set |stateful| to be <b> true</b> , otherwise <a>throw</a> a <a>TypeError</a> and
310
- abort all these steps.
311
-
312
- 6. Let |animateValue| be the result of <a>Get</a> (|prototype|, "animate").
297
+ 5. Let |animateFuncValue| be the result of <a>Get</a> (|prototype|, "animate").
313
298
314
- 7 . Let |animate | be the result of <a>converting</a> |animateValue | to the <a>Function</a>
299
+ 6 . Let |animateFunc | be the result of <a>converting</a> |animateFuncValue | to the <a>Function</a>
315
300
<a>callback function</a> type. If an exception is thrown, rethrow the exception and abort
316
301
all these steps.
317
302
318
- 8. Let |definition| be a new <a>animator definition</a> with:
303
+ 7. Let |stateFuncValue| be the result of <a>Get</a> (|prototype|, "state").
304
+
305
+ 8. Let |stateFunc| be the result of <a>converting</a> |stateFuncValue| to the <a>Function</a>
306
+ <a>callback function</a> type, If an exception is thrown, set |stateful| to be <b> false</b> ,
307
+ otherwise set |stateful| to be <b> true</b> and |stateFunc| to be undefined.
308
+
309
+ 9. Let |definition| be a new <a>animator definition</a> with:
319
310
320
311
- <a>animator name</a> being |name|
321
312
322
313
- <a>class constructor</a> being |animatorCtor|
323
314
324
- - <a>animate function</a> being |animate|
315
+ - <a>animate function</a> being |animateFunc|
316
+
317
+ - <a>state function</a> being |stateFunc|
325
318
326
319
- <a>stateful flag</a> being |stateful|
327
320
@@ -352,7 +345,8 @@ and owns the instance specific state such as animation effect and timeline. It c
352
345
- An <dfn>animator serialized options</dfn> which is a serializable object.
353
346
354
347
A <dfn>stateful animator instance</dfn> is an <a>animator instance</a> whose corresponding
355
- <a>animator definition</a> 's <a>stateful flag</a> is <b> true</b> .
348
+ definition is a <a>stateful animator definition</a> .
349
+
356
350
357
351
358
352
Creating an Animator Instance {#creating-animator-instance}
@@ -852,7 +846,7 @@ animation.play();
852
846
853
847
<xmp class='lang-javascript'>
854
848
// Inside AnimationWorkletGlobalScope.
855
- registerAnimator('twitter-header' , class HeaderAnimator extends StatelessAnimator {
849
+ registerAnimator('twitter-header' , class HeaderAnimator {
856
850
constructor(options) {
857
851
this.timing_ = new CubicBezier('ease-out' );
858
852
}
@@ -920,7 +914,7 @@ fastParallax.play();
920
914
921
915
<xmp class='lang-javascript'>
922
916
// Inside AnimationWorkletGlobalScope.
923
- registerAnimator('parallax' , class ParallaxAnimator extends StatelessAnimator {
917
+ registerAnimator('parallax' , class ParallaxAnimator {
924
918
constructor(options) {
925
919
this.rate_ = options.rate;
926
920
}
0 commit comments