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:
options
touchstart
, touchcancel
, and touchend
effects. See the Options section for more guidance.Options
minDuration
0
minDistance
0
onStart
undefined
A function that is called when the touch starts.
onStart
receives the touchpress
hook API as its only parameter.
onMove
undefined
A function that is called when the touch moves.
onMove
receives the touchpress
hook API as its only parameter.
onCancel
undefined
A function that is called when the touch is canceled.
onCancel
receives the touchpress
hook API as its only parameter.
onEnd
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:
status
recognized
, denied
, or recognizing
.sequence
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)
})