New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

game-timer

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

game-timer

Minimal fixed step game timer based on requestAnimation frame.

  • 2.0.0-1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Game Timer

A tiny (640 bytes), powerful game timer that provides a fixed timestep and abstracts away requestAnimationFrame and cancelAnimationFrame.

Usage

  1. Install
npm install game-timer
  1. Include Timer function.
import { createTimer } from "game-timer";
  1. Initialize by configuring with an update callback and a render callback.
const timer = createTimer({
  update: (deltaTime) => {
    myGame.update(deltaTime);
  },
  render: () => {
    drawGame(myGame, canvas);
  },
});
  1. Start timer. Your update callback will be called with a fixed time step in seconds as time progress and your render callback will be called when the browser is ready to render a another frame.
timer.start();
  1. Stop if needed. This will also stop rendering callback from being emitted.
timer.stop();

Configure

Beyond the update and render callback GameTimer takes a second argument which will be used as the time step. The default time step is 1/120th of a second (0.008333333333333333 seconds).

const timer = createTimer({
    update: ...,
    render: ...,
}, 1/320);

A lower time step makes simulations more accurate but requires more CPU. Changing time step after game logic has been establish might affect simulations.

Why fixed time step?

In order to avoid locking game simulation to frame rate, this game timer keeps track of passed time and emits a fixed time to your game update logic. It also prevents skipping by enforcing every simulation tick to be computed. This prevents glitches like objects falling thru collision zones and make simulations deterministic.

Further explanation can be found in this video.

Keywords

FAQs

Package last updated on 04 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc