I am building a breakout game using jQuery and the <canvas>
element. To control the paddle, it will follow the mouse. So I need to get the mouse co-ordinates in my drawPaddle()
method.
Looking on the jQuery site it has examples like this:
$(document).mousemove(function(e){
$('#status').html(e.pageX +', '+ e.pageY);
});
Which does what I want, but I don't want to add another function to my project like this. This is my drawPaddle()
method:
function drawPaddle() {
c.beginPath();
c.rect(paddleX, paddleY, 150, 10);
c.closePath();
c.fill();
// Get mouse position and update paddleX and paddleY here
}
So I need to find a way to get the mouse position whenever the drawPaddle()
is called, and not whenever the mouse is moved on page like $(document).mousemove(function(e){
does.
Any help is appreciated, thanks.