Engine.js drives all motion and physics through aDocumentation 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.
requestAnimationFrame-based loop. Understanding how the loop starts, ticks, and stops — and what happens to your scene state along the way — lets you write predictable, glitch-free games.
How the loop works
When you call$g.Animar(), the engine runs IniciarAnimacion(), which first calls Inicializar() to prepare the scene, then snapshots the current figure list into Environment.backup and hands control to requestAnimationFrame.
Step calls Dibujar(false). Passing false means “draw and advance physics”. Passing true means “draw only” — the canvas is repainted without moving anything, which is ideal for rendering a static preview before the game starts.
The animando flag
The module-level animando variable is set to true the first time Dibujar runs and serves as a guard inside Dibujar itself. If you call Dibujar(false) directly before IniciarAnimacion() has been called, Inicializar() will run automatically on that first call. Once animando is true, subsequent calls to Dibujar(false) skip Inicializar().
The
animando guard lives inside Dibujar, not in IniciarAnimacion. Calling $g.Animar() a second time while the loop is already running will invoke Inicializar() again and schedule an additional requestAnimationFrame loop. Avoid calling $g.Animar() more than once without first calling $g.DetenerAnimacion().Initialization: Inicializar()
Before the first frame is painted, Inicializar() performs two jobs:
Overlap separation
A bubble-sort-style pass iterates all figures. If any two figures are already overlapping at scene start, the engine pushes the outer figure to the right along the X axis so they begin the simulation in a valid, non-intersecting state.
Frame counter: Environment.contador
Environment.contador increments by 1 on every call to Dibujar, regardless of whether physics are advancing. You can read this value at any point to implement time-based logic — for example, triggering an event after a fixed number of frames or measuring elapsed frames since a collision.
Environment.FPS defaults to 60 and is used in the gravity acceleration formula inside afectarGravedad():FPS directly alters how quickly gravity accumulates each frame.Static preview with Dibujar(true)
You can render the scene without starting physics by calling $g.Dibujar(true). This is useful for showing an initial game state while waiting for user input.
Stopping the loop: DetenerAnimacion()
$g.DetenerAnimacion() cancels the pending requestAnimationFrame, restores every figure from Environment.backup, resets animando to false, and pauses any 'inicial' sounds.
backup is a deep clone taken at the moment Animar() was called, stopping and restarting the loop effectively resets the entire scene to its initial state.
Complete start / stop example
The snippet below wires up a canvas, adds a falling box, and lets the player start or stop the simulation with buttons.y: 50 and is ready for another run the moment Start is pressed again.
Related pages
- Collision Detection — per-frame physics resolution
- Click Events — attaching interactivity to figures
- Environment reference — all static properties and methods
- Figura reference — figure construction and physics helpers