Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 789344 of Mouse controls

  • Revision slug: Games/Workflows/2D_Breakout_game_pure_JavaScript/Mouse_controls
  • Revision title: Mouse controls
  • Revision id: 789344
  • Created:
  • Creator: trevorh
  • Is current revision? No
  • Comment Remove iframe use

Revision Content

9. Mouse controls

This is the 9th step out of 10 of the Gamedev Canvas tutorial.

The game itself is actually finished, so let's work on polishing it up. We already have the keyboard controls, but we could easily add mouse controls too. It's even easier: all we need is the listener for the mouse move event:

document.addEventListener("mousemove", mouseMoveHandler, false);

We can update the paddle position based on the pointer coordinates, the handler function is doing exactly that:

function mouseMoveHandler(e) {
    var relativeX = e.clientX - canvas.offsetLeft;
    if(relativeX > 0 && relativeX < canvas.width) {
        paddleX = relativeX - paddleWidth/2;
    }
}

If the relativeX (the relative position of the pointer on the Canvas) is greater than zero and lower than the Canvas width, which means the pointer is within the Canvas boundaries, then the paddleX position (anchored on the left edge of the paddle) will be set to the relativeX value minus half the width of the paddle.

The paddle will follow the position of the mouse cursor, but we're restricting the movement to the size of the Canvas, so it won't disappear on either sides of it completely.

{{JSFiddleEmbed("https://jsfiddle.net/end3r/L3gngab5/","","320")}}

Exercise: adjust the boundaries of the paddle movement, so the whole paddle will be visible on both edges of the Canvas instead of only the half of it.

Now, you can get back to this tutorial's index page or continue to the last, tenth chapter: Finishing up.

Revision Source

<h2 id="9._Mouse_controls">9. Mouse controls</h2>

<div class="summary">
<p>This is the <strong>9th step</strong> out of 10 of the <a href="https://developer.mozilla.org/en-US/docs/Games/Workflows/Breakout_game_from_scratch">Gamedev Canvas tutorial</a>.</p>
</div>

<p>The game itself is actually finished, so let's work on polishing it up. We already have the keyboard controls, but we could easily add mouse controls too. It's even easier: all we need is the listener for the mouse move event:</p>

<pre class="brush: js">
document.addEventListener("mousemove", mouseMoveHandler, false);</pre>

<p>We can update the paddle position based on the pointer coordinates, the handler function is doing exactly that:</p>

<pre class="brush: js">
function mouseMoveHandler(e) {
    var relativeX = e.clientX - canvas.offsetLeft;
    if(relativeX &gt; 0 &amp;&amp; relativeX &lt; canvas.width) {
        paddleX = relativeX - paddleWidth/2;
    }
}</pre>

<p>If the <code>relativeX</code> (the relative position of the pointer on the Canvas) is greater than zero and lower than the Canvas width, which means the pointer is within the Canvas boundaries, then the <code>paddleX</code> position (anchored on the left edge of the paddle) will be set to the <code>relativeX</code> value minus half the width of the paddle.</p>

<p>The paddle will follow the position of the mouse cursor, but we're restricting the movement to the size of the Canvas, so it won't disappear on either sides of it completely.</p>

<p>{{JSFiddleEmbed("https://jsfiddle.net/end3r/L3gngab5/","","320")}}</p>

<div class="summary">
<p>Exercise: adjust the boundaries of the paddle movement, so the whole paddle will be visible on both edges of the Canvas instead of only the half of it.</p>
</div>

<p>Now, you can get back to <a href="https://developer.mozilla.org/en-US/docs/Games/Workflows/Breakout_game_from_scratch">this tutorial's index page</a> or continue to the last, tenth chapter: <a href="https://developer.mozilla.org/en-US/docs/Games/Workflows/Breakout_game_from_scratch/Finishing_up">Finishing up</a>.</p>
Revert to this revision