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

simple-gameloop

v2.1.0

Published

A simple canvas gameloop with a fixed update rate and dynamic render rate

Downloads

22

Readme

Simple Gameloop

A small library for running a simulation with a fixed update rate and a variable render rate.

The update function will be run at the set rate (times per second). The render function is tied to requestAnimationFrame and so runs when the browser says it can.

The update function is called inside the requestAnimationFrame call, but only when enough time has passed (tracked via an accumulator) since the last time update was called. This ensures that the update function is called at a set rate, regardless of how frequently requestAnimationFrame runs.

See changelog.md for changes.

To Use

import * as simpleGameloop from 'simple-gameloop';

/**
 * @param dt - time in seconds since last call to update function
 */
const update = function update(dt) {
  // your update logic here
};

const render = function render() {
  // your render logic here
};

simpleGameloop.createLoop({
  update,
  render,
});

Create Canvas

There is also a helper function for creating a canvas element of a given size that returns useful references.

const canvasObj = simpleGameloop.createCanvas({
    containerSelector: '.canvasContainer',
    classes: 'canvas other-class',
    width: 500,
    height: 500,
});

This will create a 500x500 canvas inside the .canvasContainer element with the classes canvas and other-class.

canvasObj contains references to:

  • element the result of the document.createElement('canvas') call.
  • context the canvas context (e.g. element.getContext('2d')).
  • canvas a reference to context.canvas.

Loop Example

These files are included in the example directory in the repo.

Run them with npm run example.

index.html

<!doctype html>
<html>
<head>
  <title>Simple Gameloop Example</title>
</head>
<body>
  <canvas></canvas>
  <script src="index.ts"></script>
</body>
</html>

index.ts

import * as simpleGameloop from '../dist';

const ctx = document.querySelector('canvas')?.getContext('2d');
if (!ctx) {
  throw new Error('Canvas Missing');
}

ctx.canvas.style.cssText = 'border: 1px solid black';

const square = {
  x: 10,
  y: 40,
  width: 30,
  height: 20,
};

/**
 * Update the state of the scene.
 *
 * @param dt - time in seconds since last call to update function
 */
const update = function update(dt: number) {
  // Move square right at 50 pixels per second.
  square.x += 50 * dt;

  // Wrap the square back to the left edge if it goes off the right edge.
  square.x -= square.x > ctx.canvas.width ? ctx.canvas.width : 0;
};

/**
 * Draw the scene.
 */
const render = function render() {
  // Reset the canvas.
  ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);

  // Draw square.
  ctx.fillRect(square.x, square.y, square.width, square.height);

  // Draw square again if it is transitioning from right edge to left edge.
  if (square.x + square.width > ctx.canvas.width) {
    ctx.fillRect(square.x - ctx.canvas.width, square.y, square.width, square.height);
  }
};

simpleGameloop.createLoop({
  update,
  render,
  // Optionally pass in the canvas context and `fps: true` to render the FPS on the canvas.
  ctx,
  fps: true,
});

To Do

createLoop should return an object with the following methods:

  • pause to stop calling the update function.
  • play to start calling the update function.
  • setUpdateRate if you want to set how many times the update function is called per second. Default is 60.
  • setSpeed allows you to set the speed of the simulation. Set below 1 for slow motion. Default is 1.