-
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 #2440 from framer/feature/optimised-appear-v2
Optimised appear animations V2
- Loading branch information
Showing
14 changed files
with
331 additions
and
92 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,135 @@ | ||
<html> | ||
<head> | ||
<style> | ||
body { | ||
padding: 100px; | ||
margin: 0; | ||
} | ||
|
||
#box { | ||
width: 100px; | ||
height: 100px; | ||
background-color: #0077ff; | ||
} | ||
|
||
[data-layout-correct="false"] { | ||
background: #dd1144 !important; | ||
opacity: 1 !important; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script src="../../node_modules/react/umd/react.development.js"></script> | ||
<script src="../../node_modules/react-dom/umd/react-dom.development.js"></script> | ||
<script src="../../node_modules/react-dom/umd/react-dom-server-legacy.browser.development.js"></script> | ||
<script src="../../packages/framer-motion/dist/framer-motion.dev.js"></script> | ||
<script src="../projection/script-assert.js"></script> | ||
|
||
<script> | ||
const { | ||
motion, | ||
animateStyle, | ||
animate, | ||
startOptimizedAppearAnimation, | ||
optimizedAppearDataAttribute, | ||
motionValue, | ||
frame, | ||
} = window.Motion | ||
const { matchViewportBox } = window.Assert | ||
const root = document.getElementById("root") | ||
|
||
const duration = 2 | ||
const x = motionValue(0) | ||
|
||
let isFirstFrame = true | ||
|
||
// This is the tree to be rendered "server" and client-side. | ||
const Component = React.createElement(motion.div, { | ||
id: "box", | ||
initial: { x: 0, opacity: 0 }, | ||
animate: { x: 100, opacity: 1 }, | ||
transition: { duration, ease: "linear" }, | ||
style: { x }, | ||
/** | ||
* On animation start, check the values we expect to see here | ||
*/ | ||
onAnimationStart: () => { | ||
const box = document.getElementById("box") | ||
|
||
box.style.backgroundColor = "green" | ||
|
||
setTimeout(() => { | ||
/** | ||
* This animation interrupts the optimised animation. Notably, we are animating | ||
* x in the optimised transform animation and only scale here. This ensures | ||
* that any transform can force the cancellation of the optimised animation on transform, | ||
* not just those involved in the original animation. | ||
*/ | ||
animate( | ||
box, | ||
{ scale: 2, opacity: 0.1 }, | ||
{ duration: 0.3, ease: "linear" } | ||
).then(() => { | ||
if (getComputedStyle(box).opacity !== "0.1") { | ||
showError( | ||
document.getElementById("box"), | ||
`opacity animation didn't interrupt optimised animation. Opacity was ${ | ||
getComputedStyle(box).opacity | ||
} instead of 0.1.` | ||
) | ||
} | ||
|
||
const { width, left } = box.getBoundingClientRect() | ||
if (width !== 200) { | ||
showError( | ||
document.getElementById("box"), | ||
`scale animation didn't interrupt optimised animation. Width was ${width}px instead of 200px.` | ||
) | ||
} | ||
|
||
if (left <= 100) { | ||
showError( | ||
document.getElementById("box"), | ||
`scale animation incorrectly interrupted optimised animation. Left was ${left}px instead of 100px.` | ||
) | ||
} | ||
}) | ||
}, 100) | ||
}, | ||
[optimizedAppearDataAttribute]: "a", | ||
children: "Content", | ||
}) | ||
|
||
// Emulate server rendering of element | ||
root.innerHTML = ReactDOMServer.renderToString(Component) | ||
|
||
// Start optimised opacity animation | ||
startOptimizedAppearAnimation( | ||
document.getElementById("box"), | ||
"opacity", | ||
[0, 1], | ||
{ | ||
duration: duration * 1000, | ||
ease: "linear", | ||
} | ||
) | ||
|
||
// Start WAAPI animation | ||
const animation = startOptimizedAppearAnimation( | ||
document.getElementById("box"), | ||
"transform", | ||
["translateX(0px)", "translateX(100px)"], | ||
{ | ||
duration: duration * 1000, | ||
ease: "linear", | ||
}, | ||
(animation) => { | ||
setTimeout(() => { | ||
ReactDOM.hydrateRoot(root, Component) | ||
}, (duration * 1000) / 2) | ||
} | ||
) | ||
</script> | ||
</body> | ||
</html> |
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,109 @@ | ||
<html> | ||
<head> | ||
<style> | ||
body { | ||
padding: 100px; | ||
margin: 0; | ||
} | ||
|
||
#box { | ||
width: 100px; | ||
height: 100px; | ||
background-color: #0077ff; | ||
} | ||
|
||
[data-layout-correct="false"] { | ||
background: #dd1144 !important; | ||
opacity: 1 !important; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script src="../../node_modules/react/umd/react.development.js"></script> | ||
<script src="../../node_modules/react-dom/umd/react-dom.development.js"></script> | ||
<script src="../../node_modules/react-dom/umd/react-dom-server-legacy.browser.development.js"></script> | ||
<script src="../../packages/framer-motion/dist/framer-motion.dev.js"></script> | ||
<script src="../projection/script-assert.js"></script> | ||
|
||
<script> | ||
const { | ||
motion, | ||
animateStyle, | ||
animate, | ||
startOptimizedAppearAnimation, | ||
optimizedAppearDataAttribute, | ||
motionValue, | ||
frame, | ||
} = window.Motion | ||
const { matchViewportBox } = window.Assert | ||
const root = document.getElementById("root") | ||
|
||
const duration = 2 | ||
const x = motionValue(0) | ||
|
||
let isFirstFrame = true | ||
|
||
// This is the tree to be rendered "server" and client-side. | ||
const Component = React.createElement(motion.div, { | ||
id: "box", | ||
initial: { x: 0, opacity: 0 }, | ||
animate: { x: 100, opacity: 1 }, | ||
transition: { duration, ease: "linear" }, | ||
style: { x }, | ||
/** | ||
* On animation start, check the values we expect to see here | ||
*/ | ||
onAnimationStart: () => { | ||
const box = document.getElementById("box") | ||
|
||
box.style.backgroundColor = "green" | ||
|
||
setTimeout(() => { | ||
box.style.transform = "scale(2)" | ||
|
||
const { width } = box.getBoundingClientRect() | ||
if (width !== 100) { | ||
showError( | ||
document.getElementById("box"), | ||
`Setting transform became visible, which means optimised animation has been prematurely cancelled. Width was ${width}px instead of 200px.` | ||
) | ||
} | ||
}, 100) | ||
}, | ||
[optimizedAppearDataAttribute]: "a", | ||
children: "Content", | ||
}) | ||
|
||
// Emulate server rendering of element | ||
root.innerHTML = ReactDOMServer.renderToString(Component) | ||
|
||
// Start optimised opacity animation | ||
startOptimizedAppearAnimation( | ||
document.getElementById("box"), | ||
"opacity", | ||
[0, 1], | ||
{ | ||
duration: duration * 1000, | ||
ease: "linear", | ||
} | ||
) | ||
|
||
// Start WAAPI animation | ||
const animation = startOptimizedAppearAnimation( | ||
document.getElementById("box"), | ||
"transform", | ||
["translateX(0px)", "translateX(100px)"], | ||
{ | ||
duration: duration * 1000, | ||
ease: "linear", | ||
}, | ||
(animation) => { | ||
setTimeout(() => { | ||
ReactDOM.hydrateRoot(root, Component) | ||
}, (duration * 1000) / 2) | ||
} | ||
) | ||
</script> | ||
</body> | ||
</html> |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"version": "10.16.15", | ||
"version": "10.16.16-alpha.2", | ||
"packages": [ | ||
"packages/*" | ||
], | ||
|
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 |
---|---|---|
@@ -1 +1 @@ | ||
["interrupt-delay-after.html","interrupt-delay-before-accelerated.html","interrupt-delay-before.html","interrupt-spring.html","interrupt-tween-opacity-waapi.html","interrupt-tween-opacity.html","interrupt-tween-transforms.html","interrupt-tween-x.html","persist.html","portal.html","resync-delay.html","resync.html","start-after-hydration.html"] | ||
["defer-handoff.html","interrupt-delay-after.html","interrupt-delay-before-accelerated.html","interrupt-delay-before.html","interrupt-spring.html","interrupt-tween-opacity-waapi.html","interrupt-tween-opacity.html","interrupt-tween-transforms.html","interrupt-tween-x.html","persist-optimised-animation.html","persist.html","portal.html","resync-delay.html","resync.html","start-after-hydration.html"] |
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
Oops, something went wrong.