Options
All
  • Public
  • Public/Protected
  • All
Menu

Class MouseInput

Handles the mouse events and provdes an API for interacting with it easily. The simplest way to use it is:

  1. Create a new instance of MouseInput.
  2. Call registerListeners() passing the document.
  3. Set inputContainer to the canvas element created by PIXI.
  4. Call update at the end of every update frame of your game.
  5. Use the functions of this library in your code.

Different mouse positions

  • screen is the position of the mouse in the whole screen.
  • page is the position of the mouse inside the loaded webpage.
  • local is the position of the mouse relative to the inputContainer if defined, or to the element passed to registerListeners.
  • scaled is the position of the mouse inside the game, which is local modified by scaleProperties. It is mostly useful in games that scale up when their workspace is enlarged, as opposed to games that just reorganize their content and allow seeing more space inside.

In order to get the proper values for local it is recommended to set inputContainer to the canvas element used to render the game.

Scaled mouse position

In order to get the proper values for scaled you need to keep the scaleProperties in sync with the modifications you do to the rendering of the game. For example, a typical game imitating 8-bit aesthetics would likely have its own internal resolution and then would scale that up, maybe pixel-perfect or maybe not. For example: a game in resolution 320x240 scaled up proportionally to resolution 1920x1080:

  • It would scale 4.5 times, to 1440x1080.
  • It would be horizontally centered, so offset in X by 240px ( (1920 - 1440) / 2 )
  • So the scaleProperties would be `{scaleX: 4.5, scaleY: 4.5, offsetX: 240, offsetY: 0}

With these values scaledX/Y will return the proper position of the mouse in the stage.

How does it work:

See KeyboardInput for details.

Understanding mouse button states

Down, pressed and released for the mouse buttons work exactly the same as the keys in KeyboardInput, please refer to its documentation.

Hierarchy

  • MouseInput

Index

Constructors

constructor

Properties

Optional inputContainer

inputContainer: InputRelativeContainer

When provided, local mouse position will be calculated treating this as a reference. Most of the time it'll be the canvas element created by PIXI to render the game into. If the game does not begin in the top-left corner of the HTML page it has to be set for local mouse position to be calculated correctly.

scaleProperties

When viewport of the game changes there are two common solutions:

  1. Change the workspace of the game (ie. player sees more/less of the play area)
  2. Scale the game up (ie. everything get bigger/smaller)

scaleProperties is used in the second situation. When your game is scaled and possibly offset (when you prefer keeping the display ratio) inside its container local mouse positions will not take those changes into consideration while scaled mouse positions will, provided MouseInputRelativeScaleProps is set correctly.

Accessors

isAnyMouseDown

  • get isAnyMouseDown(): boolean

isAnyMousePressed

  • get isAnyMousePressed(): boolean

isAnyMouseReleased

  • get isAnyMouseReleased(): boolean

localX

  • get localX(): number

localY

  • get localY(): number

pageX

  • get pageX(): number

pageY

  • get pageY(): number

scaledX

  • get scaledX(): number

scaledY

  • get scaledY(): number

screenX

  • get screenX(): number

screenY

  • get screenY(): number

Methods

flush

  • flush(): void
  • Resets the internal state of this class to what it is during creation - nothing pressed, released or held down, positions reset. This won't affect scaleProperties and inputContainer.

    Returns void

handleContextMenuEvent

  • handleContextMenuEvent(event: Event): boolean

handleEvent

  • handleEvent(event: MouseEvent | PointerEvent): void
  • Handles a mouse event updating the internal status. This will be called automatically if you register the listeners via registerListeners. If you have listeners being registered somewhere else entirely and would like to keep it that way, you can manually call this method passing the MouseEvent/PointerEvent.

    Parameters

    • event: MouseEvent | PointerEvent

    Returns void

isMouseDown

isMousePressed

isMouseReleased

registerListeners

  • registerListeners(element: GlobalEventHandlers): void
  • Registers the mousemove, mosedown and mouseup listeners on the passed element so that the class can handle the input. This also registers an event that blocks right mouse button context menu from appearing.

    Parameters

    • element: GlobalEventHandlers

      To avoid issues with focus it's best to pass document here

    Returns void

update

  • update(): void
  • Updates the internal state, should be called at the end of each frame.

    Returns void

Generated using TypeDoc