Skip to content

Commit

Permalink
refactor: Change background particles to pixel size
Browse files Browse the repository at this point in the history
  • Loading branch information
PresentKim committed Sep 15, 2024
1 parent 4cdad59 commit ac4e09b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
26 changes: 13 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,24 +284,24 @@ var createElement = function (tag, attributes) {

var game = new Game(5, 5);

// Set the number of particles based on device performance
var BACKGROUND_PARTICLE_COUNT;
if (navigator.hardwareConcurrency) {
BACKGROUND_PARTICLE_COUNT = Math.min(30, navigator.hardwareConcurrency * 2);
} else {
BACKGROUND_PARTICLE_COUNT = 15;
}
// Set the number of particles based on device width
var BACKGROUND_PARTICLE_COUNT = Math.min(
300,
Math.floor(window.innerWidth / 2)
);

// Generate Background Floating Particles
var background = document.getElementById("background");
var createParticle = function () {
var size = Math.random() * 10 + 4;
var createParticle = function (initial) {
var duration = Math.random() * 3 + 10;
var div = createElement("div");
div.style.setProperty("--duration", duration + "s");
div.style.setProperty("--x", Math.random() * 100 + "%");
div.style.setProperty("--y", "-" + size + "rem");
div.style.setProperty("--size", size + "rem");
div.style.setProperty("--duration", size / 2 + "s");
div.style.setProperty("--rotate", Math.random() * 720 + "deg");
if (initial) {
// Set minus animationDelay to make particles start at random time
div.style.animationDelay = -duration * Math.random() + "s";
}
div.addEventListener("animationend", function () {
background.removeChild(div);
createParticle();
Expand All @@ -310,5 +310,5 @@ var createParticle = function () {
};

for (var i = 0; i < BACKGROUND_PARTICLE_COUNT; i++) {
createParticle();
createParticle(true);
}
9 changes: 4 additions & 5 deletions styles/background.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
#background div {
content: "";
position: absolute;
bottom: var(--y);
bottom: 0;
left: var(--x);
display: block;
list-style: none;
width: var(--size);
height: var(--size);
background: rgba(255, 255, 255, 0.2);
border-radius: 10%;
width: 4px;
height: 4px;
background: #ffffff;
animation: background-animation var(--duration) linear;
}

Expand Down

0 comments on commit ac4e09b

Please sign in to comment.