Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for linear() easing function #2812

Merged
merged 6 commits into from
Sep 26, 2024
Merged

Support for linear() easing function #2812

merged 6 commits into from
Sep 26, 2024

Conversation

mattgperry
Copy link
Collaborator

Currently, when a WAAPI animation is defined with an easing function:

animate(
  element,
  { opacity: 0 },
  { ease: (p) => p ** p }
) 

Or an easing name not supported by WAAPI:

animate(
  element,
  { opacity: 0 },
  { ease: "anticipate" }
) 

Then Motion will pre-generate keyframes for this animation and run it with linear easing.

This can have poor performance characteristics: https://issues.chromium.org/issues/41491098

Likewise for complex values like clip-path and filter this can have the overhead of number -> string conversion.

This PR adds support for the linear() easing function. So when an easing isn't supported by WAAPI, we generate a linear() easing function instead of pre-generating keyframes.

Comment on lines 179 to 184
ease in unsupportedEasingFunctions
) {
ease =
unsupportedEasingFunctions[
ease as keyof typeof unsupportedEasingFunctions
]
Copy link
Collaborator

@adamseckel adamseckel Sep 26, 2024

Choose a reason for hiding this comment

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

If you made a type guard like:

function isUnsupportedEase(key: string): key is keyof typeof unsupportedEasintFunctions {
  return key in unsupportedEasingFunctions
}

You could avoid the casts.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ty will implement

import { EasingFunction } from "../../../../easing/types"
import { progress } from "../../../../utils/progress"

// Create a linear easing point for every 10 ms
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why 10ms btw?

Copy link
Collaborator Author

@mattgperry mattgperry Sep 26, 2024

Choose a reason for hiding this comment

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

just somewhere between 8 (120fps) and 16 (60fps) that looks quite good without being too accurate (as there's a resolution/performance tradeoff)

@mattgperry mattgperry added the automerge Land this PR label Sep 26, 2024
@mattgperry mattgperry merged commit c14c9da into main Sep 26, 2024
1 check was pending
@mattgperry mattgperry deleted the feature/linear-easing branch September 26, 2024 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Land this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants