-
-
Notifications
You must be signed in to change notification settings - Fork 865
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for
linear()
easing function (#2812)
* Adding support for linear easing * Fixing tests * Updating linear() * Tweaking linear easing * Make minimum number of linear points * Adding guard
- Loading branch information
1 parent
bc91591
commit c14c9da
Showing
10 changed files
with
255 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/framer-motion/src/animation/animators/waapi/utils/__tests__/linear.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | ||
}) | ||
}) |
19 changes: 19 additions & 0 deletions
19
packages/framer-motion/src/animation/animators/waapi/utils/linear.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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)})` | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/framer-motion/src/animation/animators/waapi/utils/memo-supports.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/framer-motion/src/animation/animators/waapi/utils/supports-flags.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
Oops, something went wrong.