GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • DocsDocs
  • ExamplesExamples
  • AI KitAIAI KitAI
  • React
  • JS
  • Vue
  • AI
  • animate
  • Timeline sequences
  • animateLayout
  • animateView
  • scroll
  • Easing
  • drag

This view

  • Original page
  • New address

GeezerVison is showing motion.dev — motionValue | Composable animation values | Motion

Motion Values track the state and velocity of animated values.

They are composable, signal-like values that are performant because Motion throttles rendering with its optimised frameloop.

Motion Values are usually created automatically by the animate function or motion components. They aren't something you generally have to think about.

But, for advanced use cases, it's possible to create them manually.

const x = motionValue(0)

x.on("change", latest => console.log(latest))

animate(x, 100)

By manually creating motion values you can:

  • Set and get their state.

  • Subscribe to changes via the on method.

  • Automatically end existing animations when starting new animations.

const color = motionValue("#f00")

animate(color, "#0f0")

animate(color, "#333") // Will automatically end the existing animation
Spring: Follow CursorOpen example
Black Friday

Motion+ is 20% off until 30 Nov.

One payment, lifetime updates. Get every premium example, Motion UI and AI Kit for your coding agent.

Get 20% offGet 20% off

Usage

Motion Values can be created with the motionValue function. The string or number passed to motionValue will act as its initial state.

import { motionValue } from "motion"

const x = motionValue(0)

Changes to a Motion Value can be subscribed to with the .on method.

x.on("change", latest => console.log(latest))

Set value

Motion Values can be updated with the set method.

x.set(100)

Get value and velocity

The latest value of a Motion Value can be read with .get():

const latest = x.get() // 100

It's also possible to get the velocity of the value via .getVelocity():

const velocity = x.getVelocity()

Velocity is available for any number-like value, for instance 100, or unit types like "50vh" etc.

Velocity is intelligently calculated by using the value rendered during the previous animation frame (rather than the last value passed via set).

Render

Motion values can be passed to effects like styleEffect, attrEffect or propEffect to render the latest values once per animation frame.

const x = motionValue(0)
const opacity = motionValue(1)

styleEffect("li", { x, opacity })

x.set(100) // Will apply to all <li> elements on the next frame
animate(opacity, 0) // Will animate all <li> opacity

API

get()

Returns the latest state of the Motion Value.

getVelocity()

Returns the latest velocity of the motion value. Returns 0 if the value is non-numerical.

set()

Sets the Motion Value to a new state.

x.set("#f00")

jump()

Jumps the Motion Value to a new state in a way that breaks continuity from previous values:

  • Resets velocity to 0.

  • Ends active animations.

animate(x, 100)

x.jump(10)
x.getVelocity() // 0

isAnimating()

Returns true if the value is currently animating.

stop()

Stop the active animation.

on()

Subscribe to Motion Value events. Available events are:

  • change

  • animationStart

  • animationCancel

  • animationComplete

import { motionValue, frame } from "motion"

const color = motionValue("#fff")

color.on("change", latest => {
  frame.render(() => element.style.backgroundColor = latest)
})

It returns a function that, when called, will unsubscribe the listener.

const unsubscribe = x.on("change", latest => console.log(latest))

destroy()

Destroy and clean up subscribers to this Motion Value.

Motion+One payment
Lifetime updates
Professional animation toolkit

Animate faster with Motion+.

Unlock 550+ premium examples, Motion UI, AI Kit and private community access.

Examples
550+
Tutorials
110+
AI Kit
Included
Explore Motion+Explore Motion+
  1. 01springValue

    springValue() creates motion values driven by physics, with stiffness, damping, and mass.

  2. 02transformValue

    transformValue() derives a new motion value by computing output from one or more existing values.

  3. 03mapValue

    mapValue() maps a motion value's output range to numbers, colours, or complex strings.

  • Conic gradient pointer animation exampleConic gradient pointer
  • Spring: Follow Cursor animation exampleSpring: Follow Cursor

Motion is supported by

Motion Cloud members who help fund Motion’s open-source development.

Become a supporter
  • nimit
  • AAriel Rodribuez
  • BBeto
  • CCNTNAI
  • SStanislav Shilin
  • AAbhishek
  • Joshosullivam
  • DDamián Navarro
  • AAriel Conti
  • Sean Bugeja
  • electrinity
  • Meepo
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.