diff --git a/public/main.js b/public/main.js index 7a6e206..6868a45 100644 --- a/public/main.js +++ b/public/main.js @@ -397,15 +397,18 @@ return Math.max(min, Math.min(max, value)); } + function ballPositionFromEvent(event) { + return new Vector( + clamp(event.clientX - document.getElementById("gamecontainer").offsetLeft, BALL_RADIUS, playfieldWidth - BALL_RADIUS), + clamp(event.clientY - document.getElementById("gamecontainer").offsetTop, BALL_RADIUS, playfieldHeight - BALL_RADIUS) + ); + } function moveMouseCallback(event) { if (isGameOver) return; // move player var previousPosition = playerBall.outline.centre; - var newPosition = new Vector( - clamp(event.clientX - document.getElementById("gamecontainer").offsetLeft, BALL_RADIUS, playfieldWidth - BALL_RADIUS), - clamp(event.clientY - document.getElementById("gamecontainer").offsetTop, BALL_RADIUS, playfieldHeight - BALL_RADIUS) - ); + var newPosition = ballPositionFromEvent(event); playerBall.outline.centre = newPosition; playerBall.velocity = newPosition.sub(previousPosition).mul(0.1); } @@ -438,20 +441,14 @@ ballAddTimeout = window.setTimeout(ballAddTimeoutFunction, addMessage[1] * 1000); } - function startGame() { + function startGame(mouseEvent) { document.getElementById("music").currentTime = 0; document.getElementById("music").volume = 0.5; document.getElementById("music").play(); document.getElementById("playbutton").style.display = "none"; playerBall = new Ball( - new Circle( - new Vector( - canvas.width / 2 - BALL_RADIUS, - canvas.height / 2 - BALL_RADIUS - ), - BALL_RADIUS - ), + new Circle(ballPositionFromEvent(mouseEvent), BALL_RADIUS), new Vector(0, 0), "blue", 0