Skip to main content

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 check
  • onTouch(element, type, handler, options?) โ€” attaches a touch listener, returns an unsubscribe function
  • getTouchPoints(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.