Engine.js requires no build tools and no package manager. All you need is a browser that supports ES modules and a copy of theDocumentation 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.
enginejs-module directory. The steps below walk you through creating an HTML page, sizing a canvas, adding a physics-enabled square, and running the animation loop — from zero to a falling box in about five minutes.
Include Engine.js
Load
index.js as an ES module script in your HTML page. The moment the module executes, it attaches every Engine.js class and function to window.$g, making the global available to any other script on the page.The
type="module" attribute is required. ES module scripts are deferred by default, so Engine.js will finish loading before any subsequent type="module" scripts run — you can safely reference $g in a module script that appears after this tag.Set Up the HTML
Create a container element that defines the play area dimensions, then place a The container can be any block element. Its CSS
<canvas> element inside it. Engine.js reads the container’s offsetWidth and offsetHeight to size the canvas automatically when you call InitCanvas.width and height values (resolved as offsetWidth / offsetHeight) become Environment.anchura and Environment.altura respectively, which the physics system uses as the floor boundary.Initialise the Canvas
Call
$g.InitCanvas(canvas, container) with references to the two DOM elements. This stores the 2D rendering context in Environment.canvas, records the canvas HTML element in Environment.canvasHTML, and sets the canvas width and height attributes to match the container.Create a Figure
Figures are the core objects that Engine.js renders and simulates. Construct a
$g.Figura with a tipo string, a $g.Transform that describes its position and appearance, and an optional $g.Rigido to opt into gravity and collisions. Finish by registering it with $g.AgregarFigura.| Property | Description |
|---|---|
tipo | Shape type: 'cuadrado' (rectangle), 'circulo' (circle), or 'imagen' (bitmap). |
transform.x / transform.y | Top-left origin of the figure in canvas pixels. |
transform.anchura / transform.altura | Width and height of the rectangle. For circles, use radio (radius) instead. |
transform.relleno | Fill colour as any CSS colour string. |
rigido | A Rigido() instance enables gravity (default valor: 9.8) and collision detection. |
You can give a figure an optional
nombre (name) and id for later lookup. If omitted, Engine.js auto-generates both. Figures are stored in Environment.figuras in insertion order.Start the Animation Loop
Call
$g.Animar() to begin the requestAnimationFrame loop. Before the first frame is drawn, Engine.js resolves any overlapping figures and takes a deep snapshot of every figure’s state. That snapshot is restored the moment you call $g.DetenerAnimacion(), so the scene resets exactly to how it looked when Animar() was called.Full Working Example
The listing below combines all five steps into a single self-contained HTML file. Drop it next to yourenginejs-module directory, open it in a browser, and you will see a purple square fall under gravity and come to rest on the canvas floor.
Press R at any time to call
DetenerAnimacion() followed by Animar(), resetting the scene. This demonstrates the built-in snapshot/restore mechanism at work.What to Explore Next
Environment
Explore
InitCanvas, AgregarFigura, gravity settings, the frame counter, and the click listener API.Figures
Learn all three figure types in depth — circles, rectangles, and image/sprite figures.
Animation Loop
Understand the
Animar / DetenerAnimacion lifecycle, manual Dibujar calls, and the frame backup system.