Git Product home page Git Product logo

react-animation-frame's Introduction

React Animation Frame

Build Status Coverage Status npm version

A React higher-order component for invoking component repeating logic using requestAnimationFrame. It works in both the browser and React Native.

The module follows the CommonJS format, so it is compatible with Browserify and Webpack. It also supports ES5 and beyond.

Motivation

Take a look at the example project; this contains a Timer component that should animate a progress bar until the configured end time is surpassed:

'use strict';

const React = require('react');
const ReactAnimationFrame = require('react-animation-frame');

class Timer extends React.Component {
    onAnimationFrame(time) {
        const progress = Math.round(time / this.props.durationMs * 100);
        this.bar.style.width = `${progress}%`;

        if (progress === 100) {
            this.props.endAnimation();
        }
    }

    render() {
        return (
            <div className="timer">
                <p>{this.props.message}</p>
                <div className="timer__bar" ref={node => this.bar = node}></div>
            </div>
        );
    }
}

module.exports = ReactAnimationFrame(Timer);

The onAnimationFrame method will be called on each repaint, via requestAnimationFrame, by the higher-order ReactAnimationFrame component. Once the progress of our animation reaches 100%, we can kill the underlying loop using the endAnimation method passed to the props of the wrapped component.

The loop can also be throttled by passing a second parameter to ReactAnimationFrame, which represents the number of milliseconds that should elapse between invocations of onAnimationFrame:

module.exports = ReactAnimationFrame(Timer, 100);

Installation

npm i --save react-animation-frame

API

ReactAnimationFrame(Component[, throttleMs])

Wraps Component and starts a requestAnimationFrame loop. throttleMs if specified, will throttle invocations of onAnimationFrame by any number of milliseconds.

Inside a wrapped component

onAnimationFrame(timestamp, lastTimestamp)

Called on each iteration of the underlying requestAnimationFrame loop, or if the elapsed throttle time has been surpassed. timestamp is the same DOMHighResTimeStamp with which requestAnimationFrame's callback is invoked.

The previous timestamp is also passed, which can be useful for calculating deltas. For n calls, lastTimestamp will be the value of timestamp for call n - 1.

this.props.endAnimation()

Cancels the current animation frame and ends the loop.

this.props.startAnimation()

Function to restart the animation after it was ended by endAnimation.

Local development

Run npm i to install the dependencies.

  • npm run build - transpile the library
  • npm test - lints the code and runs the tests

react-animation-frame's People

Contributors

jamesseanwright avatar magnusdahlstrand avatar rosswarren avatar thomasjonas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-animation-frame's Issues

Failed to minify in non-browser environments

First off, thanks for this useful module!

I'm using create-react-app and Travis CI. When I try to run build on my local machine, it runs fine; but on Travis, I get the following error:

Failed to compile.
Failed to minify the code from this file: 
 	./node_modules/react-animation-frame/dist/AnimationFrameComponent.js:8 
Read more here: http://bit.ly/2tRViJ9

Unfortunately the error doesn't say more than that, but it points to this note in the create-react-app readme:

You may occasionally find a package you depend on needs compiled or ships code for a non-browser environment.
This is considered poor practice in the ecosystem and does not have an escape hatch in Create React App.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.