Skip to content

Commit c9c5b9d

Browse files
committed
fix(stars):
fix stacking stars by leaving the web in the background
1 parent cfa4ac0 commit c9c5b9d

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

stars.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
const finalSpeed = 0.2;
88

99
let isFinalSpeed = false;
10+
let shouldGenerateStar = true;
1011

1112
initializeCanvas();
1213
populateStars();
@@ -40,12 +41,12 @@
4041
}
4142

4243
function speedPulse() {
43-
console.log('lala');
4444
speed = 20;
4545
}
4646

4747
function refresh() {
4848
clearCanvas();
49+
checkGenerateStars();
4950
checkSpeed();
5051
layers.map(layer => {
5152
drawLayer(layer);
@@ -86,6 +87,20 @@
8687
layer = layer.filter(object => object.id !== element.id);
8788
}
8889

90+
function checkGenerateStars() {
91+
if (!shouldGenerateStar) return;
92+
shouldGenerateStar = false;
93+
populateStars();
94+
setDelayForStarGeneration();
95+
}
96+
97+
function setDelayForStarGeneration() {
98+
setTimeout(
99+
() => (shouldGenerateStar = true),
100+
getRandomInt(500, 2000) / (speed + 0.9)
101+
);
102+
}
103+
89104
function populateStars() {
90105
const starsAmount = isFinalSpeed ? 7 : 12;
91106

@@ -99,8 +114,6 @@
99114
star.y = canvas.height;
100115
layers[0].push(star);
101116
}
102-
103-
setTimeout(() => populateStars(), getRandomInt(500, 2000) / (speed + 0.9));
104117
}
105118

106119
function nextFrame() {

0 commit comments

Comments
 (0)