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

circlet

v1.1.9

Published

A React-Redux game loop.

Downloads

7

Readme

Circlet

npm package

A React-Redux game loop.

Table of Contents

Introduction

Circlet is an experimental React-Redux game loop implemented mostly according to the excellent article, A Detailed Explanation of JavaScript Game Loops and Timing, by Isaac Sukin.

Installation

NPM Package

Circlet is available as an NPM package:

yarn add -D circlet react-redux redux

Cloning This Repository

Alternatively, you could also clone this repository to your project's source directory:

git clone https://github.com/honmanyau/circlet

Setting up Circlet

Automated Set-up

The NPM package includes a Node.js script (auto-setup.js) for automatically setting up Circlet with project that is newly created with create-react-app.

create-react-app awesome-game
cd awesome-game
yarn add -D circlet react-redux redux

# The script is meant to run from the project's root directory

cp node_modules/circlet/auto-setup.js .
node auto-setup.js

Connect Circlet to the Redux Store

import { createStore, combineReducers } from 'redux';
import { circletReducer as circlet } from 'circlet';



const reducer = combineReducers({ circlet, otherReducers });
const store = createStore(reducer);

export default store;

Initialise Circlet as a Component

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import Circlet from 'circlet';

import store from './store';
import App from './App';



ReactDOM.render(
  <Provider store={store}>
    <div>
      <Circlet />
      <App />
    </div>
  </Provider>,
  document.getElementById('root')
);

Usage

To include a function in the game loop that Circlet provides, use the subscribeToCirclet action creator:

import { connect } from 'react-redux';
import { subscribeToCirclet } from 'circlet';

class Asteroid extends React.Component {
  componentDidMount() {
    this.props.subscribeToCirclet(this.update);
  }

  update = () => {
    // Handle physics here
  }

  // ...
}

const mapDispatchToProps = (dispatch) => {
  return { subscribeToCirclet: (fn) => dispatch(subscribeToCirclet(fn)) }
}

export default connect(null, mapDispatchToProps)(Asteroid);

Circlet calls a subscribed function with two parameters, render and epsilon. As a single loop may contain multiple simulated frames, the render Boolean is used as a flag to indicate the end of simulation, and when it is appropriate to call this.setState() to trigger visual changes that depend on a component's state.

The second parameter, epsilon, the number of frames that has not been simulated in the last loop; it is always positive and is designed to be used for interpolation where applicable. The following example shows how render and epsilon could be used:

import { connect } from 'react-redux';
import { subscribeToCirclet } from 'circlet';

class Asteroid extends React.Component {
  constructor(props) {
    super(props);

    this.vx = 0;
    this.vy = 2;
    this.dx = 0;
    this.dy = 0;
    this.x = 100;
    this.y = 0;
    this.state = { vx: 0, vy: 2, x: 100, y: 0 }
  }

  componentDidMount() {
    this.props.subscribeToCirclet(this.subscription);
  }

  subscription = (render, epsilon) => {
    this.update(epsilon);

    if (render) {
      this.draw();
    }
  }

  update = (epsilon) => {
    const { vx, vy } = this;

    this.x += vx;
    this.y += vy;
    // dx and dy are used for interpolated rendering only
    this.dx = vx * epsilon;
    this.dy = vy * epsilon;
  }

  draw = () => {
    const { dx, dy, x, y } = this;

    this.setState({
      x: x + dx,
      y: y + dy
    });
  }

  // ...
}

const mapDispatchToProps = (dispatch) => {
  return { subscribeToCirclet: (fn) => dispatch(subscribeToCirclet(fn)) }
}

export default connect(null, mapDispatchToProps)(Asteroid);

API

<Circlet>

The Circlet React component is mandatory and is set up as described above in the section Setting up Circlet. Circlet's looping mechanism relies on the window.requestAnimationFrame() web API, and there only one instance of <Circlet> is necessary in a given application in which everything is animated at the same target frame rate.

The <Circlet> component accepts the following options as props:

  • targetFPS: the targetFPS props is an optional parameter sets the target FPS that Circlet will attempt to achieve; the default value of targetFPS is 60. This option is primarily meant for FPS throttling, as Circlet's looping mechanism depends on window.requestAnimationFrame(), which is generally only called as fast as the display refresh rate in a browser.
<Circlet targetFPS={30} />

subscribeToCirclet()

The functionality of subscribeToCirclet() is covered above in the Usage section. It should be noted that, depending on delta time (the time between two Circlet loops, which is also the time between two window.requestAnimationFrame()) and timestep size for simulation (1000 / targetFPS), a subscribed function may be called 0 or more time each loop.

In addition, the render flag, which is applied as an argument to any subscribed function that Circlet calls, is only set to true if at least 1 frame was simulated in the last loop.