Skip to content

Commit 867cfbb

Browse files
adding bouncy ball demo files
1 parent 8600507 commit 867cfbb

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Bouncing balls</title>
6+
<link rel="stylesheet" href="style.css">
7+
</head>
8+
9+
<body>
10+
<h1>bouncing balls</h1>
11+
<canvas></canvas>
12+
13+
<script src="main-finished.js"></script>
14+
</body>
15+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Bouncing balls</title>
6+
<link rel="stylesheet" href="style.css">
7+
</head>
8+
9+
<body>
10+
<h1>bouncing balls</h1>
11+
<canvas></canvas>
12+
13+
<script src="main.js"></script>
14+
</body>
15+
</html>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// setup canvas
2+
3+
var canvas = document.querySelector('canvas');
4+
var ctx = canvas.getContext('2d');
5+
6+
var width = canvas.width = window.innerWidth;
7+
var height = canvas.height = window.innerHeight;
8+
9+
// function to generate random number
10+
11+
function random(min,max) {
12+
var num = Math.floor(Math.random()*(max-min)) + min;
13+
return num;
14+
}
15+
16+
// define Ball constructor
17+
18+
function Ball() {
19+
this.x = random(0,width);
20+
this.y = random(0,height);
21+
this.velX = random(-7,7);
22+
this.velY = random(-7,7);
23+
this.color = 'rgb(' + random(0,255) + ',' + random(0,255) + ',' + random(0,255) +')';
24+
this.size = random(10,20);
25+
}
26+
27+
// define ball draw method
28+
29+
Ball.prototype.draw = function() {
30+
ctx.beginPath();
31+
ctx.fillStyle = this.color;
32+
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
33+
ctx.fill();
34+
}
35+
36+
// define ball update method
37+
38+
Ball.prototype.update = function() {
39+
if((this.x + this.size) >= width) {
40+
this.velX = -(this.velX);
41+
}
42+
43+
if((this.x - this.size) <= 0) {
44+
this.velX = -(this.velX);
45+
}
46+
47+
if((this.y + this.size) >= height) {
48+
this.velY = -(this.velY);
49+
}
50+
51+
if((this.y - this.size) <= 0) {
52+
this.velY = -(this.velY);
53+
}
54+
55+
this.x += this.velX;
56+
this.y += this.velY;
57+
}
58+
59+
// define ball collision detection
60+
61+
Ball.prototype.collisionDetect = function() {
62+
for(j = 0; j < balls.length; j++) {
63+
if( (!(this.x === balls[j].x && this.y === balls[j].y && this.velX === balls[j].velX && this.velY === balls[j].velY)) ) {
64+
var dx = this.x - balls[j].x;
65+
var dy = this.y - balls[j].y;
66+
var distance = Math.sqrt(dx * dx + dy * dy);
67+
68+
if (distance < this.size + balls[j].size) {
69+
balls[j].color = this.color = 'rgb(' + random(0,255) + ',' + random(0,255) + ',' + random(0,255) +')';
70+
}
71+
}
72+
}
73+
}
74+
75+
// define array to store balls
76+
77+
var balls = [];
78+
79+
// define loop that keeps drawing the scene constantly
80+
81+
function loop() {
82+
ctx.fillStyle = 'rgba(0,0,0,0.25)';
83+
ctx.fillRect(0,0,width,height);
84+
85+
while(balls.length < 25) {
86+
var ball = new Ball();
87+
balls.push(ball);
88+
}
89+
90+
for(i = 0; i < balls.length; i++) {
91+
balls[i].draw();
92+
balls[i].update();
93+
balls[i].collisionDetect();
94+
}
95+
96+
requestAnimationFrame(loop);
97+
}
98+
99+
100+
101+
loop();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// setup canvas
2+
3+
var canvas = document.querySelector('canvas');
4+
var ctx = canvas.getContext('2d');
5+
6+
var width = canvas.width = window.innerWidth;
7+
var height = canvas.height = window.innerHeight;
8+
9+
// function to generate random number
10+
11+
function random(min,max) {
12+
var num = Math.floor(Math.random()*(max-min)) + min;
13+
return num;
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
html, body {
2+
margin: 0;
3+
}
4+
5+
html {
6+
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
7+
height: 100%;
8+
}
9+
10+
body {
11+
overflow: hidden;
12+
height: inherit;
13+
}
14+
15+
h1 {
16+
font-size: 2rem;
17+
letter-spacing: -1px;
18+
position: absolute;
19+
margin: 0;
20+
top: -4px;
21+
right: 5px;
22+
23+
color: transparent;
24+
text-shadow: 0 0 4px white;
25+
}

0 commit comments

Comments
 (0)