touchevents
Subscribe to touch events and read active touch points via the Touch Events API.
Import: @rtorcato/browser-common/touchevents
๐ MDN: Touch Events ยท ๐ caniuse: touch
Exampleโ
import {
isTouchEventsAvailable,
onTouch,
getTouchPoints,
getTouchCount,
} from '@rtorcato/browser-common/touchevents'
if (isTouchEventsAvailable()) {
const off = onTouch(canvas, 'touchmove', (e) => {
const points = getTouchPoints(e)
points.forEach((p) => drawDot(p.x, p.y))
}, { passive: true })
canvas.addEventListener('touchstart', (e) => {
if (getTouchCount(e) === 2) startPinch(e)
})
off()
}
Exportsโ
isTouchEventsAvailable()โ feature checkonTouch(element, type, handler, options?)โ attaches a touch listener, returns an unsubscribe functiongetTouchPoints(event)โ{x, y}points for every currently active touch (event.touches)getTouchCount(event)โ number of currently active touches (event.touches.length)
See the API reference for full signatures.