Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

popmotion

> 前端框架
Open source

Simple animation libraries for delightful user interfaces

20.2K stars0 likes5 views
WebsiteGitHub

About

Simple animation libraries for delightful user interfaces

The animator's toolbox

Popmotion is:

  • Powerful: It supports keyframe and spring animations for numbers, colors and complex strings.
  • Low level: It's designed to be composable and portable into any JavaScript environment, with an eye on worklets in the future.
  • Stable: It's written in TypeScript and enjoys over 95% test coverage.
  • Tiny: animate is just ~4.5kb, and every function is individually importable.

Quick start

bash
npm install popmotion
javascript
import { animate } from "popmotion"

animate({
  from: 0,
  to: 100,
  onUpdate: latest => console.log(latest)
})

Animation

animate

animate performs a keyframes or spring animation.

javascript
import { animate } from "popmotion"

animate({
  from: 0, 
  to: 100,
  onUpdate: latest => console.log(latest)
})

It can animate numbers:

javascript
animate({ from: 0, to: 100 })

Or strings of the same type:

javascript
animate({ from: "0px", to: "100px" })
animate({ from: "#fff", to: "#000" })

The strings can be pretty complex, for instance box shadows or SVG path definitions. The only limitation is that the numbers and colors contained within must be in the same order:

javascript
animate({
  from: "0px 0px 0px rgba(0, 0, 0, 0)",
  to: "10px 10px 0px rgba(0, 0, 0, 0.2)"
})

The type of animation performed will be automatically detected from the provided options, or can be chosen manually by defining type as "keyframes", "spring" or "decay".

Options

These options can be set for all animations:

from

An initial value to start the animation from.

Defaults to 0

javascript
animate({
  from: "linear-gradient(#e66465, #9198e5)",
  to: "linear-gradient(#9198e5, #e66465)"
})
elapsed

Sets an initial elapsed time, in milliseconds. Set to a negative value for a delay.

javascript
animate({
  to: 100,
  elapsed: -300
})
repeat

The number of times to repeat the animation. Set to Infinity to repeat forever.

javascript
animate({
  to: 100,
  repeat: 2
})
repeatDelay

The duration, in milliseconds, to wait before repeating the animation.

javascript
animate({
  to: 100,
  repeat: 2,
  repeatDelay: 200
})
repeatType

Either "loop", "mirror" or "reverse". Defaults to "loop".

  • "loop": Repeats the animation from 0.
  • "mirror": Swaps the from/to values alternately.
  • "reverse": Reverses the animation alternately.
javascript
animate({
  to: 100,
  repeat: 2,
  repeatType: "reverse"
})
driver

By default, the animation will be driven by a requestAnimationFrame loop. driver can specify a different source.

A Driver is a function that accepts the animations update function. This is a function that can be called with a time delta from the previous frame. The Driver must return a function that will be called when the animation is stopped.

javascript
const xrDriver = session => update => {
  let latestRequestId = 0
  let prevTimestamp = performance.now()
  
  const step = timestamp => {
    const delta = timestamp - prevTimestamp
    prevTimestamp = timestamp

    update(delta)

    latestRequestId = session.requestAnimationFrame(step)
  }

  let latestRequestId = session.requestAnimationFrame(step)

  return () => session.cancelRequestAnimationFrame(latestRequestId)
}

animate({
  to: 100,
  driver: xrDriver(xrSession)
})
type

animate will automatically detect the type of animation to use based on the options provided. But a specific type can be chosen manually by defining type as "keyframes", "spring" or "decay".

javascript
animate({
  to: 100,
  type: "spring"
})

Lifecycle events

The following lifecycle events are available for all animations:

onUpdate

This is called every frame the animation fires with the latest computed value.

javascript
animate({
  to: 100,
  onUpdate: latest => console.log(latest)
})
onPlay

This is called when the animation starts. Currently this automatically when animate is called.

javascript
animate({
  to: 100,
  onPlay: () => {}
})
onComplete

This is called when the animation successfully completes.

javascript
animate({
  to: 100,
  onComplete:() => {}
})
onRepeat

This is called when an animation repeats.

javascript
animate({
  to: 100,
  repeat: 2,
  onRepeat: () => {}
})
onStop

This is called when the animation is stopped by the stop control.

javascript
const animation = animate({
  to: 100,
  onStop: () => {}
})

animation.stop()

Keyframes options

A keyframes animation is the default animation type and it can be defined either with a from and to option:

javascript
animate({ from: 0, to: 100 })

Or as a series of keyframes provided to the to option:

javascript
animate({ to: [0, 100, 200] })
to

A single value to animate to, or an array of values to animate through.

javascript
animate({
  to: ["#0ff", "#f00", "#0f0"]
})

If to is an array, any defined from will be ignored.

duration

This defines the duration of the animation, in milliseconds.

javascript
animate({
  to: 100,
  duration: 300
})
ease

This is an easing function, or array of functions, to use when easing between each keyframe.

javascript
import { animate, linear, easeInOut } from "popmotion"

animate({
  to: 100,
  ease: linear
})

animate({
  to: ["#fff", "#000", "#f00"],
  ease: [linear, easeInOut]
})

If set as any array, the length of this array must be one shorter than the number of values being animated between.

offset

This is an array of values between 0 and 1 that defines at which point throughout the animation each keyframe should be reached.

This array should be the same length as the number of defined keyframes.

javascript
animate({
  to: ["#fff", "#000", "#f00"],
  offset: [0, 0.2, 1]
})

Spring options

Springs are great for creating natural-feeling interfaces and dynamic interruptable animations.

A spring animation will be used if any of the stiffness, damping or mass options are detected.

Note: A spring simulation is inherently numerical so if it's given a color, array or object, it runs the animation from 0 to 100 and interpolates that to the given values. This strategy is likely to be tweaked before the official release so animations made this way may change in feel.

to

A single value to animate to.

javascript
animate({
  to: 100,
  type: "spring"
})

If to is an array, any defined from will be ignored.

stiffness

This defines the stiffness of the spring. A higher stiffness will result in a snappier animation.

Defaults to 100

javascript
animate({
  to: 100,
  stiffness: 1000
})
damping

This is the opposing force to stiffness. As you reduce this value, relative to stiffness, the spring will become bouncier and the animation will last longer. Likewise, higher relative values will have less bounciness and result in shorter animations.

Defaults to 10

javascript
animate({
  to: 100,
  damping: 50
})
mass

This is the mass of the animating object. Heavier objects will take longer to speed up and slow down.

Defaults to 1.

javascript
animate({
  to: 100,
  mass: 2
})
velocity

The initial velocity, in units per second, of the animation.

javascript
animate({
  to: 100,
  velocity: 1000
})
duration

The duration of the spring, in milliseconds.

Will be overridden by stiffness, mass or damping.

javascript
animate({
  to: 100,
  duration: 1000
})
bounce

The bounciness of the spring, as a value between 0 and 1, where 0 is no bounce.

Will be overridden by stiffness, mass or damping.

javascript
animate({
  to: 100,
  bounce: 0.2
})
restDelta

The distance from the animation target at which the animation can be considered complete. When both restDelta and restSpeed are met, the animation completes.

javascript
animate({
  to: 100,
  restDelta: 0.5
})
restSpeed

The absolute velocity, in units per second, below which the animation can be considered complete. When both restDelta and restSpeed are met, the animation completes. Defaults to 10.

javascript
animate({
  to: 100,
  restSpeed: 5
})

Playback controls

animate returns PlaybackControls, which can be used to control the playback of the animation.

Currently this only includes a stop method, but may expand with more.

stop

Stops the animation.

javascript
const playback = animate({ from: 0, to: 100 })
playback.stop()

inertia

The inertia animation is used to gradually decelerate a number. Think smartphone scroll momentum.

Options

In addition to animate's from, onUpdate and onComplete options, inertia also supports the following:

velocity

The initial velocity, in units per second, of the animation.

javascript
inertia({
  from: 0,
  velocity: 100
})
power

A constant with which to calculate a target value. Higher power = further target.

Defaults to 0.8.

javascript
inertia({
  from: 0,
  power: 0.3
})
timeConstant

Adjusting the time constant will change the duration of the deceleration, thereby affecting its feel.

Defaults to 350.

javascript
inertia({
  from: 0,
  velocity: 100,
  timeConstant: 400
})
modifyTarget

A function that receives the calculated target and returns a new one. Useful for snapping the target to a grid.

javascript
const roundToNearest = target => v => Math.ceil(v / target) * target

inertia({
  from: 0,
  velocity: 100,
  modifyTarget: roundToNearest(100)
})
min

The minimum value at which the animation will switch from gradual deceleration and use a spring animation to snap to this point.

javascript
inertia({
  from: 50,
  velocity: -100,
  min: 0
})
max

The maximum value at which the animation will switch from gradual deceleration and use a spring animation to snap to this point.

javascript
inertia({
  from: 50,
  velocity: 100,
  max: 100
})
bounceStiffness

This defines the stiffness of the spring when the animation hits either min or max. A higher stiffness will result in a snappier animation.

Defaults to 500

javascript
inertia({
  from: 0,
  velocity: 100,
  max: 50,
  bounceStiffness: 1000
})
bounceDamping

This is the opposing force to bounceStiffness. As you reduce this value, relative to bounceStiffness, the spring will become bouncier and the animation will last longer. Likewise, higher relative values will have less bounciness and result in shorter animations.

Defaults to 10

javascript
inertia({
  from: 0,
  velocity: 100,
  max: 50,
  bounceDamping: 300
})
restDelta

The distance from the animation target at which the animation can be considered complete.

javascript
inertia({
  from: 0,
  velocity: 100,
  restDelta: 0.5
})

Iterators

Powering animate and inertia are the keyframes, spring, and decay iterators.

javascript
import { keyframes, spring, decay } from "popmotion";

Iterators give you the ability to run an animation with a high degree of control. For example, Framer uses the spring iterator to draw its animation editor visualiser by running it synchronously.

Each can be initialised with the matching options above (decay with a subset of inertia's options, excluding the bounce- options):

javascript
const animation = spring({
  from: 0,
  to: 100,
  stiffness: 200
})

With the returned iterator, you can resolve the animation at a specific timestamp with its next method.

javascript
// Resolve the animation at 200ms

Issues· 50 open

View all issuesOpen on GitHub
  • #965

    Both

    Updated Aug 17, 2025
  • #964

    is this project abandoned?, I want to add: [get velocity method] when animation is playing

    Updated Jul 27, 2025
  • #866

    How to sequence animations on different elements

    Updated Jun 15, 2025
  • #963

    Disable easing in and out

    Updated Sep 28, 2024
  • #956

    Popmotion.io footer nav link

    Updated Mar 12, 2024
  • #933

    popmotion 11.0.3 import not working (missing exports)

    Updated Nov 19, 2023
  • #960

    Is there a lifecycle event that can listen to the completion of each repetition?

    Updated Jun 21, 2023
  • #929

    Popmotion: Animation duration stretched when using pixi.js Ticker as animation driver

    Updated May 19, 2023
  • #957

    Pause and rewind capability

    Updated Apr 12, 2023
  • #955

    Modernize build target and eliminating tslib.

    Updated Feb 3, 2023

> Tags

JavaScriptanimationcssjavascript-motion-enginemotion

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架