Skip to content

Commit fa91540

Browse files
committed
feat: add confetti
1 parent 823d2c9 commit fa91540

6 files changed

Lines changed: 71 additions & 24 deletions

File tree

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"@tailwindcss/vite": "^4.1.13",
1313
"astro": "^5.13.8",
14+
"canvas-confetti": "^1.9.3",
1415
"tailwindcss": "^4.1.13"
1516
}
1617
}

src/pages/index.astro

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ import "../styles/global.css";
3939
>
4040
<ProductCard image={crepeMaker} name="Crepe Maker (30cm)" />
4141
</div>
42-
<div
42+
<button
43+
data-click="confetti"
4344
data-rotate="follow-mouse"
44-
class="absolute -right-5 -bottom-5 px-5 py-2 bg-gray-800 rounded-sm inline-block text-white font-semibold text-lg"
45+
aria-hidden="true"
46+
class="absolute z-200 cursor-pointer -right-5 -bottom-5 px-5 py-2 bg-gray-800 hover:bg-gray-700 active:bg-gray-900 rounded-sm inline-block text-white font-semibold text-lg"
4547
>
4648
Reserve
47-
</div>
49+
</button>
4850
</div>
4951
</div>
5052
</header>
@@ -138,26 +140,7 @@ import "../styles/global.css";
138140
</p>
139141
</footer>
140142
<script>
141-
(() => {
142-
const MAX_X = 3;
143-
const MAX_Y = 10;
144-
const RANGE_X = 700;
145-
const RANGE_Y = 500;
146-
147-
const followMouse = Array.from(
148-
document.querySelectorAll("[data-rotate=follow-mouse]")
149-
) as HTMLElement[];
150-
151-
document.addEventListener("mousemove", (e) => {
152-
followMouse.forEach((elm) => {
153-
const rect = elm.getBoundingClientRect();
154-
const center = {
155-
left: rect.left + rect.width / 2,
156-
top: rect.top + rect.height / 2,
157-
};
158-
elm.style.transform = `rotateX(${Math.round((Math.min(center.top - e.clientY, RANGE_Y) / RANGE_Y) * MAX_X)}deg) rotateY(${Math.round((Math.min(center.left - e.clientX, RANGE_X) / RANGE_X) * MAX_Y * -1)}deg)`;
159-
});
160-
});
161-
})();
143+
import "../scripts/click-confetti";
144+
import "../scripts/follow-mouse";
162145
</script>
163146
</Layout>

src/scripts/click-confetti.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import confetti from "canvas-confetti";
2+
import { isReducedMotion } from "./media";
3+
4+
if (!isReducedMotion) {
5+
const elms = Array.from(
6+
document.querySelectorAll("[data-click=confetti]")
7+
) as HTMLElement[];
8+
console.log(elms);
9+
elms.forEach((elm) => {
10+
elm.addEventListener("click", () => {
11+
console.log("click");
12+
const rect = elm.getBoundingClientRect();
13+
confetti({
14+
particleCount: 50,
15+
spread: 50,
16+
origin: {
17+
x: (rect.left + rect.width / 2) / window.innerWidth,
18+
y: (rect.top + rect.height / 2) / window.innerHeight,
19+
},
20+
});
21+
});
22+
});
23+
}

src/scripts/follow-mouse.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { isReducedMotion } from "../scripts/media";
2+
3+
const MAX_X = 3;
4+
const MAX_Y = 10;
5+
const RANGE_X = 700;
6+
const RANGE_Y = 500;
7+
8+
if (!isReducedMotion) {
9+
const followMouse = Array.from(
10+
document.querySelectorAll("[data-rotate=follow-mouse]")
11+
) as HTMLElement[];
12+
13+
document.addEventListener("mousemove", (e) => {
14+
followMouse.forEach((elm) => {
15+
const rect = elm.getBoundingClientRect();
16+
const center = {
17+
left: rect.left + rect.width / 2,
18+
top: rect.top + rect.height / 2,
19+
};
20+
elm.style.transform = `rotateX(${Math.round(
21+
(Math.min(center.top - e.clientY, RANGE_Y) / RANGE_Y) * MAX_X
22+
)}deg) rotateY(${Math.round(
23+
(Math.min(center.left - e.clientX, RANGE_X) / RANGE_X) * MAX_Y * -1
24+
)}deg)`;
25+
});
26+
});
27+
}

src/scripts/media.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const isReducedMotion =
2+
window.matchMedia(`(prefers-reduced-motion: reduce)`).matches === true;

0 commit comments

Comments
 (0)