Skip to content

Commit 0e83eed

Browse files
authored
refactor: update js to match webpage (#712)
The JavaScript in some files does not match the code blocks online and an earlier JavaScript file. I have refactored them for consistency to match the main [webpage](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_building_practice) on this, with Prettier adjusting indentation and quotation marks.
1 parent 1a5d8ea commit 0e83eed

File tree

2 files changed

+75
-78
lines changed

2 files changed

+75
-78
lines changed
Lines changed: 72 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// set up canvas
22

3-
const canvas = document.querySelector('canvas');
4-
const ctx = canvas.getContext('2d');
3+
const canvas = document.querySelector("canvas");
4+
const ctx = canvas.getContext("2d");
55

6-
const width = canvas.width = window.innerWidth;
7-
const height = canvas.height = window.innerHeight;
6+
const width = (canvas.width = window.innerWidth);
7+
const height = (canvas.height = window.innerHeight);
88

99
// function to generate random number
1010

@@ -19,89 +19,87 @@ function randomRGB() {
1919
}
2020

2121
class Ball {
22-
23-
constructor(x, y, velX, velY, color, size) {
24-
this.x = x;
25-
this.y = y;
26-
this.velX = velX;
27-
this.velY = velY;
28-
this.color = color;
29-
this.size = size;
30-
}
31-
32-
draw() {
33-
ctx.beginPath();
34-
ctx.fillStyle = this.color;
35-
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
36-
ctx.fill();
37-
}
38-
39-
update() {
40-
if ((this.x + this.size) >= width) {
41-
this.velX = -(Math.abs(this.velX));
42-
}
43-
44-
if ((this.x - this.size) <= 0) {
45-
this.velX = Math.abs(this.velX);
46-
}
47-
48-
if ((this.y + this.size) >= height) {
49-
this.velY = -(Math.abs(this.velY));
22+
constructor(x, y, velX, velY, color, size) {
23+
this.x = x;
24+
this.y = y;
25+
this.velX = velX;
26+
this.velY = velY;
27+
this.color = color;
28+
this.size = size;
29+
}
30+
31+
draw() {
32+
ctx.beginPath();
33+
ctx.fillStyle = this.color;
34+
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
35+
ctx.fill();
36+
}
37+
38+
update() {
39+
if (this.x + this.size >= width) {
40+
this.velX = -Math.abs(this.velX);
41+
}
42+
43+
if (this.x - this.size <= 0) {
44+
this.velX = Math.abs(this.velX);
45+
}
46+
47+
if (this.y + this.size >= height) {
48+
this.velY = -Math.abs(this.velY);
49+
}
50+
51+
if (this.y - this.size <= 0) {
52+
this.velY = Math.abs(this.velY);
53+
}
54+
55+
this.x += this.velX;
56+
this.y += this.velY;
57+
}
58+
59+
collisionDetect() {
60+
for (const ball of balls) {
61+
if (!(this === ball)) {
62+
const dx = this.x - ball.x;
63+
const dy = this.y - ball.y;
64+
const distance = Math.sqrt(dx * dx + dy * dy);
65+
66+
if (distance < this.size + ball.size) {
67+
ball.color = this.color = randomRGB();
68+
}
5069
}
51-
52-
if ((this.y - this.size) <= 0) {
53-
this.velY = Math.abs(this.velY);
54-
}
55-
56-
this.x += this.velX;
57-
this.y += this.velY;
58-
}
59-
60-
collisionDetect() {
61-
for (const ball of balls) {
62-
if (!(this === ball)) {
63-
const dx = this.x - ball.x;
64-
const dy = this.y - ball.y;
65-
const distance = Math.sqrt(dx * dx + dy * dy);
66-
67-
if (distance < this.size + ball.size) {
68-
ball.color = this.color = randomRGB();
69-
}
70-
}
71-
}
72-
}
73-
70+
}
71+
}
7472
}
7573

7674
const balls = [];
7775

7876
while (balls.length < 25) {
79-
const size = random(10,20);
80-
const ball = new Ball(
81-
// ball position always drawn at least one ball width
82-
// away from the edge of the canvas, to avoid drawing errors
83-
random(0 + size,width - size),
84-
random(0 + size,height - size),
85-
random(-7,7),
86-
random(-7,7),
87-
randomRGB(),
88-
size
89-
);
77+
const size = random(10, 20);
78+
const ball = new Ball(
79+
// ball position always drawn at least one ball width
80+
// away from the edge of the canvas, to avoid drawing errors
81+
random(0 + size, width - size),
82+
random(0 + size, height - size),
83+
random(-7, 7),
84+
random(-7, 7),
85+
randomRGB(),
86+
size
87+
);
9088

9189
balls.push(ball);
9290
}
9391

9492
function loop() {
95-
ctx.fillStyle = 'rgba(0, 0, 0, 0.25)';
96-
ctx.fillRect(0, 0, width, height);
93+
ctx.fillStyle = "rgba(0, 0, 0, 0.25)";
94+
ctx.fillRect(0, 0, width, height);
9795

98-
for (const ball of balls) {
99-
ball.draw();
100-
ball.update();
101-
ball.collisionDetect();
102-
}
96+
for (const ball of balls) {
97+
ball.draw();
98+
ball.update();
99+
ball.collisionDetect();
100+
}
103101

104-
requestAnimationFrame(loop);
102+
requestAnimationFrame(loop);
105103
}
106104

107105
loop();

javascript/oojs/bouncing-balls/main.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// setup canvas
22

3-
const canvas = document.querySelector('canvas');
4-
const ctx = canvas.getContext('2d');
3+
const canvas = document.querySelector("canvas");
4+
const ctx = canvas.getContext("2d");
55

66
const width = (canvas.width = window.innerWidth);
77
const height = (canvas.height = window.innerHeight);
88

99
// function to generate random number
1010

1111
function random(min, max) {
12-
const num = Math.floor(Math.random() * (max - min + 1)) + min;
13-
return num;
12+
return Math.floor(Math.random() * (max - min + 1)) + min;
1413
}
1514

1615
// function to generate random color

0 commit comments

Comments
 (0)