React Animated Handwriting Text with Vara.js

Minh Vu

By Minh Vu

Updated Nov 22, 2023

Figure: React Animated Handwriting Text with Vara.js

Disclaimer: All content on this website is derived directly from my own expertise and experiences. No AI-generated text or automated content creation tools are used.

Hey guys, have you been looking for an animation library that can animate the text by the stroke? If so, you are at the right place.

Let's see how we can create an animated handwriting text in React like the image below with the Vara library.

Contents

1. Installing Vara.js

Before using the Vara library, we will have to install it. You can use npm install vara to install Vara.js.

If you are using TypeScript, remember to install its type by using the npm install -D @types/vara command.

2. Using Vara.js to Create Animated Handwriting Text in React

Let's create a new file called VaraText.tsx (or VaraText.jsx if you are using JavaScript) inside src/components.

Then, we will create a component called VaraText as follow:

src/components/VaraText.tsx
function VaraText({ text }: { text: string }) {
  useEffect(() => {
    var vara = new Vara(
      "#vara-container",
      "https://raw.githubusercontent.com/akzhy/Vara/master/fonts/Satisfy/SatisfySL.json",
      [
        {
          text: text,
          fontSize: 40,
          strokeWidth: 0.7
        }
      ]
    );
  }, []);
 
  return <div id="vara-container" className="z-[20]"></div>;
}

I will explain line-by-line from the code above.

  1. First of all we define a React component called VaraText with one argument text which is the text we want to write out.
  2. Then, inside the useEffect hook we will create a new Vara component. This component will be rendered in a div that we will define later.
  3. The first parameter will be the id of the div tag, which was mentioned above, that we will draw out the text.
  4. The second parameter will be the JSON file of the font being used. There are four pre-made fonts from the Vara's author. You can choose any of them for your text, in this case, I will pick SatisfySL.
  5. The third parameter is a list containing different texts you want to show. Each text will have various properties for you to customize such as fontSize, strokeWidth, speed, and more.

For the second parameter, remember to insert the URL of the raw font file on GitHub, which starts with "https://raw.githubusercontent...".

You can add more text like this:

src/components/VaraText.tsx
function VaraText({ text }: { text: string }) {
  useEffect(() => {
    var vara = new Vara(
      "#vara-container",
      "https://raw.githubusercontent.com/akzhy/Vara/master/fonts/Satisfy/SatisfySL.json",
      [
        {
          text: text,
          fontSize: 40,
          strokeWidth: 0.7
        },
        {
          text: "Some text",
          fontSize: 26
        },
        {
          text: "Some more text"
        }
      ]
    );
  }, []);
 
  return <div id="vara-container" className="z-[20]"></div>;
}

After creating the VaraText component, you can go anywhere on your page to create an animated handwriting text component. For example, let's modify the index.tsx inside src/pages to see how it works.

src/pages/index.tsx
import VaraText from "@/components/VaraText";
 
export default function Home() {
  return (
    <div>
      <VaraText text="WiseCode Team" />
    </div>
  );
}

This code will output the text "WiseCode Team" as we can see from the Introduction section.

Conclusion

That's all for this tutorial. We have been using Vara to create beautiful animated handwriting text in React.

For more customization, please visit the Vara project homepage to learn how to adjust the animation and all other properties of the text.

Happy coding!

Minh Vu

Minh Vu

Software Engineer

Hi guys 👋, I'm a developer specializing in Elastic Stack and Next.js. My blog shares practical tutorials and insights based on 3+ years of hands-on experience. Open to freelance opportunities — let's get in touch!

Comments

Be the first to comment!

Leave a Comment

Receive Latest Updates 📬

Get every new post, special offers, and more via email. No fee required.