Engine

LittleJS - The Tiny Fast JavaScript Game Engine MIT License - Copyright 2021 Frank Force

Engine Features

  • Object oriented system with base class engine object
  • Base class object handles update, physics, collision, rendering, etc
  • Engine helper classes and functions like Vector2, Color, and Timer
  • Super fast rendering system for tile sheets
  • Sound effects audio with zzfx and music with zzfxm
  • Input processing system with gamepad and touchscreen support
  • Tile layer rendering and collision system
  • Particle effect system
  • Medal system tracks and displays achievements
  • Debug tools and debug rendering system
  • Post processing effects
  • Call engineInit() to start it up!

Classes

Color
EngineObject
Particle
ParticleEmitter
RandomGenerator
Timer
Vector2

Members

(static, constant) engineName :string

Name of engine

Type:
  • string
Default Value
  • LittleJS

(static) engineObjects :Array.<EngineObject>

Array containing all engine objects

Type:
  • Array.<EngineObject>

(static) engineObjectsCollide :Array.<EngineObject>

Array with only objects set to collide with other objects this frame (for optimization)

Type:
  • Array.<EngineObject>

(static, constant) engineVersion :string

Version of engine

Type:
  • string
Default Value
  • 1.14.22

(static) frame :number

Current update frame, used to calculate time

Type:
  • number

(static, constant) frameRate :number

Frames per second to update

Type:
  • number
Default Value
  • 60

(static) paused :boolean

Is the game paused? Causes time and objects to not be updated

Type:
  • boolean
Default Value
  • false

(static) time :number

Current engine time since start in seconds

Type:
  • number

(static, constant) timeDelta :number

How many seconds each frame lasts, engine uses a fixed time step

Type:
  • number
Default Value
  • 1/60

(static) timeReal :number

Actual clock time since start in seconds (not affected by pause or frame rate clamping)

Type:
  • number

Methods

(static) engineAddPlugin(updateopt, renderopt, glContextLostopt, glContextRestoredopt)

Add a new update function for a plugin

Parameters:
NameTypeAttributesDescription
updatePluginCallback<optional>
renderPluginCallback<optional>
glContextLostPluginCallback<optional>
glContextRestoredPluginCallback<optional>

(async, static) engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, imageSourcesopt, rootElementopt)

Startup LittleJS engine with your callback functions

Parameters:
NameTypeAttributesDefaultDescription
gameInitGameInitCallback

Called once after the engine starts up, can be async for loading

gameUpdateGameCallback

Called every frame before objects are updated (60fps), use for game logic

gameUpdatePostGameCallback

Called after physics and objects are updated, even when paused, use for UI updates

gameRenderGameCallback

Called before objects are rendered, use for drawing backgrounds/world elements

gameRenderPostGameCallback

Called after objects are rendered, use for drawing UI/overlays

imageSourcesArray.<string><optional>
[]

List of image file paths to preload (e.g., ['player.png', 'tiles.png'])

rootElementHTMLElement<optional>

Root DOM element to attach canvas to, defaults to document.body

Example
// Basic engine startup
 engineInit(
   () => { LOG('Game initialized!'); },  // gameInit
   () => { updateGameLogic(); },         // gameUpdate
   () => { updateUI(); },                // gameUpdatePost
   () => { drawBackground(); },          // gameRender
   () => { drawHUD(); },                 // gameRenderPost
   ['tiles.png', 'tilesLevel.png']       // images to load
 );
 

(static) engineObjectsCallback(posopt, sizeopt, callbackFunctionopt, objectsopt)

Triggers a callback for each object within a given area

Parameters:
NameTypeAttributesDefaultDescription
posVector2<optional>

Center of test area, or undefined for all objects

sizeVector2 | number<optional>

Radius of circle if float, rectangle size if Vector2

callbackFunctionObjectCallbackFunction<optional>

Calls this function on every object that passes the test

objectsArray.<EngineObject><optional>
engineObjects

List of objects to check

(static) engineObjectsCollect(posopt, sizeopt, objectsopt) → {Array.<EngineObject>}

Collects all object within a given area

Parameters:
NameTypeAttributesDefaultDescription
posVector2<optional>

Center of test area, or undefined for all objects

sizeVector2 | number<optional>

Radius of circle if float, rectangle size if Vector2

objectsArray.<EngineObject><optional>
engineObjects

List of objects to check

Returns:
  • List of collected objects
Type: 
Array.<EngineObject>

(static) engineObjectsDestroy()

Destroy and remove all objects

(static) engineObjectsRaycast(start, end, objectsopt) → {Array.<EngineObject>}

Return a list of objects intersecting a ray

Parameters:
NameTypeAttributesDefaultDescription
startVector2
endVector2
objectsArray.<EngineObject><optional>
engineObjects

List of objects to check

Returns:
  • List of objects hit
Type: 
Array.<EngineObject>

(static) engineObjectsUpdate()

Update each engine object, remove destroyed objects, and update time

(static) getPaused() → {boolean}

Get if game is paused

Returns:
Type: 
boolean

(static) setPaused(isPausedopt)

Set if game is paused

Parameters:
NameTypeAttributesDefaultDescription
isPausedboolean<optional>
true

Type Definitions

GameCallback()

GameInitCallback() → {void|Promise.<void>}

Returns:
Type: 
void | Promise.<void>

ObjectCallbackFunction(object)

Parameters:
NameTypeDescription
objectEngineObject

ParticleCallbackFunction(particle)

Parameters:
NameTypeDescription
particleParticle

PluginCallback()