Member-only story

Framer Motion — MotionValues

John Au-Yeung
3 min readFeb 13, 2021

--

Photo by Dimon Blr on Unsplash

With the Framer Motion library, we can render animations in our React app easily.

In this article, we’ll take a look at how to get started with Framer Motion.

Injecting MotionValues

We can inject MotionValues into our components.

They’ll be reflected in all the components.

For example, we can write:

import { motion, useMotionValue } from "framer-motion";
import React from "react";
export default function App() {
const x = useMotionValue(0);
return (
<>
<motion.div
drag="x"
style={{ x, backgroundColor: "red", width: 50, height: 50 }}
/>
<motion.svg drag="x">
<motion.circle cx={x} cy="30" r="20" stroke-width="3" fill="red" />
</motion.svg>
</>
);
}

We add the motion.div and motion.svg to set the x position when we drag on both the div and the circle.

We set the x property in the style prop for HTML elements.

For SVGs, we set x as the value of the attribute.

We can watch the latest value of a MotionValue with the onChange method.

For instance, we can write:

import { motion, useMotionValue }…

--

--

No responses yet