Engine.js provides a built-in click detection system that maps raw canvasDocumentation 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.
onclick events to individual figures. You provide a single callback — Environment.clickListener — and the engine calls it with whichever figure was under the cursor, handling all coordinate translation and shape-specific hit testing for you.
How click detection works
$g.DetectarClick(bindear) attaches (or removes) an onclick handler on Environment.canvasHTML — the raw <canvas> DOM element. When a click fires, the handler iterates every figure in Environment.figuras and calls detectarClickFigura({ x: event.offsetX, y: event.offsetY }, figura) from utilidades.class.js. The first figure that passes the hit test triggers Environment.clickListener(figura).
Hit testing: detectarClickFigura
The hit-testing logic differs by figure type:
| Shape | Test used |
|---|---|
'circulo' | The click point’s distance is tested against a bounding box extended by transform.radio in all directions. |
'cuadrado' / 'imagen' | Standard AABB: the click point must fall within [transform.x, transform.x + transform.altura] × [transform.y, transform.y + transform.anchura]. |
Setting the click listener
Assign your handler toEnvironment.clickListener before binding, then call $g.DetectarClick(true) to attach the onclick.
$g is Environment. index.js sets window.$g = Environment directly, so $g is not a wrapper object — it is the Environment class itself. There is no $g.Environment property. All static properties — including clickListener, figuras, contador, and FPS — are readable and writable directly as $g.clickListener, $g.figuras, etc.Unbinding clicks
Passfalse to DetectarClick to replace the onclick with an empty no-op function, effectively removing click handling without modifying the DOM element.
Using click events without the animation loop
Click detection is completely independent of the animation loop. You can bind the handler and render a single static frame with$g.Dibujar(true) — figures are visible and clickable without any physics running.
Full working example
The snippet below creates a clickable circle on a static canvas. No animation loop is needed.You clicked: myCircle.
Listening for clicks during the animation loop
DetectarClick works equally well when $g.Animar() is running. The onclick handler operates outside the requestAnimationFrame cycle, so clicks are processed independently of frame timing.
Related pages
- Animation Loop — starting and stopping the render loop
- Figures — constructing figures with
nombreandtipo - Environment reference —
clickListener,canvasHTML, and other static properties - Motor reference —
DetectarClickandDibujarsignatures