I’m creating a code where 3 balls will bounce when a button is clicked. I have a code I found in the internet and added the start button but clicking it multiple times speed up the balls. Also, I tried adding a reset button that will clear out the canvas but can’t make it work. Sharing both the HTML and JS code.
JavaScript
x
61
61
1
const canvas = document.getElementById('myCanvas');
2
const ctx = canvas.getContext('2d');
3
const width = canvas.width = 1000;
4
const height = canvas.height = 500;
5
ctx.fillStyle = 'grey';
6
ctx.fillRect(0, 0, width, height);
7
8
function random(min, max) {
9
const num = Math.floor(Math.random() * (max - min + 1)) + min;
10
return num;
11
}
12
13
function Ball(x, y, velX, velY, color, size) {
14
this.x = x;
15
this.y = y;
16
this.velX = velX;
17
this.velY = velY;
18
this.color = color;
19
this.size = size;
20
}
21
Ball.prototype.draw = function() {
22
ctx.beginPath();
23
ctx.fillStyle = this.color;
24
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
25
ctx.fill();
26
}
27
Ball.prototype.update = function() {
28
if ((this.x + this.size) >= width) {
29
this.velX = -(this.velX);
30
}
31
if ((this.x - this.size) <= 0) {
32
this.velX = -(this.velX);
33
}
34
if ((this.y + this.size) >= height) {
35
this.velY = -(this.velY);
36
}
37
if ((this.y - this.size) <= 0) {
38
this.velY = -(this.velY);
39
}
40
this.x += this.velX;
41
this.y += this.velY;
42
}
43
let balls = [];
44
45
function loop() {
46
while (balls.length < 3) {
47
let size = 35;
48
let ball = new Ball(
49
random(0 + size, width - size), random(0 + size, height - size), 5, 5, 'yellow', size);
50
balls.push(ball);
51
}
52
ctx.fillStyle = 'grey';
53
ctx.fillRect(0, 0, width, height);
54
for (let i = 0; i < balls.length; i++) {
55
balls[i].draw();
56
balls[i].update();
57
}
58
requestAnimationFrame(loop);
59
}
60
61
document.getElementById('Begin').addEventListener('click',loop);
JavaScript
1
2
1
<input type='button' id='Begin' value='start'>
2
<canvas id='myCanvas'></canvas>
Advertisement
Answer
You need to store the requestAnimationFrame in a var and use cancelAnimationFrame to stop
I added a div to hold the buttons and now delegate from that div so only one event handler is needed.
JavaScript
1
82
82
1
const canvas = document.getElementById('myCanvas');
2
const ctx = canvas.getContext('2d');
3
const width = canvas.width = 1000;
4
const height = canvas.height = 500;
5
ctx.fillStyle = 'grey';
6
ctx.fillRect(0, 0, width, height);
7
let rq; // this we can use to request and cancel
8
9
10
function random(min, max) {
11
const num = Math.floor(Math.random() * (max - min + 1)) + min;
12
return num;
13
}
14
15
function Ball(x, y, velX, velY, color, size) {
16
this.x = x;
17
this.y = y;
18
this.velX = velX;
19
this.velY = velY;
20
this.color = color;
21
this.size = size;
22
}
23
Ball.prototype.draw = function() {
24
ctx.beginPath();
25
ctx.fillStyle = this.color;
26
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
27
ctx.fill();
28
}
29
Ball.prototype.update = function() {
30
if ((this.x + this.size) >= width) {
31
this.velX = -(this.velX);
32
}
33
if ((this.x - this.size) <= 0) {
34
this.velX = -(this.velX);
35
}
36
if ((this.y + this.size) >= height) {
37
this.velY = -(this.velY);
38
}
39
if ((this.y - this.size) <= 0) {
40
this.velY = -(this.velY);
41
}
42
this.x += this.velX;
43
this.y += this.velY;
44
}
45
let balls = [];
46
47
function loop() {
48
while (balls.length < 3) {
49
let size = 35;
50
let ball = new Ball(
51
random(0 + size, width - size), random(0 + size, height - size), 5, 5, 'yellow', size);
52
balls.push(ball);
53
}
54
ctx.fillStyle = 'grey';
55
ctx.fillRect(0, 0, width, height);
56
for (let i = 0; i < balls.length; i++) {
57
balls[i].draw();
58
balls[i].update();
59
}
60
rq = requestAnimationFrame(loop);
61
}
62
63
document.getElementById('butDiv').addEventListener('click', function(e) {
64
const tgt = e.target;
65
if (tgt.id === "Begin") {
66
if (tgt.value === "start") {
67
loop()
68
tgt.value = "stop";
69
document.getElementById("resetCanvas").classList.add("hide");
70
} else {
71
cancelAnimationFrame(rq)
72
this.value = "start";
73
document.getElementById("resetCanvas").classList.remove("hide");
74
}
75
} else if (tgt.id === "resetCanvas") {
76
cancelAnimationFrame(rq)
77
ctx.fillStyle = 'grey';
78
ctx.fillRect(0, 0, width, height);
79
document.getElementById("Begin").value="start";
80
tgt.classList.add("hide");
81
}
82
})
JavaScript
1
3
1
.hide {
2
display: none;
3
}
JavaScript
1
4
1
<div id="butDiv">
2
<input type='button' id='Begin' value='start'> <input type='button' class="hide" id='resetCanvas' value='reset'>
3
</div>
4
<canvas id='myCanvas'></canvas>