10

Create Velocity Scroll Animation in React with Framer Motion

 1 year ago
source link: https://blog.bitsrc.io/create-velocity-scroll-animation-in-react-with-framer-motion-4f1d00dbe26
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Create Velocity Scroll Animation in React with Framer Motion

0*4WtzqwcCtTd6mxK_.png

Introduction

Scrolling in a web page is very common and we do that everyday. But have you ever tried to create an animation based on scrolling ? As a gaming enthusiast, I love animations effects, so I tried to add some animation with Framer Motion

Framer Motion is a production-ready motion library for React. It is simple yet powerful, allowing you to express complex user interactions with robust, semantic markup. I wrote a tutorial on how to create a navigation animation with it, you can check it out on this article

Set up

Well, first things we need are… React and Framer Motion, of course, so let’s create a simple React app and install Framer Motion with

yarn add framer-motion

Then we will create a simple layout like this:

1*YIKrWWIZe3NCiaUa4GlVnQ.png

In the code, we create a body that has a lot of height so we can scroll. And a text, I recommend using span so many span tags still appear inline

Create the animation

There are 2 animations I want to create in this example :

  • When scrolling up and down, the text will scroll horizontal left and right
  • When scrolling up and down, the text will tilt left and right

All based on scroll velocity, let’s go to the first animation

For the first animation to work, we need to have a motion value, that I will name it baseX ( for transformX in CSS ) with default value is 0:

1*iEgsGUKldfPquQqjiXT7tQ.png

Then we use the useScroll hook to get the scrollY for vertical scroll. Next is the useVelocity hook that we are going to pass the scrollY to it. So it can keep track of the scrollX to calculate the velocity. Finally we use useSpring for some spring animation, if you used react-spring before, this should be very similar.

Next thing we want to do is transform the velocity we have above to the value we need within the range with useTransform hook.

1*7j1Zx1TbKjBOxUG6n5neqQ.png

Then we calculate the transformX with useTransform using the baseX value we have ( don’t mind the wrap function, you can check it out in my source code below, it simply wrap the range of values )

The most important part is useAnimationFrame hook that triggers every frame. So every frame in our app, we calculate the velocityFactor to see if we are scrolling up or down to set the transformX value to left or right, above is my formula.

Finally, we put the x variable to the style attribute in a motion variable like this:

1*kYfGtqEiHPTKbZcURfbvlg.png

And we will have this animation:

1*S53ThKrnQ5zdD7SWGzTytw.gif

You can see the faster you scroll, the faster the text transforms ( if the gif’s quality is bad, my demo is at the end of the article )

The tilting text

The second animation that I mentioned above is the tilting animation, similar to the transformX above, we are going to use the skew attribute.

1*aSL9Ch_bOq7DKIFg9-evTQ.png

This animation is simpler, we have the scrollVelocity before, we will create spring values for it. And transform it to the -30 and 30 range, so our text won’t be tilted more than 30 degree.

1*ePM315hYrlb7erJuLM5DUg.png

Then we pass the skewVelocityFactor to skew prop, this time we wrap the span with motion . So you will have this animation:

1*eXNqZd5hD4eJA61a97TVwg.gif

The text will be tilted based on the scroll velocity.
Sorry for the GIF quality but it couldn’t reach 60fps.

You should try the demo: https://scroll-velocity-framer-motion.vercel.app/

And if the article didn’t show you enough details, check out my source code

Thanks for reading!

Build apps with reusable components like Lego

1*mutURvkHDCCgCzhHe-lC5Q.png

Bit’s open-source tool help 250,000+ devs to build apps with components.

Turn any UI, feature, or page into a reusable component — and share it across your applications. It’s easier to collaborate and build faster.

Learn more

Split apps into components to make app development easier, and enjoy the best experience for the workflows you want:

Micro-Frontends

Design System

Code-Sharing and reuse

Monorepo


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK