Skip to content

Instantly share code, notes, and snippets.

@mvasilkov
Last active August 8, 2022 13:30
Show Gist options
  • Save mvasilkov/1276235d5c76f01bd7a745b84094bb15 to your computer and use it in GitHub Desktop.
Save mvasilkov/1276235d5c76f01bd7a745b84094bb15 to your computer and use it in GitHub Desktop.

Hey hey people, Mark here, and today I wanted to share with you a very small library for js13k games called natlib. It consists of (refactored and improved!) parts lifted from my previous years' js13k submissions, some of those were in top-20 or something.

Included in this version are:

  • Fixed-step mainloop code that I use everywhere

    • Decouples world updates from rendering
    • Prevents floating-point error accumulation
    • Interpolate between the previous and the current state for smooth animation, especially noticeable when the rendering performance is low or inconsistent.
  • Mulberry32, a great little 32-bit PRNG

    • Created by Tommy Ettinger, the man, the legend!
    • Can be seeded for repeatable results
    • Has the full 32-bit cycle (2**32 iterations)
    • Comes with Fisher–Yates array shuffle, and uniform float in the closed range [-1, 1]
  • Pointer controls class

    • Handles mice and touch events, with proper coordinate translation
  • Viewport auto-scaling

    • Makes your game's canvas fit the browser window, preserving the aspect ratio.
    • Works on desktop and Android, iOS (but Safari is honestly in a rough shape)

git: https://github.com/mvasilkov/natlib
npm: https://www.npmjs.com/package/natlib

I'm trying really hard to make a library, NOT a framework or "game engine". As such, all of the components are fairly decoupled, it plays well with bundlers and tree-shaking.

I have monumental plans for this tiny project, so it'll enjoy unrelenting support.

A very small usage example is here: https://codepen.io/mvasilkov/pen/yLKEbyr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment