Skip to content

Commit f1b1d73

Browse files
✨ feat: add confetti animation when player score (#53)
* ✨ feat: add confetti animation when player score * 📚 docs: add canvas-confetti to project dependencies in README * 🦄 refactor: move confetti functionality to UI module --------- Co-authored-by: lingbopro <ilovemc@zohomail.cn>
1 parent c43c5f7 commit f1b1d73

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,7 @@ This project uses or references the following open source projects:
4848
- **Sober.js** [[Official Website]](https://soberjs.com) [[GitHub]](https://github.com/apprat/sober)
4949
Sober is a Web Component UI component library that references the Google Material You design specification.
5050

51+
- **canvas-confetti** [[Official Website]](https://catdad.github.io/canvas-confetti/) [[GitHub]](https://github.com/catdad/canvas-confetti)
52+
🎉 performant confetti animation in the browser
53+
5154
Thanks to these projects and their authors.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"vite-plugin-html": "^3.2.2"
1717
},
1818
"dependencies": {
19+
"canvas-confetti": "^1.9.3",
1920
"sober": "~0.7.0"
2021
}
2122
}

src/script.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { lang } from './lang';
2-
import { hideLoading, showLoading, showSnackBar } from './ui';
2+
import { hideLoading, showLoading, showSnackBar, showWinnerConfetti } from './ui';
33

44
let players = [];
55
let totalScores = {};
@@ -146,6 +146,10 @@ export function incrementCurrentMatchScore(playerName) {
146146
)
147147
);
148148
disableScoreButtons();
149+
150+
// Wining Animation
151+
showWinnerConfetti();
152+
149153
setTimeout(() => {
150154
document.getElementById('result').innerText = '';
151155
updatePlayerScoreList();

src/ui.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import confetti from 'canvas-confetti';
12
import { getLanguageList, lang, langForce, setCurrentLanguage, setLanguage, updateElementLanguages } from './lang';
23
import { readConfig, writeConfig } from './utils';
34

@@ -63,6 +64,46 @@ export function hideLoading() {
6364
document.getElementById('top-loading').style.visibility = 'hidden';
6465
}
6566

67+
/**
68+
* @param {number} particleRatio
69+
* @param {confetti.Options} [opts]
70+
*/
71+
function fireConfetti(particleRatio, opts = {}) {
72+
const count = 200;
73+
const defaults = {
74+
origin: { y: 0.7 },
75+
};
76+
confetti({
77+
...defaults,
78+
...opts,
79+
particleCount: Math.floor(count * particleRatio),
80+
});
81+
}
82+
export function showWinnerConfetti() {
83+
fireConfetti(0.25, {
84+
spread: 26,
85+
startVelocity: 55,
86+
});
87+
fireConfetti(0.2, {
88+
spread: 60,
89+
});
90+
fireConfetti(0.35, {
91+
spread: 100,
92+
decay: 0.91,
93+
scalar: 0.8,
94+
});
95+
fireConfetti(0.1, {
96+
spread: 120,
97+
startVelocity: 25,
98+
decay: 0.92,
99+
scalar: 1.2,
100+
});
101+
fireConfetti(0.1, {
102+
spread: 120,
103+
startVelocity: 45,
104+
});
105+
}
106+
66107
export function e_toggleTheme(theme) {
67108
let pageEl = document.getElementById('page');
68109
let themeIconEl = document.getElementById('theme-icon');

0 commit comments

Comments
 (0)