Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (78 sloc) 2.28 KB
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Bounce 3D</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>
<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(),
xpos = 0,
ypos = 0,
zpos = 0,
vx = Math.random() * 10 - 5,
vy = Math.random() * 10 - 5,
vz = Math.random() * 10 - 5,
fl = 250,
vpX = canvas.width / 2,
vpY = canvas.height / 2,
top = -100,
bottom = 100,
left = -100,
right = 100,
back = 100,
front = -100;
(function drawFrame () {
window.requestAnimationFrame(drawFrame, canvas);
context.clearRect(0, 0, canvas.width, canvas.height);
xpos += vx;
ypos += vy;
zpos += vz;
if (xpos + ball.radius > right) {
xpos = right - ball.radius;
vx *= -1;
} else if (xpos - ball.radius < left) {
xpos = left + ball.radius;
vx *= -1;
}
if (ypos + ball.radius > bottom) {
ypos = bottom - ball.radius;
vy *= -1;
} else if (ypos - ball.radius < top) {
ypos = top + ball.radius;
vy *= -1;
}
if (zpos + ball.radius > back) {
zpos = back - ball.radius;
vz *= -1;
} else if (zpos - ball.radius < front) {
zpos = front + ball.radius;
vz *= -1;
}
if (zpos > -fl) {
var scale = fl / (fl + zpos);
ball.scaleX = ball.scaleY = scale;
ball.x = vpX + xpos * scale;
ball.y = vpY + ypos * scale;
ball.visible = true;
} else {
ball.visible = false;
}
if (ball.visible) {
ball.draw(context);
}
}());
};
</script>
</body>
</html>