npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

troika-animation

v0.52.0

Published

Troika Animation Utilities

Downloads

1,623

Readme

Troika Animation

This package provides a small library for "tweening" values over time, heavily optimized for performance. It is used by the Troika framework behind the scenes for its declarative transition and animation features, but can also be used as a standalone library.

Installation

If you're using the Troika framework, you don't need to install this directly. If you want to use it standalone, you can get it from NPM:

npm install troika-animation

Usage

To animate a value, you basically need to:

  1. Create a Tween describing the start and end values, how the value should change over time, and a callback to invoke on each frame
  2. Create a Runner
  3. Start the tween in the runner

Tween

See the JSDoc comments in Tween.js for more details about each parameter.

import { Tween } from 'troika-animation'

function onTweenFrame(tweenedValue) {
  // ...do something with the tweenedValue
}

const tween = new Tween(
  onTweenFrame,  // callback
  -100,          // fromValue
  100,           // toValue
  5000,          // duration in ms
  0,             // delay
  'easeOutExpo', // easing
  1,             // iterations
  'forward',     // direction
  'number'       // interpolation
)

MultiTween

This is a specialized Tween that, instead of managing a value, manages a list of other Tweens. It essentially creates a sub-timeline, whose playback is controlled as a whole. For example you can apply an easing to the entire set of tweens as a whole, stretch out their duration, repeat them, etc.

import { MultiTween, Tween } from 'troika-animation'

const tween1 = new Tween(/*...*/)
const tween2 = new Tween(/*...*/)
const multiTween = new MultiTween(
  [tween1, tween2], // list of tweens
  5000,             // duration
  0,                // delay
  'easeInOutExpo',  // easing
  10,               // iterations
  'alternate'       // direction
)

SpringTween

See the JSDoc comments in SpringTween.js for more details about each parameter.

This is a lot like Tween but instead of having a fixed duration, its value is transitioned using a simple spring physics simulation.

import { SpringTween } from 'troika-animation'

const tween = new Tween(
  onTweenFrame,   // callback
  -100,           // fromValue
  100,            // toValue
  {               // spring simulation config, or the name of a preset
    mass: 1,      // object mass
    tension: 170, // spring tension force
    friction: 26  // friction force
  },
  0               // delay
)

The meanings of the spring configuration parameters match those from react-spring. The named presets also match those defined by react-spring.

Runner

Also see the JSDoc comments in Runner.js

import { Tween, Runner } from 'troika-animation'

const tween = new Tween(/*...*/)
const runner = new Runner()
runner.start(tween)

Easings

When constructing a Tween, the easing parameter controls the rate of change over time. You can either specify a string, referring to the name of one of the built-in easing functions, or you can provide a custom easing function.

Built-in named easings

The built-in named easings can be found in Easings.js. These may be familiar, as they mostly match the ones provided by the popular jQuery Easing Plugin and easings.net.

There is also an online demo of troika-animation easings that shows the curve for each.

Custom easing functions

An easing function takes a single parameter t, which refers to the fraction of time passed in the tween's duration from 0 to 1. It must return a number specifying the progress between the start and end values at t; 0 corresponds to the start value and 1 corresponds to the end value. Most easings stay in the range from 0 to 1 but some may exceed it.

Interpolation

The tween interpolation parameter controls how the fractional value returned from the easing is applied to the start and end values to calculate the in-between value. In most cases you're probably tweening two numbers, which is handled as the default interpolation. But if you have a different type of value, you may need to change the interpolator.

Two built-in interpolation types are provided, which can be referred to by their string names (see Interpolators.js)

  • "number" - simple linear interpolation between two numeric values (the default).
  • "color" - interprets the start/end values as RGB colors, and interpolates each color channel independently. The start/end values can be 24-bit integers or any CSS color string value, and the interpolated values will always be returned as 24-bit integers (8 bits red, 8 bits green, 8 bits blue.)

If you need a different interpolation, you can provide a custom function. Your function will take three parameters: the start value, the end value, and the progress between them (the output of the easing function) in the range from 0 to 1. It must return an in-between value of the appropriate type.