Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
68 lines (61 sloc) 1.63 KB
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Gravity</title>
<link rel="stylesheet" href="../include/style.css">
</head>
<body>
<header>
Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
</header>
<canvas id="canvas" width="400" height="400"></canvas>
<aside>Press arrow keys.</aside>
<script src="../include/utils.js"></script>
<script src="./classes/ball.js"></script>
<script>
window.onload = function () {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
ball = new Ball(),
vx = 0,
vy = 0,
ax = 0,
ay = 0,
gravity = 0.02;
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
window.addEventListener('keydown', function (event) {
switch (event.keyCode) {
case 37: //left
ax = -0.1;
break;
case 39: //right
ax = 0.1;
break;
case 38: //up
ay = -0.1;
break;
case 40: //down
ay = 0.1;
break;
}
}, false);
window.addEventListener('keyup', function () {
ax = 0;
ay = 0;
}, false);
(function drawFrame () {
window.requestAnimationFrame(drawFrame, canvas);
context.clearRect(0, 0, canvas.width, canvas.height);
vx += ax;
vy += ay;
vy += gravity;
ball.x += vx;
ball.y += vy;
ball.draw(context);
}());
};
</script>
</body>
</html>