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

Reducing canvas memory usage #1026

Merged
merged 21 commits into from
Mar 12, 2021
Merged

Reducing canvas memory usage #1026

merged 21 commits into from
Mar 12, 2021

Conversation

mattgperry
Copy link
Collaborator

This PR makes it possible for a motion component to render in static mode without instantiating a VisualElement, significantly reducing memory consumption.

A VisualElement is an abstraction around the renderer, by default the DOM. It enables us to performantly interact with the renderer and to communicate throughout the tree, away from React and its lifecycles. We create one for every motion component.

Static mode is a flag we can switch on in the Framer canvas and in server rendering where motion components behave more like normal elements in that a new style tag is created every render and no animations/gestures are mounted.

This PR changes the architecture slightly so that the initial map of values is created externally from the VisualElement and can itself either be used to render the motion component via React in static mode or to instantiate the VisualElement in normal mode.

This will save hundreds of megabytes of memory consumption from the Framer canvas in large prototypes. In a future refactor I'll change the VisualElement factory function to a class which will cut memory consumption in normal mode also.

This architecture also gets us pretty close to allowing the VisualElement itself to be lazy loaded, so we can bring the m component down from 13kb to something closer to 5.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 10, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit ab68333:

Sandbox Source
Framer Motion: Simple animation Configuration
App Store UI using React and Framer Motion Configuration
Framer Motion: Reorder animation Configuration
Framer Motion: growing item positionTransition issue Configuration
Framer Motion: Image lightbox Configuration

@eelco eelco requested review from nvh and adamseckel and removed request for nvh March 10, 2021 14:23
@@ -98,6 +98,7 @@ export function resolveVariant(
export function checkIfControllingVariants(props: MotionProps) {
return (
typeof (props.animate as AnimationControls)?.start === "function" ||
isVariantLabel(props.initial) ||
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this a drive by fix?

@@ -21,7 +21,7 @@ export const App = () => {
data-testid="rotate"
style={{ rotate: 45 }}
/>
<motion.rect
{/* <motion.rect
Copy link
Collaborator

Choose a reason for hiding this comment

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

This need to stay commented out?

Copy link
Collaborator

@adamseckel adamseckel left a comment

Choose a reason for hiding this comment

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

Looks good. Aside from the clear problem it solves I think the solution of passing the visualState into useVisualElement is quite elegant 👌

@mattgperry mattgperry added the automerge Land this PR label Mar 12, 2021
@mergetron mergetron bot merged commit def9f7f into main Mar 12, 2021
This was referenced Mar 15, 2021
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