Skip to content

Instantly share code, notes, and snippets.

@luizbills
Last active July 11, 2025 17:28
Show Gist options
  • Save luizbills/1e0d2a864073e5c4c054f7c9a3feb59c to your computer and use it in GitHub Desktop.
Save luizbills/1e0d2a864073e5c4c054f7c9a3feb59c to your computer and use it in GitHub Desktop.
How to detect "double taps" (ou double click) in Litecanvas
litecanvas();
use(pluginDoubleTap)
const actor = {}
function init() {
actor.x = W/2
actor.y = H/2
// on "double tap" move our actor
listen('DoubleTap', (x, y) => {
actor.x = x
actor.y = y
})
}
function draw() {
cls(0)
circfill(actor.x, actor.y, 20, 4)
}
// the "double tap" plugin
function pluginDoubleTap(engine) {
const lastTap = new Map()
let timeoutId = 0
engine.listen('tapped', (x, y, id) => {
const maxInterval = 0.3
const maxDist = 16
if (!lastTap.has(id)) {
// store the first tap
lastTap.set(id, {x, y, t: T})
timeoutId = setTimeout(
() => lastTap.delete(id),
50 + maxInterval*1000
)
} else {
const prev = lastTap.get(id)
// check that no more than 300 ms have
// passed between the first and second tap
if (T - prev.t <= maxInterval && hypot(x - prev.x, y - prev.y) < maxDist) {
emit('DoubleTap', x, y)
}
lastTap.delete(id)
clearTimeout(timeoutId)
}
})
}
@luizbills
Copy link
Author

luizbills commented Jul 11, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment