arcade2d
Class

Mouse

input/mouse.ts:57

Game-scoped input sampler that tracks the canvas-local mouse position and the state of the three standard buttons (left, middle, right).

Mouse lives at the Game tier rather than on a World because mouse input is inherently page-global — DOM events fire whether a world is mounted or not, and the same physical cursor is shared across world swaps (menu → gameplay → game-over). The world-space projection of the cursor is layered on top by World.getMouseState which reads this snapshot and runs it through the active world's camera.

Most game code reads mouse state via the convenience accessors on the game or world — game.getMouseState() for screen space, world.getMouseState() for world space — rather than touching this component directly; the class itself is exported so that callers writing custom bootstrap, or users who want to swap in a custom input source (e.g. a recorded-input variant for tests), have something concrete to construct and register.

Snapshot semantics

The component follows the canonical "input sampler" pattern described in the World docblock, applied one tier up:

  1. DOM events update a private pending buffer as they arrive — this is asynchronous and can happen at any time relative to the engine's update tick.
  2. Mouse.onPreUpdate copies the pending buffer into a private snapshot once per game tick, before the active world's update phase runs.
  3. Mouse.getState returns a MouseSnapshot derived from the snapshot, so every component reading the mouse during a single tick sees the same screen-space position and the same button states.

Event sourcing

Listeners are attached during Mouse.onAdded and removed in Mouse.onDestroy:

  • mousemove and mousedown are listened on the canvas, so cursor tracking and press detection only fire while the pointer is over the game.
  • mouseup is listened on window, so releases that happen off the canvas (after dragging the cursor off the game area) still register and clear the held-button state. Without this, the button would appear stuck-down until the user came back over the canvas and released again.

The engine does not call event.preventDefault() on any of these — game code is free to do that itself if it wants to suppress browser defaults like right-click context menus or middle-click autoscroll.

Constructors

#
constructor(host: Game): Mouse

Parameters

Properties

#
enabled: boolean

Per-component gate on the three update hooks (onPreUpdate, onUpdate, onPostUpdate). When explicitly false, the engine skips all three for this component during the host's tick — useful for temporarily pausing behaviour (e.g. a freeze powerup) without removing the component and losing its internal state.

Does not gate onAdded or onDestroy; those always fire so a host can never end up with a half-attached component.

readonly #
host: Game

The host this component is attached to. Stored read-only; subclasses access it as this.host directly, or through the tier-appropriate aliases (this.world, this.game) on the per-tier abstract bases.

Accessors

readonly #
game: Game

The Game this component is attached to — identical to AbstractComponent.host at this tier, exposed under the game name so subclass code reads the same on every tier.

Methods

#
getState(): MouseSnapshot

Returns a fresh MouseSnapshot per call — game code may stash the returned object for the duration of a frame without worrying about mid-frame mutation. The contained Point for screenPosition is also a fresh clone.

Returns

MouseSnapshot
#
onAdded(): void

Lifecycle hook that is called when the component is added to the host object. Should not be called directly.

Returns

void
#
onDestroy(): void

Lifecycle hook that is called when the host object is destroyed. Should not be called directly.

Returns

void
#
onPreUpdate(): void

Returns

void
#
onUpdate(_update: WorldUpdate, _deps: Record): void

Lifecycle hook for the main update phase. Called once per world tick, after every component's onPreUpdate and before any onPostUpdate.

Parameters

Returns

void
ESC