Created
July 10, 2025 23:48
-
-
Save luizbills/e14689eb02e36cfab10ffb813a5df3aa to your computer and use it in GitHub Desktop.
How to detect "swipe" gesture 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(); | |
const taps = new Map() | |
const actor = {} | |
function init() { | |
actor.x = W/2 | |
actor.y = H/2 | |
// on "swipe" move our actor | |
listen('swipe', (angle, len) => { | |
actor.x += len/2 * cos(angle) | |
actor.y += len/2 * sin(angle) | |
}) | |
} | |
function draw() { | |
cls(0) | |
circfill(actor.x, actor.y, 64, 4) | |
} | |
function tap(x, y, id) { | |
// store the initial touch/click | |
taps.set(id, {x, y}) | |
} | |
function untap(x, y, id) { | |
const from = taps.get(id) | |
taps.delete(id) | |
const angle = atan2(y - from.y, x - from.x); | |
const length = hypot(x - from.x, y - from.y); | |
emit('swipe', angle, length) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Live Demo