HTML and CSS Reference
In-Depth Information
Multiple-object springing
Let's make another quick example to see this in action. Again, we'll go with the bubble-type reaction, but
this time, all bubbles can bounce off of each other. Figure 9-10 illustrates the effect.
Figure 9-10. Multiple-object collision
Here's the code in document 07-bubbles-2.html :
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Bubbles 2</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<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'),
balls = [],
numBalls = 10,
bounce = -0.5,
spring = 0.03,
gravity = 0.1;
for (var ball, i = 0; i < numBalls; i++) {
ball = new Ball(Math.random() * 30 + 20, Math.random() * 0xffffff);
ball.x = Math.random() * canvas.width / 2;
ball.y = Math.random() * canvas.height / 2;
ball.vx = Math.random() * 6 - 3;
ball.vy = Math.random() * 6 - 3;
balls.push(ball);
}
function checkCollision (ballA, i) {
 
Search WWH ::




Custom Search