Engine.js drives its canvas rendering through a central animation loop. Understanding how the loop starts, stops, and interacts with the editor UI is essential for both building scenes and extending the engine’s behavior. This page explains the lifecycle of a playback session, how to control it programmatically, and how canvas click detection ties into the system.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/OmarMtya/engine.js/llms.txt
Use this file to discover all available pages before exploring further.
The Animation Loop
The engine exposes three core rendering methods on the global$g object:
| Method | Description |
|---|---|
$g.Animar() | Starts the continuous animation loop. Each frame re-renders all figures and advances physics. |
$g.DetenerAnimacion() | Stops the running animation loop. The canvas freezes at the last rendered frame. |
$g.Dibujar() | Renders a single static frame. Does not start or stop an ongoing loop. |
$g.Animar() when you need live simulation and $g.Dibujar() when you only need to reflect a property change on the canvas without entering the full loop.
Calling
$g.Dibujar() while the animation loop is already running will not stop the loop — it simply draws one extra frame. Always call $g.DetenerAnimacion() first if you want to halt simulation.The Play/Pause Button Flow
The#play button in the header toggles between two states. The animando global boolean tracks which state is active. The full sequence for each transition is:
Editor enters play mode
The Attributes panel (
#atributos) is hidden so property values cannot be changed mid-simulation. The currently selected object is deselected and the hierarchy list is refreshed without click listeners so objects cannot be selected.Global audio plays and click detection is disabled
If a global background audio track has been loaded (
sonidoGlobal), it begins playing. Canvas click detection is then disabled so user interaction with the canvas is not misinterpreted as object selection.Press Pause
The
animando flag is set to false, the loop is stopped, and a static frame is drawn to freeze the canvas at its final simulation state.Programmatic Playback Control
You can trigger the same play/pause logic directly from code without relying on the button. The pattern mirrors what the#play click handler does:
animando in sync with your calls so that any code that checks the flag (such as the hierarchy list renderer) behaves correctly.
Canvas Click Detection
$g.DetectarClick() controls whether the engine listens for clicks on the canvas and maps them to figures.
The Click Listener Callback
When click detection is active,$g.clickListener is called with the Figura object that was clicked. You can override this callback to add custom selection logic:
Physics During Animation
When the animation loop is running, every figure that has arigido component attached will have gravity and collision simulated each frame. The rigido.valor property controls the gravity strength applied to that object. Objects without a rigido component remain stationary during playback.
Typical gravity values and their feel:
| Value | Effect |
|---|---|
0.1 | Very floaty — suitable for space or underwater scenes |
0.5 | Normal gravity — good general starting point |
1.0+ | Heavy, fast-falling objects |
$g.Animar() is active. Pausing stops all physics advancement immediately, and calling $g.Dibujar() renders the frozen state.