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-mainloop

v0.6.0

Published

A React Component that uses a game loop to animate another component.

Downloads

35

Readme

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