HTML and CSS Reference
In-Depth Information
<canvas id="canvas" width="400" height="400"></canvas>
<script src="utils.js"></script>
<script src="ball3d.js"></script>
<script>
window.onload = function () {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
balls = [],
numBalls = 20,
fl = 250,
vpX = canvas.width / 2,
vpY = canvas.height / 2,
top = -200,
bottom = 200,
left = -200,
right = 200,
front = -200
back = 200;
for (var ball, i = 0; i < numBalls; i++) {
ball = new Ball3d(15);
ball.xpos = Math.random() * 400 - 200;
ball.ypos = Math.random() * 400 - 200;
ball.zpos = Math.random() * 400 - 200;
ball.vx = Math.random() * 5 - 1;
ball.vy = Math.random() * 5 - 1;
ball.vz = Math.random() * 5 - 1;
balls.push(ball);
}
function move (ball) {
ball.xpos += ball.vx;
ball.ypos += ball.vy;
ball.zpos += ball.vz;
//check boundaries
if (ball.xpos + ball.radius > right) {
ball.xpos = right - ball.radius;
ball.vx *= -1;
} else if (ball.xpos - ball.radius < left) {
ball.xpos = left + ball.radius;
ball.vx *= -1;
}
if (ball.ypos + ball.radius > bottom) {
ball.ypos = bottom - ball.radius;
ball.vy *= -1;
} else if (ball.ypos - ball.radius < top) {
ball.ypos = top + ball.radius;
ball.vy *= -1;
}
if (ball.zpos + ball.radius > back) {
ball.zpos = back - ball.radius;
ball.vz *= -1;
} else if (ball.zpos - ball.radius < front) {
Search WWH ::




Custom Search