Last active
July 11, 2025 17:28
-
-
Save luizbills/1e0d2a864073e5c4c054f7c9a3feb59c to your computer and use it in GitHub Desktop.
How to detect "double taps" (ou double click) in Litecanvas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Live Demo