-
Notifications
You must be signed in to change notification settings - Fork 808
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2259 from framer/feature/scroll-animate
Hardware accelerated scroll animations
- Loading branch information
Showing
12 changed files
with
198 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { scroll, animate } from "framer-motion" | ||
import * as React from "react" | ||
import { useEffect } from "react" | ||
|
||
export const App = () => { | ||
useEffect(() => { | ||
/** | ||
* Animate both background-color (WAAPI-driven) and color (sync) | ||
*/ | ||
return scroll( | ||
animate( | ||
"#color", | ||
{ | ||
backgroundColor: ["#fff", "#000"], | ||
color: ["#000", "#fff"], | ||
transform: ["none", "translateX(100px)"], | ||
}, | ||
{ ease: "linear" } | ||
) | ||
) | ||
}, []) | ||
|
||
return ( | ||
<> | ||
<div style={{ ...spacer, backgroundColor: "red" }} /> | ||
<div style={{ ...spacer, backgroundColor: "green" }} /> | ||
<div style={{ ...spacer, backgroundColor: "blue" }} /> | ||
<div style={{ ...spacer, backgroundColor: "yellow" }} /> | ||
<div id="color" style={progressStyle}> | ||
A | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
const spacer = { | ||
height: "100vh", | ||
} | ||
|
||
const progressStyle: React.CSSProperties = { | ||
position: "fixed", | ||
top: 0, | ||
left: 0, | ||
width: 100, | ||
height: 100, | ||
display: "flex", | ||
justifyContent: "center", | ||
alignItems: "center", | ||
fontSize: 80, | ||
lineHeight: 80, | ||
fontWeight: "bold", | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { cancelFrame, frame } from "../../../frameloop" | ||
|
||
type Update = (progress: number) => void | ||
|
||
export interface ProgressTimeline { | ||
currentTime: null | { value: number } | ||
|
||
cancel?: VoidFunction | ||
} | ||
|
||
export function observeTimeline(update: Update, timeline: ProgressTimeline) { | ||
let prevProgress: number | ||
|
||
const onFrame = () => { | ||
const { currentTime } = timeline | ||
const percentage = currentTime === null ? 0 : currentTime.value | ||
const progress = percentage / 100 | ||
|
||
if (prevProgress !== progress) { | ||
update(progress) | ||
} | ||
|
||
prevProgress = progress | ||
} | ||
|
||
frame.update(onFrame, true) | ||
|
||
return () => cancelFrame(onFrame) | ||
} |
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,5 @@ | ||
import { memo } from "../../../utils/memo" | ||
|
||
export const supportsScrollTimeline = memo( | ||
() => window.ScrollTimeline !== undefined | ||
) |