Skip to content

Commit e969947

Browse files
committed
bouncing-balls setup
1 parent 9d96d12 commit e969947

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

bouncing-balls/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# BounceBalls
2+
3+
![BounceBalls](https://mdn.mozillademos.org/files/13865/bouncing-balls.png)
4+
5+
This page was given as a challenge in the MDN JS guide!
6+
To show how powerful JavaScript objects can be, we use the CanvasAPI and the requestAnimationFrameAPI method to create animations across the entire screen.
7+
8+
## Important Learning Points
9+
10+
- CanvasAPI Basics
11+
- Understanding the prototype in JS
12+
- Creating methods and properties

bouncing-balls/favicon/favicon.png

6.27 KB
Loading

bouncing-balls/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="pt-br">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Bouncing balls</title>
8+
<link rel="stylesheet" href="style.css" />
9+
<link rel="shortcut icon" href="favicon/favicon.png" type="image/x-icon" />
10+
</head>
11+
<body>
12+
<h1>Bouncing balls</h1>
13+
<canvas></canvas>
14+
<script src="main.js"></script>
15+
</body>
16+
</html>

bouncing-balls/main.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// setup canvas
2+
3+
const canvas = document.querySelector("canvas");
4+
const ctx = canvas.getContext("2d");
5+
6+
const width = (canvas.width = window.innerWidth);
7+
const height = (canvas.height = window.innerHeight);
8+
9+
// function to generate random number
10+
11+
function random(min, max) {
12+
const num = Math.floor(Math.random() * (max - min + 1)) + min;
13+
return num;
14+
}
15+
16+
// define Ball constructor
17+
18+
function Ball(x, y, velX, velY, color, size) {
19+
this.x = x;
20+
this.y = y;
21+
this.velX = velX;
22+
this.velY = velY;
23+
this.color = color;
24+
this.size = size;
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 (let j = 0; j < balls.length; j++) {
63+
if (!(this === balls[j])) {
64+
const dx = this.x - balls[j].x;
65+
const dy = this.y - balls[j].y;
66+
const distance = Math.sqrt(dx * dx + dy * dy);
67+
68+
if (distance < this.size + balls[j].size) {
69+
balls[j].color = this.color =
70+
"rgb(" +
71+
random(0, 255) +
72+
"," +
73+
random(0, 255) +
74+
"," +
75+
random(0, 255) +
76+
")";
77+
}
78+
}
79+
}
80+
};
81+
82+
// define array to store balls and populate it
83+
84+
let balls = [];
85+
86+
while (balls.length < 25) {
87+
const size = random(10, 20);
88+
let ball = new Ball(
89+
// ball position always drawn at least one ball width
90+
// away from the adge of the canvas, to avoid drawing errors
91+
random(0 + size, width - size),
92+
random(0 + size, height - size),
93+
random(-7, 7),
94+
random(-7, 7),
95+
"rgb(" + random(0, 255) + "," + random(0, 255) + "," + random(0, 255) + ")",
96+
size
97+
);
98+
balls.push(ball);
99+
}
100+
101+
// define loop that keeps drawing the scene constantly
102+
103+
function loop() {
104+
ctx.fillStyle = "rgba(0,0,0,0.50)";
105+
ctx.fillRect(0, 0, width, height);
106+
107+
for (let i = 0; i < balls.length; i++) {
108+
balls[i].draw();
109+
balls[i].update();
110+
balls[i].collisionDetect();
111+
}
112+
113+
requestAnimationFrame(loop);
114+
}
115+
116+
loop();

bouncing-balls/style.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
html,
2+
body {
3+
margin: 0;
4+
}
5+
6+
html {
7+
font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;
8+
height: 100%;
9+
}
10+
11+
body {
12+
overflow: hidden;
13+
height: inherit;
14+
}
15+
16+
h1 {
17+
font-size: 2rem;
18+
letter-spacing: -1px;
19+
position: absolute;
20+
margin: 0;
21+
top: -4px;
22+
right: 5px;
23+
color: transparent;
24+
text-shadow: 0 0 1px white;
25+
}

0 commit comments

Comments
 (0)