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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-state-animation

v0.1.0

Published

Simple animations with React state

Readme

react-state-animation

react-state-animation provides a capability to update React component's state value by requestAnimationFrame with a simple APIs that builds on d3-ease. The file size is just 4KB (minified).

This works with regular React component and React Canvas

Installation

npm install react-state-animation --save
Include the module by CommonJS way
import ReactStateAnimation from 'react-state-animation' or var ReactStateAnimation = require('react-state-animation');

This will require ES5 modules converted by babel. ES6 sources are in /src and converted ES5 modules are located in /lib.

##Demo http://tejitak.github.io/react-state-animation/examples/demo/

API

  • linearIn(stateProp, endStateValue, duration)
  • linearOut(stateProp, endStateValue, duration)
  • linearInOut(stateProp, endStateValue, duration)
  • quadIn(stateProp, endStateValue, duration)
  • quadOut(stateProp, endStateValue, duration)
  • quadInOut(stateProp, endStateValue, duration)
  • cubicIn(stateProp, endStateValue, duration)
  • cubicOut(stateProp, endStateValue, duration)
  • cubicInOut(stateProp, endStateValue, duration)
  • polyIn(stateProp, endStateValue, duration)
  • polyOut(stateProp, endStateValue, duration)
  • polyInOut(stateProp, endStateValue, duration)
  • sinIn(stateProp, endStateValue, duration)
  • sinOut(stateProp, endStateValue, duration)
  • sinInOut(stateProp, endStateValue, duration)
  • expIn(stateProp, endStateValue, duration)
  • expOut(stateProp, endStateValue, duration)
  • expInOut(stateProp, endStateValue, duration)
  • circleIn(stateProp, endStateValue, duration)
  • circleOut(stateProp, endStateValue, duration)
  • circleInOut(stateProp, endStateValue, duration)
  • bounceIn(stateProp, endStateValue, duration)
  • bounceOut(stateProp, endStateValue, duration)
  • bounceInOut(stateProp, endStateValue, duration)
  • backIn(stateProp, endStateValue, duration)
  • backOut(stateProp, endStateValue, duration)
  • backInOut(stateProp, endStateValue, duration)
  • elasticIn(stateProp, endStateValue, duration)
  • elasticOut(stateProp, endStateValue, duration)
  • elasticInOut(stateProp, endStateValue, duration)

The above API returns Promise, so you can chain additinal processes by using then.

##Usage

Example 1. Use outside of component

var yourComponent = React.render(
    <YourComponent />,
    document.getElementById('demo')
)
var reactStateAnimation = new ReactStateAnimation(yourComponent)
// your component's state 'x' will be updated to 350 with linear order in 1 sec, then alpha will be 0 on end of moving
reactStateAnimation.linearInOut('x', 350/*end value*/, 1000/*duration(ms)*/).then(() => reactStateAnimation.linearInOut('alpha', 0, 400))

Example 2. Linear Move in React Component

Set any state (e.g. 'x') associated with position left style

import React from 'react'
import ReactStateAnimation from 'react-state-animation'

export default class Demo extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            x: 0
        }
        // react state animation wrapper
        this._animate = new ReactStateAnimation(this)
    }

    start() {
        // start animation
        this._animate.linearInOut('x', 350/*end value*/, 1000/*duration(ms)*/)
    }

    stop() {
        this._animate.stop()
    }

    getStyle() {
        return {
            position: 'absolute',
            backgroundColor: "#009688",
            top: 0,
            left: this.state.x + "px",
            width: this.props.width,
            height: this.props.height
        }
    }

    render() {
        return (
            <div style={this.getStyle()}></div>
        )
    }
}

Demo.defaultProps = {
    width: 50,
    height: 50
}

Example 3. Linear Move in React Canvas

Set any state (e.g. 'x') associated with position left style

import React from 'react'
import ReactCanvas from 'react-canvas'
import ReactStateAnimation from 'react-state-animation'

var Surface = ReactCanvas.Surface
var Group = ReactCanvas.Group
var Layer = ReactCanvas.Layer

export default class DemoCanvas extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            x: 0
        }
        // react state animation wrapper
        this._animate = new ReactStateAnimation(this)
    }

    start() {
        // start animation
        this._animate.linearInOut('x', 350/*end value*/, 1000/*duration*/)
    }

    stop() {
        this._animate.stop()
    }

    getGroupStyle() {
        return {
            position: 'absolute',
            backgroundColor: "#f4f4f4",
            top: 0,
            left: 0,
            width: this.props.canvasWidth,
            height: this.props.canvasHeight
        }
    }

    getStyle() {
        return {
            position: 'absolute',
            backgroundColor: "#009688",
            top: 0,
            left: this.state.x,
            width: this.props.width,
            height: this.props.height
        }
    }

    render() {
        return (
            <Surface ref="surface" top={0} left={0} width={this.props.canvasWidth} height={this.props.canvasHeight} enableCSSLayout={true}>
               <Group style={this.getGroupStyle()}>
                    <Layer style={this.getStyle()} />
                </Group>
            </Surface>
        )
    }
}

DemoCanvas.defaultProps = {
    canvasWidth: 400,
    canvasHeight: 50,
    width: 50,
    height: 50
}

Example 4. Multiple states in ReactART

Set any state (e.g. 'x') associated with position left style


import React from 'react'
import ReactART from 'react-art'
import Circle from 'react-art/lib/Circle.art.js'
import ReactStateAnimation from 'react-state-animation'

var Surface = ReactCanvas.Surface

export default class DemoCanvas extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            x: props.position.x,
            y: props.position.y,
            radius: props.radius 
        }
        // react state animation wrapper
        this._animate = new ReactStateAnimation(this)
    }
    
    _Floating() {
        // pass an array to the manimate method with the states, to be animated
        this._animate.manimate([
            /* state: 'theNameOfTheState', target: endValue */
            {state: 'x', target: this.props.position.x-15},
            {state: 'radius', target: this.props.radius+4}
        ], 800, 'elasticInOut')
        .then(() => this._animate.manimate([
            {state: 'x', target: this.props.position.x},
            {state: 'radius', target: this.props.radius-2}
        ], 800, 'elasticInOut'))
        .then(() => this._Floating());
    }
    
    render() {
        return (
            <Surface ref="surface" top={0} left={0} width={this.props.canvasWidth} height={this.props.canvasHeight}>
               <Circle radius={this.state.radius} fill="#fca500" x={this.state.x} y={this.state.y}  />
            </Surface>
        )
    }
}

DemoCanvas.defaultProps = {
    canvasWidth: 400,
    canvasHeight: 300,
    radius: 30,
    position: {
        x: 50,
        y: 50
    }
}

Note

React setState is now asynchronously called as a batch. So, using regular instance properties instaed of state seems faste especially for React Canvas.

Please check the demo for canvas performance between React Canvas with setState (asynchronous and batch) and without setStates

Development

  1. Run "npm install"
  2. Run "gulp"
  3. Access to "http://localhost:8080/html/"