LittleJS

LittleJS - The Tiny Fast JavaScript Game Engine

![LittleJS Screenshot](/LittleJS/examples/logo.png) [![NPM Package][npm]][npm-url] [![Build Size][build-size]][build-size-url] [![NPM Downloads][npm-downloads]][npmtrends-url] [![DeepScan][deepscan]][deepscan-url] [![Discord][discord]][discord-url]

🚂 All aboard!

LittleJS is a fast lightweight open source HTML5 game engine for modern web development. Its small footprint is packed with a comprehensive feature set including hybrid rendering, physics, particles, sound effects, music, and input handling. The code is very clean and well documented with some fun examples to get you started. Choo-Choo!

🚀 Join the First Ever LittleJS Game Jam

The LittleJS Game Jam will take place From Nov 11 to Dec 12! Unleash your creativity and develop amazing games using the LittleJS game engine. 🕹️🎮 Sign up today and get more info about the jam on itch.io!

## [Demo](https://killedbyapixel.github.io/LittleJS/examples/starter/) | [Docs](https://killedbyapixel.github.io/LittleJS/docs) | [Trailer](https://youtu.be/chuBzGjv7Ms) | [Discord](https://discord.gg/zb7hcGkyZe) | [Tutorial](https://github.com/KilledByAPixel/LittleJS/blob/main/examples/breakoutTutorial/README.md) | [Quick Reference](https://github.com/KilledByAPixel/LittleJS/blob/main/reference.md)

LittleJS Screenshot

About LittleJS

LittleJS is a small but powerful game engine with many features and no dependencies.

✨ Graphics

🔊 Audio

🎮 Input

💥 Physics

🚀 Flexibility

🛠️ And more…

How to use LittleJS

To get started download the latest LittleJS package from GitHub or install via npm: npm install littlejsengine

You will need to run a local web server to run LittleJS games!

This is because web browsers just have protection from loading local files. You may see a console error like “The image element contains cross-origin data.” Don’t panic, it’s easy to fix! If you are using Visual Studio Code there is a Live Preview Extension that will handle this for you automatically. Another option is to setup a simple local web server like http-server via npm.

Learn how to make a simple game from scratch with The Breakout Tutorial.

LittleJS Engine Quick Reference Sheet - This cheat sheet can help you get started.

Watch this GitNation talk to hear more about LittleJS works and get some tips on how to use it.

Join our vibrant community on Discord to get help, share your projects, and collaborate with others!

Examples

These demos are for both learning and using as starter projects to create your own games.

Builds

To easily include LittleJS in your game, you can use one of the pre-built js files.

To rebuild the engine you must first run npm install to setup the necessary npm dependencies. Then call npm run build to build the engine.

The starter example project includes a node js file build.js that compresses everything into a tiny zip file using Google Closure, UglifyJS, and ECT Zip.

LittleJS Setup

To start LittleJS, you need to create these 5 functions and pass them to engineInit.

function gameInit()
{
    // called once after the engine starts up
    // setup the game
}

function gameUpdate()
{
    // called every frame at 60 frames per second
    // handle input and update the game state
}

function gameUpdatePost()
{
    // called after physics and objects are updated
    // setup camera and prepare for render
}

function gameRender()
{
    // called before objects are rendered
    // draw any background effects that appear behind objects
}

function gameRenderPost()
{
    // called after objects are rendered
    // draw effects or hud that appear above all objects
}

// Startup LittleJS Engine
engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost);

LittleJS Objects

LittleJS can be used as an object oriented system by extending the base class EngineObject with your own. This lightweight class provides many useful features including physics, collision, parent/child system, and sorted rendering. These objects are added to the global list of objects where they will automatically be updated and rendered until destroyed.

Here is a template you can use to make objects that behave however you want. See the examples for a complete demonstration.

class MyObject extends EngineObject 
{
    constructor(pos, size, tileInfo, angle)
    {
        super(pos, size, tileInfo, angle);
        // setup object
    }

    update()
    {
        // update object physics and position
        super.update(); 
    }

    render()
    {
        // draw object as a sprite
        super.render();
    }
}

Debugging

Debug builds of LittleJS have a special menu that can be opened by pressing the Esc key.

Games Made With LittleJS

Here are a few of the amazing games people made using LittleJS…

LittleJS Logo