Skip to main content

Basic Mouse Input

info

Move the mouse to move the cube around. Press the mouse buttons to change the color of the cube.

Mouse handling in the PlayCanvas engine is provided by the Mouse object. The Mouse object provides a simple interface for detecting when the mouse is moved or when mouse buttons are pressed. It also removes some of the cross-browser inconsistencies with handling mouse co-ordinates.

Take a look at the tutorial project. Here is the code from mouse.js:

import {
Asset,
Script,
Vec3,
MOUSEBUTTON_LEFT,
MOUSEBUTTON_MIDDLE,
MOUSEBUTTON_RIGHT,
EVENT_MOUSEMOVE,
EVENT_MOUSEDOWN
} from 'playcanvas';

/**
* Mouse - Moves an entity based on mouse position and changes its material based on mouse clicks.
*/
export class Mouse extends Script {
static scriptName = 'mouse';

/**
* Red material to apply on left click.
*
* @attribute
* @title Red Material
* @type {Asset}
* @resource material
*/
redMaterial;

/**
* Green material to apply on middle click.
*
* @attribute
* @title Green Material
* @type {Asset}
* @resource material
*/
greenMaterial;

/**
* Blue material to apply on right click.
*
* @attribute
* @title Blue Material
* @type {Asset}
* @resource material
*/
blueMaterial;

initialize() {
/** @private */
this.pos = new Vec3();

this.app.mouse.disableContextMenu();

this.app.mouse.on(EVENT_MOUSEMOVE, this._onMouseMove, this);
this.app.mouse.on(EVENT_MOUSEDOWN, this._onMouseDown, this);
}

_onMouseMove(event) {
const cameraEntity = this.app.root.findByName('Camera');
const depth = 10;

if (cameraEntity?.camera) {
cameraEntity.camera.screenToWorld(event.x, event.y, depth, this.pos);
this.entity.setPosition(this.pos);
}
}

_onMouseDown(event) {
const materialAsset = this._getMaterialForButton(event.button);
if (materialAsset?.resource) {
this.entity.render.meshInstances[0].material = materialAsset.resource;
}
}

_getMaterialForButton(button) {
switch (button) {
case MOUSEBUTTON_LEFT:
return this.redMaterial;
case MOUSEBUTTON_MIDDLE:
return this.greenMaterial;
case MOUSEBUTTON_RIGHT:
return this.blueMaterial;
default:
return null;
}
}
}

Accessing the mouse​

Mouse control is managed by the Mouse object. The framework provides an instance of this on the application app which is available to all script objects as:

this.app.mouse

Disabling the right-click menu​

In the constructor for our script object we disable the right-click menu to stop it popping up when we click the right mouse button.

this.app.mouse.disableContextMenu();

Binding to events​

The Mouse object allows you to listen to different events corresponding to mouse actions. In the tutorial, we are binding the method _onMouseMove to the move event and _onMouseDown to the button down event.

Notice how we also pass this into the on() method for binding to events. This third argument is the object that is used as this in the event callback.

this.app.mouse.on(EVENT_MOUSEMOVE, this._onMouseMove, this);
this.app.mouse.on(EVENT_MOUSEDOWN, this._onMouseDown, this);

Events available on Mouse are:

  • EVENT_MOUSEUP - fires when a mouse button is released
  • EVENT_MOUSEDOWN - fires when a mouse button is pressed
  • EVENT_MOUSEMOVE - fires when the mouse is moved
  • EVENT_MOUSEWHEEL - fires when the mouse wheel is rotated.

Mouse input in browsers is usually implemented by listening to DOM events on elements in your page's DOM. The problem is that different browsers implement the events slightly differently and supply different values. In order to simplify the code you write the PlayCanvas engine allows you to bind your event handlers to the PlayCanvas mouse handler instead of directly the DOM Element. The engine supplies a MouseEvent object when the event fires which is consistent across all browsers. If you do need the original DOM event, it is available as the event property in MouseEvent.

Moving the mouse​

The first event handler is _onMouseMove. This is fired whenever the mouse moves. For an EVENT_MOUSEMOVE event, the MouseEvent object will have the current position of the mouse x and y and also the change in position since the last event in dx and dy. In our tutorial we're using the current position of the mouse and moving the cube to the cursor position.

Mouse buttons​

The second event handler is _onMouseDown. This is fired whenever one of the three mouse buttons is clicked. In the EVENT_MOUSEDOWN and EVENT_MOUSEUP events, the MouseEvent object will have a button property which contains the button that has been pressed/released. It can be one of the following values:

  • MOUSEBUTTON_NONE
  • MOUSEBUTTON_LEFT
  • MOUSEBUTTON_MIDDLE
  • MOUSEBUTTON_RIGHT

In our tutorial, we're changing the color of the cube depending on which mouse button was pressed.

Try it out​

Try the tutorial in full screen here or at the top of the page. Move the mouse to move the cube and click the left, middle and right mouse button to change the color of the cube.