Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
    <style>
        canvas {
            border:1px solid #444;
        }
    </style>
  <!--[if IE]><script type="text/javascript" src="http://jimmyinteractive.com/games/Lib/excanvas.js"></script><![endif]-->
  
</head>
<body onload="load();">
  <h1>Bounce an Image</h1>
  <p>We will now bounce our image around the screen. Follow the steps below</p>
  
  <ol>
    <li>Instead of updating the x/y by 1 each time we will update the x/y by our X velocity(direction) and our Y velocity(direction).  These are saved in the xVol and yVol varialbes. 
      <pre>
     //todo: update position
      x= x + xVol;
      y= y + yVol;</pre>
    </li>
    
     <li>when we hit a wall we need to change our velocity to go the other way.  When we hit the far wall we will go backwards by changing the velocity to a negivty number, -1.
<pre>//todo: bounce off wall   
  if(x > 350) xVol = -3;
  if(x < 0) xVol = 3;
  if(y > 550) yVol = -1;
  if(y < 0) yVol = 1;
      </pre>
    </li>
       <li>Lets try something, comment out the ctx.clearRect function by placing two slashes in front of it.
      <pre>
     // ctx.clearRect(0, 0, canvas.width, canvas.height); </pre>
    </li>
     
 
    
  </ol>
 <div> x=<span id="xlable"></span>
   y=<span id="ylable"></span>
  </div>
  <canvas id="cv" width="400" height="600"></canvas>
 
 
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
jamesfdickinsonpro
0viewers