Engine.js is a lightweight, zero-dependency 2D game engine that runs entirely in the browser. It wraps the HTML5 Canvas API with a small set of focused classes — giving you physics-enabled figures, sprite animation, audio, and click events without reaching for any external library. Load a single module script, and every building block you need is available on the globalDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/OmarMtya/enginejs-module/llms.txt
Use this file to discover all available pages before exploring further.
$g object.
The $g Global Namespace
Engine.js exposes its entire surface area through one property on window: $g. When index.js is loaded as an ES module, an immediately-invoked function attaches every class and engine function to window.$g, so you never need to import individual modules yourself.
$g is:
| Property | Type | Description |
|---|---|---|
$g.Figura | Class | A renderable game object |
$g.Transform | Class | Position, size, and colour data |
$g.Rigido | Class | Rigid-body physics component |
$g.Imagen | Class | Image/sprite asset wrapper |
$g.Sprite | Class | Sprite-sheet animation descriptor |
$g.Sonido | Class | Audio asset wrapper |
$g.InitCanvas | Function | Size canvas to a container element |
$g.InitCanvasStatic | Function | Size canvas to fixed dimensions |
$g.AgregarFigura | Function | Add a figure to the scene |
$g.Animar | Function | Start the requestAnimationFrame loop |
$g.DetenerAnimacion | Function | Stop the loop and restore the scene |
$g.Dibujar | Function | Draw one frame manually |
$g.DetectarClick | Function | Bind or unbind the canvas click listener |
The library’s class names are in Spanish —
Figura (figure), Transform (transform), Rigido (rigid body), Imagen (image), Sonido (sound). Method names are mixed: InitCanvas and AgregarFigura follow English or hybrid conventions. This is an intentional design choice by the author.Environment also exposes audioGeneral, a static property you can assign a global Audio element to if you need a background track managed outside of any individual figure’s Sonido component.
Figure Types
AFigura is the core renderable primitive. Three tipo values are supported:
circulo
A filled circle. Sized by
radio (radius) in its Transform. The engine uses arc() with fill() under the hood.cuadrado
A filled rectangle. Sized by
anchura (width) and altura (height) in its Transform. Rendered with fillRect().imagen
A bitmap drawn from an
Imagen object attached to the Transform. Pass a Sprite to the Imagen constructor to enable sprite-sheet animation.Physics System
Engine.js ships a simple but practical rigid-body system powered by theRigido class. Attach a Rigido instance to any Figura and it immediately participates in gravity and collision detection.
- Gravity — Each frame,
afectarGravedad()increments the figure’syposition byrigido.valorand slightly increases that value, producing an accelerating fall. The default gravity value mirrorsEnvironment.gravedad(9.8). - Floor detection —
tocandoFondo()clamps a figure to the bottom edge of the canvas and setsrigido.colision = true, preventing it from falling further. - Rigid-body collisions —
tocandoRigidos()detects overlap between figures that both have aRigidocomponent. When two figures collide, the upper figure stops and the lower figure inherits the incoming velocity. - Collision-free mode — Pass
sinColision: truetoRigidoto apply gravity without triggering collision checks — useful for projectiles or background particles.
Multimedia
Images & Sprites
TheImagen class wraps an HTML Image object and optionally pairs it with a Sprite descriptor for sprite-sheet animation. The Sprite constructor accepts the row, number of columns, frame height, frame width, and playback speed (in seconds). The engine advances frames automatically on each render tick.
Audio
TheSonido class wraps an HTML Audio element and controls when it plays via an activacion mode:
activacion | Behaviour |
|---|---|
'inicial' | Plays immediately when the animation loop starts; pauses when DetenerAnimacion is called. |
'colision' | Plays while the figure’s rigido.colision is true; pauses otherwise. |
'colisionInversa' | Plays while the figure is not in collision; pauses on impact. |
Animation Loop
The loop is driven by the browser’s nativerequestAnimationFrame. Calling $g.Animar() (which maps to IniciarAnimacion) does three things in order:
- Resolves any overlapping figures at startup.
- Takes a deep snapshot (
backup) of every figure’s current state. - Schedules the recursive
Stepfunction, which callsDibujar(false)on every frame — clearing the canvas, redrawing all figures, and advancing physics.
Scene Backup & Restore
Because a snapshot is captured the moment$g.Animar() is called, stopping the loop with $g.DetenerAnimacion() automatically restores every figure to its original position and state. This makes it straightforward to implement a “restart level” feature — simply call DetenerAnimacion() and then Animar() again.
Key Features at a Glance
Zero Dependencies
Engine.js is a single directory of vanilla ES modules. No bundler, no npm install — just a
<script type="module"> tag.Canvas-Native Rendering
Figures are drawn directly with the Canvas 2D API. You get
arc, fillRect, and drawImage without any abstraction overhead.Built-in Physics
Gravity, floor clamping, and rigid-body collision detection are included out of the box via the
Rigido component.Sprite Animation
Drive sprite-sheet animations at any playback speed with the
Sprite class — no extra setup required.Spatial Audio
Attach a
Sonido to any figure and choose whether audio triggers at loop start, on collision, or on separation.Click Events
Register a single
clickListener on the Environment and DetectarClick() routes canvas clicks to whichever figure was hit.Next Steps
Quickstart
Build and run your first Engine.js scene in under five minutes.
Environment
Learn how
InitCanvas, AgregarFigura, gravity, and global state work together.