Tweening
Also called inbetweening or tween animation
Generating the in-between frames of an animation from a start and an end state, so an element moves smoothly from one to the other.
import { useRef, useState } from 'react';
import { motion, useReducedMotion } from 'motion/react';
const IMAGE = '/products/stoneware-mug.jpg';
type Flight = Record<'id' | 'left' | 'top' | 'width' | 'dx' | 'dy', number>;
export function ProductCard() {
const imageRef = useRef<HTMLImageElement>(null);
const cartRef = useRef<HTMLSpanElement>(null);
const [count, setCount] = useState(0);
const [flight, setFlight] = useState<Flight | null>(null);
const reduceMotion = useReducedMotion();
function addToCart() {
setCount((n) => n + 1);
const from = imageRef.current?.getBoundingClientRect();
const to = cartRef.current?.getBoundingClientRect();
if (reduceMotion || !from || !to) return;
const { left, top, width } = from;
// Fly from the image's center to the cart's center.
const dx = to.x + to.width / 2 - (left + width / 2);
const dy = to.y + to.height / 2 - (top + from.height / 2);
setFlight({ id: Date.now(), left, top, width, dx, dy });
}
return (
<div className='flex w-72 flex-col gap-4'>
<span ref={cartRef} className='self-end text-sm font-medium'>
Cart ({count})
</span>
<img
ref={imageRef}
src={IMAGE}
alt='Stoneware mug'
className='aspect-square rounded-xl object-cover'
/>
<button
type='button'
onClick={addToCart}
className='rounded-md bg-primary px-4 py-2 text-sm font-medium text-primary-foreground'
>
Add to cart
</button>
{flight && (
<motion.img
key={flight.id}
src={IMAGE}
alt=''
// A tween: start, end, a fixed 500ms, and one easing curve.
initial={{ x: 0, y: 0, scale: 1 }}
animate={{ x: flight.dx, y: flight.dy, scale: 0.3 }}
transition={{ type: 'tween', duration: 0.5, ease: 'easeInOut' }}
onAnimationComplete={() => setFlight(null)}
style={{ left: flight.left, top: flight.top, width: flight.width }}
className='pointer-events-none fixed aspect-square rounded-xl object-cover'
/>
)}
</div>
);
}When a product is added to the cart, tween a copy of its thumbnail from the product card to the cart icon over 500ms with an ease-in-out curve, shrinking it from scale 1 to 0.3 on the way. Animate only transform and opacity, and skip the flight when prefers-reduced-motion is set.
Paste into Layerwise or any AI coding tool.
What is Tweening?
Tweening, short for in-betweening, comes from hand-drawn animation: a lead artist drew the key poses and assistants drew the frames between them. On the web the browser or an animation library does that work, calculating a new frame for every screen refresh.
You describe the start state, the end state, a duration, and an easing curve. The easing decides how the in-between frames are spaced: evenly with linear, bunched up at both ends with ease-in-out. CSS transitions, CSS animations, and libraries such as Motion and GSAP all tween.
A tween always takes the same, exact amount of time, which makes it predictable and easy to line up with other motion. A spring animation is the main alternative: it has no fixed duration, and it handles being interrupted halfway through more naturally.
Often confused with
- Spring animation
- A spring animation is driven by physics settings and has no fixed duration. A tween runs for an exact time along an easing curve.
Related terms
All termsKeyframes
Snapshots that define how an element looks at set points in an animation. The browser fills in the motion between them.
Easing
A curve that controls how an animation’s speed changes over time, so motion can start slow, land softly, or keep one steady pace.
Spring animation
An animation driven by simulated spring physics in place of a fixed duration and curve, so motion can overshoot, settle, and react naturally.
Frame rate (FPS)
The number of frames a screen draws each second; around 60 fps, motion looks smooth, and at lower rates it starts to look choppy.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.