A React Component that uses a game loop to animate another component
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.editorconfig
.eslintignore
.eslintrc
.gitignore
.npmignore
.travis.yml
LICENSE
README.md
package.json

README.md

DISCLAIMER

This library isn't production-ready. It needs testing.

React-MainLoop

Travis npm David David

A React Component that runs a game loop.

Why

I wanted a way to animate a canvas-based app with React ART.

How

Using Isaac Sukin's excellent MainLoop.js library.

To read more about game loops, you can read Isaac's excellent blog post or the Game Loop chapter of Robert Nystrom's book Game Programming Patterns.

Why isn't this a mixin?

I favour composition. See this blog post which explains much better than I could why this is favourable.

What's with using forceUpdate()?

Isn't that against best practice? Yes, but it gives me greater control of when render gets called in the loop cycle. Please raise an issue if you can think of a better way!

How to use

All examples use ES6 syntax.

import Animator from 'react-mainloop';

const TIMESTEP = 1000 / 60,
      MAX_FPS = 60;

const animate = new Animator(TIMESTEP, MAX_FPS);

const update = (delta) => {
  /* ... */
  return {
    context,
    props
  };
};

const MyAnimatedComponent = animate(MyComponent, update);

React.render(<MyAnimatedComponent />, document.getElementById('someID'));

API

import Animator from 'react-mainloop';

Animator

Creates a function that wraps a React component with an animator for the given FPS settings.

const animate = new Animator(timestep, maxFPS);

Params

  • timestep number optional - Sets how many milliseconds should be simulated by every run of update(). Default: 1000 / 60 (reference)
  • maxFPS number optional - Limit the maximum FPS. Default: 60 (reference)

Returns

  • animate() function - Used to animate a React component.

animate()

Wraps a given React component in an Animate component, which controls the props and context for the given component.

const MyAnimatedComponent = animate(Component, update);

Params

  • Component ReactComponent - The component to wrap and animate.
  • update() function - Called zero or more times per frame depending on the frame rate.
  • begin() function optional - Called once at the beginning of a frame.

Returns

  • AnimatedComponent ReactComponent - A normal React component, but animated!

begin()

User-supplied

Called once at the beginning of a frame. Useful for e.g. handling events since the previous frame.

See MainLoop.setBegin().

function begin() {
  /* ... */
}

update()

User-supplied

Runs updates (e.g. AI and physics).

The update() function should simulate anything that is affected by time. It can be called zero or more times per frame depending on the frame rate.

Both props and context can be returned.

See MainLoop.setUpdate().

function update(delta) {
  /* ... */
  return {
    context,
    props
  }
}

Params

  • delta number - The amount of time in milliseconds to simulate in the update.

Returns

  • context object - The next context properties to feed your component. This will be saved in context.animContext.
  • props object - The next set of props to feed your component.

See Introductin to Contexts in React.js

AnimatedComponent

Returned from animate(). A normal React component, but animated!

All supplied props will be passed through to the wrapped component.

<AnimatedComponent thisProp="getsPassedThrough" />

props

  • run boolean optional - Animate if true. Default: true

License

Eclipse Public License v1.0