Skip to content

Commit 362a5b1

Browse files
committed
Add realistic advanced particle system
1 parent e55d789 commit 362a5b1

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

index.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,53 @@ 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+
block.style.pointerEvents = "none";
49+
document.body.appendChild(block);
50+
return block;
51+
}
52+
let blocks = [
53+
createBlock(pos, 35, 35, "#572C25"),
54+
createBlock({ x: pos.x + rand(30), y: pos.y + rand(30) }, 30, 30, "#692A22"),
55+
createBlock({ x: pos.x + rand(30), y: pos.y + rand(30) }, 30, 30, "#662C24"),
56+
createBlock({ x: pos.x + rand(30), y: pos.y + rand(30) }, 20, 20, "#572A22"),
57+
createBlock({ x: pos.x + rand(30), y: pos.y + rand(30) }, 20, 20, "#672115"),
58+
createBlock({ x: pos.x + rand(30), y: pos.y + rand(30) }, 15, 15, "#572A22"),
59+
createBlock({ x: pos.x + rand(30), y: pos.y + rand(30) }, 10, 10, "#692A22"),
60+
createBlock({ x: pos.x + rand(30), y: pos.y + rand(30) }, 10, 10, "#662C24"),
61+
];
62+
63+
blocks.forEach((block) => {
64+
const blockSpinning = [
65+
{ opacity: 1, transform: "translateX(0) translateY(0)" },
66+
{ opacity: 0, transform: `translateX(${rand(50)}px) translateY(${rand(50)}px)` },
67+
];
68+
69+
const blockTiming = {
70+
duration: 270,
71+
iterations: 1,
72+
};
73+
let anim = block.animate(blockSpinning, blockTiming);
74+
anim.addEventListener("finish", () => {
75+
block.remove();
76+
});
77+
});
78+
}
79+
3380
const regularAction = () => {
3481
clickMeText.innerText = `Congrats! You clicked it ${counter} times!`;
3582
playFart(regularFart, true);
@@ -180,10 +227,11 @@ let shaking = false;
180227
let counter = 0; // TODO: DONT FORGET TO SET TO 0 ON RELEASE!!!
181228

182229
// TODO: change it to onmousedown (it stopped working after separating button and label)
183-
clickMe.onclick = () => {
230+
clickMe.onclick = (e) => {
184231
counter += 1;
185232
popupText.innerText = counter + "🍑💨";
186233
fireEvents();
234+
emitParticles({x: e.clientX, y: e.clientY});
187235
};
188236

189237
let prevTimestamp = 0;

0 commit comments

Comments
 (0)