@wapps/react-animation-frame

0.0.12 • Public • Published

react-animation-frame

Build Status

React higher-order component for managing recurring animation frames

Installation

npm install --save @wapps/react-animation-frame

How to use

import React, { Component } from 'react';
import withAnimationFrame from '@wapps/react-animation-frame';

class Loop extends Component {
  constructor(props) {
    super(props);

    this.nodeRef = React.createRef();
    this.startTime = performance.now();
  }

  onAnimationFrame(time) {
    const { duration } = this.props;
    const progress = ((time - this.startTime) / duration) * 100;

    if (progress >= 100) {
      this.startTime = performance.now();
      return;
    }

    const node = this.nodeRef.current;
    node.style.width = `${progress}%`;
  }

  render() {
    return (
      <div
        style={{ backgroundColor: 'blue', color: 'white' }}
        ref={this.nodeRef}
      >
        Loop
      </div>
    );
  }
}

export default withAnimationFrame(Loop);

Live Demo

For a demo, check out https://hupe1980.github.io/wapps-components/

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @wapps/react-animation-frame

Weekly Downloads

0

Version

0.0.12

License

MIT

Unpacked Size

6.85 kB

Total Files

5

Last publish

Collaborators

  • hupe1980