Skip to content

Commit 519e310

Browse files
committed
fix(theme): add touch event support for mobile character animation
Add touchstart, touchmove, and touchend listeners so finger interaction paints characters the same way as mouse on desktop.
1 parent 1de689d commit 519e310

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

docs/.vitepress/theme/components/CharacterCanvas.vue

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ function onMouseMove(e: MouseEvent) {
231231
mouseY = e.clientY
232232
}
233233
234+
function onTouchMove(e: TouchEvent) {
235+
const touch = e.touches[0]
236+
if (touch) {
237+
mouseX = touch.clientX
238+
mouseY = touch.clientY
239+
}
240+
}
241+
242+
function onTouchEnd() {
243+
mouseX = -9999
244+
mouseY = -9999
245+
}
246+
234247
function onMouseLeave() {
235248
mouseX = -9999
236249
mouseY = -9999
@@ -254,6 +267,9 @@ function start() {
254267
onMounted(() => {
255268
window.addEventListener('resize', onResize)
256269
window.addEventListener('mousemove', onMouseMove)
270+
window.addEventListener('touchstart', onTouchMove, { passive: true })
271+
window.addEventListener('touchmove', onTouchMove, { passive: true })
272+
window.addEventListener('touchend', onTouchEnd)
257273
document.addEventListener('mouseleave', onMouseLeave)
258274
nextTick(() => {
259275
requestAnimationFrame(() => start())
@@ -265,6 +281,9 @@ onUnmounted(() => {
265281
cancelAnimationFrame(animationId)
266282
window.removeEventListener('resize', onResize)
267283
window.removeEventListener('mousemove', onMouseMove)
284+
window.removeEventListener('touchstart', onTouchMove)
285+
window.removeEventListener('touchmove', onTouchMove)
286+
window.removeEventListener('touchend', onTouchEnd)
268287
document.removeEventListener('mouseleave', onMouseLeave)
269288
})
270289
</script>

0 commit comments

Comments
 (0)