Skip to content

Commit 2efdc3b

Browse files
committed
Add realistic advanced particle system
1 parent e55d789 commit 2efdc3b

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

index.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,52 @@ function playFart(fart, randomPitch) {
3030
shaking = true;
3131
}
3232

33+
function emitParticles(pos) {
34+
let clickMeRect = clickMeText.getBoundingClientRect();
35+
console.log("rect", clickMeRect);
36+
function rand(multiplier) {
37+
return (Math.random() - 0.5) * multiplier;
38+
}
39+
function createBlock(pos, w, h, color) {
40+
var block = document.createElement("div");
41+
block.style.position = "absolute";
42+
block.style.left = pos.x + "px";
43+
block.style.top = pos.y + "px";
44+
block.style.width = w + "px";
45+
block.style.height = h + "px";
46+
block.style.backgroundColor = color;
47+
block.style.borderRadius = "50%";
48+
document.body.appendChild(block);
49+
return block;
50+
}
51+
let blocks = [
52+
createBlock(pos, 35, 35, "#572C25"),
53+
createBlock({ x: pos.x + rand(30), y: pos.y + rand(30) }, 30, 30, "#692A22"),
54+
createBlock({ x: pos.x + rand(30), y: pos.y + rand(30) }, 30, 30, "#662C24"),
55+
createBlock({ x: pos.x + rand(30), y: pos.y + rand(30) }, 20, 20, "#572A22"),
56+
createBlock({ x: pos.x + rand(30), y: pos.y + rand(30) }, 20, 20, "#672115"),
57+
createBlock({ x: pos.x + rand(30), y: pos.y + rand(30) }, 15, 15, "#572A22"),
58+
createBlock({ x: pos.x + rand(30), y: pos.y + rand(30) }, 10, 10, "#692A22"),
59+
createBlock({ x: pos.x + rand(30), y: pos.y + rand(30) }, 10, 10, "#662C24"),
60+
];
61+
62+
blocks.forEach((block) => {
63+
const blockSpinning = [
64+
{ opacity: 1, transform: "translateX(0) translateY(0)" },
65+
{ opacity: 0, transform: `translateX(${rand(50)}px) translateY(${rand(50)}px)` },
66+
];
67+
68+
const blockTiming = {
69+
duration: 270,
70+
iterations: 1,
71+
};
72+
let anim = block.animate(blockSpinning, blockTiming);
73+
anim.addEventListener("finish", () => {
74+
block.remove();
75+
});
76+
});
77+
}
78+
3379
const regularAction = () => {
3480
clickMeText.innerText = `Congrats! You clicked it ${counter} times!`;
3581
playFart(regularFart, true);
@@ -180,10 +226,11 @@ let shaking = false;
180226
let counter = 0; // TODO: DONT FORGET TO SET TO 0 ON RELEASE!!!
181227

182228
// TODO: change it to onmousedown (it stopped working after separating button and label)
183-
clickMe.onclick = () => {
229+
clickMe.onclick = (e) => {
184230
counter += 1;
185231
popupText.innerText = counter + "🍑💨";
186232
fireEvents();
233+
emitParticles({x: e.clientX, y: e.clientY});
187234
};
188235

189236
let prevTimestamp = 0;

0 commit comments

Comments
 (0)