Skip to content

02-arkanoid-game Add Mouse Event for paddle #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions 02-arkanoid-game/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
function initEvents() {
document.addEventListener('keydown', keyDownHandler)
document.addEventListener('keyup', keyUpHandler)
document.onmousemove = moveMousePaddle;

function keyDownHandler(event) {
const { key } = event
Expand All @@ -230,6 +231,15 @@
}
}


function moveMousePaddle(event) {
const canv = canvas.getBoundingClientRect(); // Posición del canvas
const mousePosX = event.pageX - canv.left; // Posición del ratón en el canvas
// El 'paddle' solo se moverá si es dentro del canvas
if (mousePosX > 0 && mousePosX < (canvas.width - paddleWidth)) {
paddleX = mousePosX;
}
}
// a que velocidad de fps queremos que renderice nuestro juego
const fps = 60

Expand Down