Skip to content

Add homepage confetti effect for 50th Remote Hack celebration #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _hacks/50.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
hack_number: 50
date: 2024-06-29
upcoming: true
celebrate: true
---
4 changes: 4 additions & 0 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ <h1 class="⭐️">
{% endif %}
</main>

{% if page.celebrate %}
<script src="/assets/confetti.js"></script>
{% endif %}

</body>

</html>
61 changes: 61 additions & 0 deletions assets/confetti.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Confetti effect implementation
(function() {
// Check for prefers-reduced-motion
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (prefersReducedMotion) {
return;
}

const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
document.body.appendChild(canvas);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

const confettis = [];
const colors = ['#FFC0CB', '#FFD700', '#8A2BE2', '#00FF00', '#FF4500', '#00BFFF'];

function createConfetti() {
const x = Math.floor(Math.random() * canvas.width);
const y = Math.floor(Math.random() * canvas.height);
const color = colors[Math.floor(Math.random() * colors.length)];
const size = Math.random() * (15 - 5) + 5;
const speed = Math.random() * (5 - 2) + 2;
const angle = Math.random() * 360;
confettis.push({x, y, color, size, speed, angle});
}

function drawConfetti() {
confettis.forEach((confetti, index) => {
ctx.beginPath();
ctx.arc(confetti.x, confetti.y, confetti.size, 0, 2 * Math.PI);
ctx.fillStyle = confetti.color;
ctx.fill();

// Update confetti position
confetti.y += confetti.speed;
confetti.x += Math.cos(confetti.angle) * confetti.speed;

// Remove confetti that are out of the screen
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't do this, it is basically just a memory leak?

if (confetti.y > canvas.height) {
confettis.splice(index, 1);
}
});
}

function update() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawConfetti();
requestAnimationFrame(update);
}

// Create confettis every 100ms
setInterval(createConfetti, 100);

// Stop creating confettis after 5 seconds
setTimeout(() => {
clearInterval(createConfetti);
Comment on lines +53 to +57
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no interval id being stored, so it'll never stop 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can limit it easily to just the homepage then I think this is fine

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I guess it is fine! I thought it was worth noting that the code from Copilot Workspace Preview has syntax issues 😓

}, 5000);

update();
})();