Skip to content

Commit

Permalink
fix event on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
verdulife committed Feb 26, 2024
1 parent 0a1fd4e commit 116ad72
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/components/CursorHit.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
</style>

<script>
window.addEventListener("click", (ev) => {
function hitScreen(mouseX: number, mouseY: number) {
const sound = new Audio("/hit.wav")
const { clientX: mouseX, clientY: mouseY } = ev
const sprite = document.createElement("span")
const spriteCenter = 300 / 2
const randomSprite = Math.ceil(Math.random() * 2)
Expand All @@ -24,14 +23,29 @@
background-image: url("/sprite-${randomSprite}.png");
background-repeat: no-repeat;
background-position: 0 0;
filter: saturate(0.5);
animation: sprite 200ms steps(15, end);
animation: sprite 150ms steps(15, end);
z-index: 999;
pointer-events: none;
`

sound.play()
document.body.appendChild(sprite)
setTimeout(() => document.body.removeChild(sprite), 500)
})
setTimeout(() => document.body.removeChild(sprite), 200)
}

function isTouchDevice() {
return "ontouchstart" in window || navigator.maxTouchPoints > 0
}

if (isTouchDevice()) {
window.addEventListener("touchstart", (ev) => {
const { clientX: mouseX, clientY: mouseY } = ev.touches[0]
hitScreen(mouseX, mouseY)
})
} else {
window.addEventListener("click", (ev) => {
const { clientX: mouseX, clientY: mouseY } = ev
hitScreen(mouseX, mouseY)
})
}
</script>

0 comments on commit 116ad72

Please sign in to comment.