SHIFT + D

Mousepress

Updated on September 20, 2024Source codeTests

createMousepress is a factory that returns Recognizeable effects for recognizing mouse presses.

A mouse press starts with a mousedown event, and is denied by a mouseup or mouseout event.

While the mouse is still down, createMousepress continuously recognizes the gesture at a rate of 60fps. The gesture is recognized when the mouse has been down for a minimum duration and has moved a minimum distance, both of which are configurable.

Create mousepress

Call createMousepress with these parameters to create your mousedown, mouseout, and mouseup effects:

Parameter
Type
Required
Description
options
Object
no
Options to customize the behavior of the mousedown, mouseout, and mouseup effects. See the Options section for more guidance.

Options

Option
Type
Default
Description
minDuration
number
0
The minimum millisecond duration of the mouse press, in milliseconds.
minDistance
number
0
The minimum pixel distance the mouse must move.
onDown
Function
undefined

A function that is called when the mouse is pressed down.

onDown receives the mousepress hook API as its only parameter.

onMove
Function
undefined

A function that is called when the mouse moves.

onMove receives the mousepress hook API as its only parameter.

onOut
Function
undefined

A function that is called when the mouse leaves the target.

onOut receives the mousepress hook API as its only parameter.

onUp
Function
undefined

A function that is called when the mouse is released.

onUp receives the mousepress hook API as its only parameter.

Hook API

The hook API is an object that is passed to the onDown, onMove, onOut, and onUp options as their only parameter. It contains the following properties:

Property
Type
Description
status
string
The status of the sequence. One of recognized, denied, or recognizing.
metadata
Object
Metadata about the sequence. See the Metadata section for more guidance.
sequence
Array
The sequence of events that have occurred so far.

Metadata

Your created mousepress effects store data in the Recognizeable instance's metadata property.

Mousepress metadata includes all pointer metadata.

Using with Listenable

The easiest way to use createMousepress with Listenable is to use the Mousepress class, which is a very minimal subclass of Listenable that handles configuration for you.

Here's an example of how to use Mousepress to nicely combine createMousepress with Listenable:

import { Mousepress } from '@baleada/logic'

const mousepress = new Mousepress([mousepressOptions])

mousepress.listen(() => {
  console.log(mousepress.metadata.duration)
})

KeyreleaseMouserelease

Edit doc on GitHub

ON THIS PAGE

MousepressCreate mousepressOptionsHook APIMetadataUsing with Listenable