HTML Canvas Animation Move ball in two directions

Description

HTML Canvas Animation Move ball in two directions

View in separate window

<!doctype html>
<html lang="en">
<head>

</head>//from  ww w.  ja  va 2  s.c o m
<body> 
<canvas id="canvasOne" width="500" height="500">
 Your browser does not support the HTML 5 Canvas. 
</canvas>
<script type="text/javascript">
canvasApp();
function canvasApp() {
  function  drawScreen () {
    context.fillStyle = '#EEEEEE';
    context.fillRect(0, 0, theCanvas.width, theCanvas.height);
    //Box
    context.strokeStyle = '#000000'; 
    context.strokeRect(1,  1, theCanvas.width-2, theCanvas.height-2);
    
    ball.x += xunits;
    ball.y += yunits;
    
    context.fillStyle = "#000000";
    context.beginPath();
    context.arc(ball.x,ball.y,15,0,Math.PI*2,true);
    context.closePath();
    context.fill();
  
  }
  var speed = 5;
  var p1 = {x:20,y:20};
  var angle = 45;
  var radians = angle * Math.PI/ 180;
  var xunits = Math.cos(radians) * speed;
  var yunits = Math.sin(radians) * speed;
  var ball = {x:p1.x, y:p1.y};
  var points = new Array();
  
    theCanvas = document.getElementById('canvasOne');
  context = theCanvas.getContext('2d');
  
  function gameLoop() {
      window.setTimeout(gameLoop, 20);
      drawScreen()  
    }
    
  gameLoop();
  
  
}

</script>

</body>
</html>



PreviousNext

Related