Basic animation example: Step-by-step

Matt PerryMatt Perry

In this tutorial, we'll build the Basic animation example step-by-step.

This example is rated 1/5 difficulty, which means we'll spend some time explaining the Motion APIs that we've chosen to use (and why), and also any browser JavaScript APIs we encounter that might be unfamiliar to beginners.

Introduction

This basic example shows how to use Motion's transition options to configure your animations. We'll be using the animate function with custom options to create a smooth scaling animation.

Get started

Let's start with the basic HTML and CSS structure:

<div class="box"></div>
 
<style>
    .box {
        width: 100px;
        height: 100px;
        background-color: #ff6c85;
        border-radius: 10px;
        transform: scale(0.4);
    }
</style>

Let's animate!

Import from Motion

First, we need to import the animate function from Motion:

import { animate } from "motion"

Basic functionality

Now, let's implement the animation that scales our box from its initial size (defined as 0.4 in the above CSS) to full size (1):

animate(".box", { scale: 1 }, { ease: "circInOut", duration: 1 })

This single line does three important things:

Options explained

Let's break down the options we're using:

{ ease: "circInOut", duration: 1 }

Motion provides many options to customize your animations:

Easing Functions

The ease option accepts various easing functions that control the rate of change during the animation:

Try it yourself

You can experiment with different options to see how they affect the animation:

// Slow bounce effect
animate(".box", { scale: 1 }, { ease: "easeIn", duration: 2 })
 
// A delayed ease in/out with a fast middle
animate(
    ".box",
    { scale: 1 },
    { ease: [0.42, 0.1, 0.25, 0.92], duration: 0.5, delay: 0.3 }
)
 
// Looping animation
animate(
    ".box",
    { scale: 1 },
    {
        ease: "easeInOut",
        duration: 1,
        repeat: Infinity,
        repeatType: "mirror",
    }
)

Conclusion

In this tutorial, we learned how to use Motion's animation options to control the timing, easing, and behavior of animations. These options provide fine-grained control over your animations, allowing you to create experiences that feel natural and engaging.

Tutorial Project

We're currently working on adding a tutorial for every example on the Motion Examples website. So far, 12% of examples have a tutorial.