-
-
Notifications
You must be signed in to change notification settings - Fork 865
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
Changes from 5 commits
ff682be
231946d
2880a44
78d7880
504c7a2
72c6894
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { noop } from "../../../../../utils/noop" | ||
import { generateLinearEasing } from "../linear" | ||
|
||
describe("generateLinearEasing", () => { | ||
test("Converts easing function into string of points", () => { | ||
expect(generateLinearEasing(noop, 110)).toEqual( | ||
"linear(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)" | ||
) | ||
expect(generateLinearEasing(() => 0.5, 200)).toEqual( | ||
"linear(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5)" | ||
) | ||
expect(generateLinearEasing(() => 0.5, 0)).toEqual("linear(0.5, 0.5)") | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { EasingFunction } from "../../../../easing/types" | ||
import { progress } from "../../../../utils/progress" | ||
|
||
// Create a linear easing point for every 10 ms | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why 10ms btw? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
const resolution = 10 | ||
|
||
export const generateLinearEasing = ( | ||
easing: EasingFunction, | ||
duration: number // as milliseconds | ||
): string => { | ||
let points = "" | ||
const numPoints = Math.max(Math.round(duration / resolution), 2) | ||
|
||
for (let i = 0; i < numPoints; i++) { | ||
points += easing(progress(0, numPoints - 1, i)) + ", " | ||
} | ||
|
||
return `linear(${points.substring(0, points.length - 2)})` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { memo } from "../../../../utils/memo" | ||
import { supportsFlags } from "./supports-flags" | ||
|
||
export function memoSupports<T extends any>( | ||
callback: () => T, | ||
supportsFlag: keyof typeof supportsFlags | ||
) { | ||
const memoized = memo(callback) | ||
return () => supportsFlags[supportsFlag] ?? memoized() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Add the ability for test suites to manually set support flags | ||
* to better test more environments. | ||
*/ | ||
export const supportsFlags: Record<string, boolean | undefined> = { | ||
linearEasing: undefined, | ||
} |
There was a problem hiding this comment.
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:
You could avoid the casts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ty will implement