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

react-animation-frame

v1.0.2

Published

A React higher-order component for managing recurring animation frames

Downloads

10,953

Readme

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