SHIFT + D

Touchpress

Updated on September 20, 2024Source codeTests

createTouchpress is a factory that returns Recognizeable effects for recognizing touch presses.

A touch press starts with a touchstart event, and is denied by a touchend or touchcancel event.

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

Create touchpress

Call createTouchpress with these parameters to create your touchstart, touchcancel, and touchend effects:

Parameter
Type
Required
Description
options
Object
no
Options to customize the behavior of the touchstart, touchcancel, and touchend effects. See the Options section for more guidance.

Options

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

A function that is called when the touch starts.

onStart receives the touchpress hook API as its only parameter.

onMove
Function
undefined

A function that is called when the touch moves.

onMove receives the touchpress hook API as its only parameter.

onCancel
Function
undefined

A function that is called when the touch is canceled.

onCancel receives the touchpress hook API as its only parameter.

onEnd
Function
undefined

A function that is called when the touch is released.

onEnd receives the touchpress hook API as its only parameter.

Hook API

The hook API is an object that is passed to the onStart, onMove, onCancel, and onEnd 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 touchpress effects store data in the Recognizeable instance's metadata property.

Touchpress metadata includes all pointer metadata.

Using with Listenable

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

Here's an example of how to use Touchpress to nicely combine createTouchpress with Listenable:

import { Touchpress } from '@baleada/logic'

const touchpress = new Touchpress([touchpressOptions])

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

MousereleaseTouchrelease

Edit doc on GitHub

ON THIS PAGE

TouchpressCreate touchpressOptionsHook APIMetadataUsing with Listenable