Skip to content

Commit 855ff11

Browse files
committed
feature: add confetti to win condition
1 parent c2bdd3e commit 855ff11

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

js/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"dev": "yarn serve"
1010
},
1111
"dependencies": {
12-
"canvas-confetti": "^1.3.1",
12+
"canvas-confetti": "^1.3.2",
1313
"core-js": "^3.6.5",
1414
"jspdf": "^2.1.1",
1515
"lodash": "^4.17.20",

js/src/utils/canvasConfetti.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import confetti from 'canvas-confetti'
2+
3+
export const launchConfetti = () => {
4+
const duration = 15 * 1000
5+
const animationEnd = Date.now() + duration
6+
const defaults = { startVelocity: 30, spread: 360, ticks: 60, zIndex: 0 }
7+
8+
function randomInRange(min, max) {
9+
return Math.random() * (max - min) + min
10+
}
11+
12+
const confettiInterval = setInterval(function() {
13+
const timeLeft = animationEnd - Date.now()
14+
15+
if (timeLeft <= 0) {
16+
return clearInterval(confettiInterval)
17+
}
18+
19+
const particleCount = 50 * (timeLeft / duration)
20+
// since particles fall down, start a bit higher than random
21+
confetti(
22+
Object.assign({}, defaults, {
23+
particleCount,
24+
origin: { x: randomInRange(0.1, 0.3), y: Math.random() - 0.2 }
25+
})
26+
)
27+
confetti(
28+
Object.assign({}, defaults, {
29+
particleCount,
30+
origin: { x: randomInRange(0.7, 0.9), y: Math.random() - 0.2 }
31+
})
32+
)
33+
}, 250)
34+
}

js/src/views/SpaceGame.vue

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import MiniGame from '../components/MiniGame'
44
import MiniGame2 from '../components/MiniGame2'
55
import MiniGame3 from '../components/MiniGame3'
66
import MiniGameStatus from '../components/MiniGameStatus'
7+
import { launchConfetti } from '../utils/canvasConfetti'
78
89
export default {
910
components: {
@@ -22,6 +23,15 @@ export default {
2223
miniGamesWon: 0
2324
})
2425
26+
watch(
27+
() => state.gameStatus,
28+
currentValue => {
29+
if (currentValue === 'Player wins!') {
30+
launchConfetti()
31+
}
32+
}
33+
)
34+
2535
watch(
2636
() => user.miniGamesWon,
2737
currentValue => {

js/yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -2468,10 +2468,10 @@ caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001111:
24682468
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001112.tgz#0fffc3b934ff56ff0548c37bc9dad7d882bcf672"
24692469
integrity sha512-J05RTQlqsatidif/38aN3PGULCLrg8OYQOlJUKbeYVzC2mGZkZLIztwRlB3MtrfLmawUmjFlNJvy/uhwniIe1Q==
24702470

2471-
canvas-confetti@^1.3.1:
2472-
version "1.3.1"
2473-
resolved "https://registry.yarnpkg.com/canvas-confetti/-/canvas-confetti-1.3.1.tgz#40d3af7a8079d1002a555462f587fb06cc4a81ab"
2474-
integrity sha512-gX4K7JH9JsjEl6eOUa5/NilKExT/9uxXg6Z2+Z2O/lgU/pc1Rfd97Q+bBHQpnZeYfBEbvp1iU8e0CKW+1WBdGw==
2471+
canvas-confetti@^1.3.2:
2472+
version "1.3.2"
2473+
resolved "https://registry.yarnpkg.com/canvas-confetti/-/canvas-confetti-1.3.2.tgz#8376f50bb7a68f7f048c5ee6ad77f95d0cd0948d"
2474+
integrity sha512-fLnMiI7eueyQUXG2msPnOtxjf/kvyEPm6w9ycherNtjc9qtfXcvlyZtoNfA7RClcAMhNOcKghV3z2te9Pusr7g==
24752475

24762476
canvg@^3.0.6:
24772477
version "3.0.7"

0 commit comments

Comments
 (0)