HTML and CSS Reference
In-Depth Information
<canvas id="canvas" width="400" height="400"></canvas>
<script src="utils.js"></script>
<script src="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,
front = -100,
back = 100;
(function drawFrame () {
window.requestAnimationFrame(drawFrame, canvas);
context.clearRect(0, 0, canvas.width, canvas.height);
xpos += vx;
ypos += vy;
zpos += vz;
//check boundaries
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;
Search WWH ::




Custom Search