Skip to content
60fps/ui

Motion

Motion drives a number — or the keys of an object — from one value to another using a spring, damping curve or inertial fling. It runs on a single shared frame loop and emits a change event on every tick, so the adapter just forwards that value to your view.

useAnimate

useMotion

Click the track to spring the box across.

Usage

In React, useAnimate keeps the value off the render path entirely — it hands you a ref and writes to the DOM node directly on each frame:

import { useAnimate } from '@60fps/react/motion';

function Box() {
	const [ref, motion] = useAnimate<HTMLDivElement>(0, (x, el) => {
		el.style.transform = `translateX(${x}px)`;
	});

	return <div ref={ref} onClick={() => motion.set(240, { easing: 'spring' })} />;
}

Prefer reactive state instead? useMotion returns a stateful value you can render directly:

import { useMotion } from '@60fps/react/motion';

const [x, motion] = useMotion(0);
// x updates on every frame; motion.set(…) to animate

In Vue, useMotion returns a [ref, motion] tuple — or binds an object key in place:

<script setup lang="ts">
import { useMotion } from '@60fps/vue/motion';

const [x, motion] = useMotion(0);
const onClick = () => motion.set(240, { easing: 'spring' });
</script>

<template>
	<div @click="onClick" :style="{ translate: `${x}px` }" />
</template>

API

motion.set(value, options?)

Animates toward value, returning a promise that resolves when the motion settles.

Option Type Default Description
easing 'spring' | 'lerp' | 'damping' | 'immediate' 'spring' Interpolation curve used to reach the target.
immediate boolean false Jump to the value with no animation (also available as easing: "immediate").
inertia boolean false Carry the current velocity into a fling, decelerating toward the target.
snapPoints number[] When flinging, settle onto the nearest of these values.
velocity number Seed velocity for an inertial motion (px/ms).
min / max number ∓Infinity Clamp bounds; values beyond them rubberband during inertia.

Other members

Member Type Default Description
value number The current animated value.
stop() () => void Halt the motion in place and emit `stop`.
on('change', cb) (value) => void Subscribe to per-frame value updates.
clean() () => void Stop the loop and remove all listeners.