Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

AnimationWorklet article #6701

Merged
merged 4 commits into from
Oct 11, 2018
Merged

AnimationWorklet article #6701

merged 4 commits into from
Oct 11, 2018

Conversation

surma
Copy link
Contributor

@surma surma commented Oct 8, 2018

What's changed, or what was fixed?

  • New article on Houdini’s AnimationWorklet

Target Live Date: ASAP

  • This has been reviewed and approved by (@jakearchibald)
  • I have run npm test locally and all tests pass.
  • I have added the appropriate type-something label.
  • I've staged the site and manually verified that my content displays correctly.

CC: @petele @jpmedley @majido @flackr

@petele PTAL, specifically at videos and the code-in-tables situation :)

Copy link
Contributor

@jpmedley jpmedley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surma,

I hope you don't feel too beat up by this. Your writing is always pretty clean of typos and easy to understand. What I hope you notice is that my suggestions are about writing simpler sentences and removing words that don't contribute meaning.

@@ -0,0 +1,493 @@
project_path: /web/_project.yaml
book_path: /web/updates/_book.yaml
description: Houdini’s Animation Worklet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy the snippet to here.

**TL;DR:** Animation Worklet allows you to write imperative animations that run
at the device's native frame rate for that extra buttery jank-free smoothness™,
make your animations more resilient against main thread jank and can be linked
to scroll instead of time. Animation Worklet is in Chrome Canary (behind the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to: 'and are linkable to scrolling instead of time.'


## Another Animation API?

Actually no, it is an extension of what we've already have, and with good
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"we've" to "we".

## Another Animation API?

Actually no, it is an extension of what we've already have, and with good
reason! Bear with me, let's start at the beginning: What do we have? If you want
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete 'Bear with me,'. Change rest to: 'Let's start at the beginning. If you want...'

simple A to B transitions, [CSS
Animations](https://developer.mozilla.org/en-US/docs/Web/CSS/animation) for
potentially cyclical, more complex time-based animations and [Web Animations
API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API) (or
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete 'or'.

thread or run in a separate thread in sync with the compositor.

Note: You should try avoiding "slow" properties at all costs. Limit yourself to
animation `opacity` and `transform` to make sure your animations run smooth even
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'smooth' to 'smoothly'

access to all the new goodness at the same time.

Animation Worklet is in Canary and we are aiming for an Origin Trial with Chrome
69. We are eagerly awaiting you to build great new experiences on the web and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're 6 days from 70 going to stable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct. The more accurate timeline is that it will be in origin trial for 71. Time flies.

access to all the new goodness at the same time.

Animation Worklet is in Canary and we are aiming for an Origin Trial with Chrome
69. We are eagerly awaiting you to build great new experiences on the web and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to 'We are eagerly awaiting your great new web experiences and hearing about what we can improve.'

69. We are eagerly awaiting you to build great new experiences on the web and
telling us about your experiences and what we can improve. There is also a
[polyfill](https://github.com/GoogleChromeLabs/houdini-samples/blob/master/animation-worklet/anim-worklet.js)
that gives you the same API, but doesn't provide the performance isolation.tion.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete 'tion.'

[polyfill](https://github.com/GoogleChromeLabs/houdini-samples/blob/master/animation-worklet/anim-worklet.js)
that gives you the same API, but doesn't provide the performance isolation.tion.

Keep in mind that CSS Transitions and CSS Animations are still very valid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete 'very'

second you start scrolling up, it comes back into view, even if you are half way
down that page. The animation depends not only on scroll position, but also on
your previous scroll direction. It is _stateful_.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting example. I think there are two different things at play in this example:

  1. Direction itself is a state: to compute direction one needs to know the previous scroll position.
  2. You can think of this animation effect as if it is trying to keep the address bar attached to a particular baseline position as page is scrolled. In this model, the baseline position is also state.

Here is how the animation behaves:

  • A) page is scrolling up (swipe down gesture) and address bar is fully shown => do not move the address bar and instead reset the baseline position.
  • B) page is scrolling down (swipe up gesture) or address bar is not fully shown => move the address bar as if it is attached to the baseline position.
  • C) scrolling is finished (i.e., animation) => reset the baseline to top of the document (so next time if user is scrolling up) it starts coming back into view.

Here both A & C are updating the state but there is a subtle difference: only (A) requires the animation itself to be stateful. (C) can be achieved even if state is kept outside the animation.

I think the example you gave 'But the second you start scrolling up, it comes back into view, even if you are half way down the page' is highlighting (C) but I think the case is more clear if we highlight (A) instead. Honestly this is a subtle distinction and maybe not worth trying to clarify.

Also perhaps worth making it clear that computing direction itself requires state (i.e., previous position). Another example of this is "velocity".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are technically correct. I’d like to lead this with a visual example of statefulness, even if the actual state is something else. When people start building this, they will hopefully discover what to do :D

// Some math to make sure that `localTime` is always > 0.
effect.localTime = 2000 + this.direction * (currentTime % 2000);
}
destroy() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have resolved to move away from having a destroy hook. Instead the plan is to have the state as a property the class. However this is not been implemented or specc'ed.

We also plan to have the statefullness be clearly stated via a parent class that should be extended.

So I think the ultimate syntax will be something like.

registerAnimator('randomspin', class MyEffect extends StatefulAnimator {
      constructor(options = {}, state = {}) {
        this.direction = state.direction || (Math.random() > 0.5 ? 1 : -1);
      }
      animate(currentTime, effect) {
        // Some math to make sure that `localTime` is always > 0.
        effect.localTime = 2000 + this.direction * (currentTime % 2000);
      }
      get state() { 
          return {
             direction: this.direction
           };
      }
}

Given however that this is not yet specc'd (my bad 😢 ), perhaps it is fine to talk about destroy hook and then have a note with a link to potential changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol. I will add a note for now and update the article once it’s specced and landed :)

@surma surma force-pushed the animationworklet branch 2 times, most recently from af2590d to 3ce6963 Compare October 10, 2018 22:08
@surma
Copy link
Contributor Author

surma commented Oct 10, 2018

Joe, I 100% appreciate you taking the time to give this monster an in-depth review. Thank you very much.

Waiting for a LGTM from @petele and then this is probably good to go :)

Note: You should avoid "slow" properties at all costs. Limit yourself to
animation `opacity` and `transform` to make sure your animations run smoothly
even on slow devices. Currently, Chrome's implementation doesn't even allow you
to animate slow properties with AnimationWorklet and will throw a warning at
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now support slow properties in AnimationWorklet. They just run in sync with main thread. So they still have to be avoided. Here is a demo of width animating with a worklet.

Also we are actually going to remove the warning from devtools. We feel it is a bit spammy and not appropriate for console. We should instead expose this in devtools elsewhere (e.g., in animations panel). So maybe not mention this.

@WebFundBot
Copy link

Whoops!

There were 2 warnings that will prevent this PR from being merged. Please take a look, and either fix, or provide a justification for why they can't be fixed.

WARNINGS
src/content/en/updates/2018/10/animation-worklet.md#L493 - Consider adding a "was this page helpful?" widget to your page: https://developers.google.com/web/resources/widgets#helpful
src/content/en/updates/2018/10/animation-worklet.md#L221 - Common typo found: 'implicity' Should it be 'implicitly'?

@surma surma merged commit 72182b3 into master Oct 11, 2018
@surma surma deleted the animationworklet branch October 11, 2018 21:07
@WebFundBot
Copy link

🎉 This has been pushed live to https://developers.google.com/web/

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants