كيف أصنع تأثير قصاصات الاحتفال (Confetti) بدون مكتبة؟
قصاصات الاحتفال تعطي لحظة فرح بعد إتمام إجراء مهم، مثل إرسال نموذج أو شراء. نرسمها على canvas شفاف فوق الصفحة بجاذبية وسرعة عشوائية، دون أي مكتبة خارجية.
الخطوات
- أضف canvas ثابتاً يغطي الشاشة مع pointer-events: none و aria-hidden="true" حتى لا يعيق النقر ولا يقرؤه قارئ الشاشة.
- عند النقر أنشئ عشرات القطع بألوان وزوايا وسرعات عشوائية تنطلق من موضع الزر.
- في كل إطار أضف جاذبية صغيرة للسرعة العمودية، وقلّل السرعة الأفقية قليلاً (احتكاك الهواء)، ثم ارسم القطعة مدوّرة.
- احذف القطع التي خرجت من أسفل الشاشة، وأوقف الحلقة عندما لا يبقى شيء.
- إذا فضّل المستخدم تقليل الحركة فلا تُطلق القصاصات، وأعلمه بنص في منطقة aria-live بدلاً منها.
الكود
HTML
<!doctype html>
<html lang="ar" dir="rtl">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Confetti</title>
<style>
body { min-height: 100vh; display: grid; place-items: center; margin: 0; font-family: system-ui, "Segoe UI", Tahoma, sans-serif; }
button { padding: .7rem 1.4rem; font: inherit; cursor: pointer; }
canvas { position: fixed; inset: 0; inline-size: 100%; block-size: 100%; pointer-events: none; }
#status { position: fixed; inset-block-end: 1rem; inset-inline: 0; text-align: center; }
</style>
<button id="go" type="button">أرسل</button>
<canvas id="c" aria-hidden="true"></canvas>
<p id="status" role="status" aria-live="polite"></p>
<script>
const canvas = document.getElementById("c");
const ctx = canvas.getContext("2d");
const status = document.getElementById("status");
const reduce = matchMedia("(prefers-reduced-motion: reduce)");
const colors = ["#6c5ce7", "#00b894", "#fdcb6e", "#e17055", "#0984e3"];
let pieces = [], raf = 0, W = 0, H = 0;
function size() {
const dpr = window.devicePixelRatio || 1;
W = innerWidth; H = innerHeight;
canvas.width = W * dpr; canvas.height = H * dpr;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
}
size(); addEventListener("resize", size);
function burst(x, y) {
for (let i = 0; i < 70; i++) {
const a = Math.random() * Math.PI * 2, s = 4 + Math.random() * 8;
pieces.push({
x, y, vx: Math.cos(a) * s, vy: Math.sin(a) * s - 6,
rot: Math.random() * 6.28, vr: (Math.random() - .5) * .3,
w: 6 + Math.random() * 6, h: 3 + Math.random() * 4,
c: colors[(Math.random() * colors.length) | 0]
});
}
if (!raf) raf = requestAnimationFrame(tick);
}
function tick() {
ctx.clearRect(0, 0, W, H);
pieces = pieces.filter((p) => p.y < H + 20);
for (const p of pieces) {
p.vy += .25; // gravity
p.vx *= .99; // air drag
p.x += p.vx; p.y += p.vy; p.rot += p.vr;
ctx.save();
ctx.translate(p.x, p.y); ctx.rotate(p.rot);
ctx.fillStyle = p.c;
ctx.fillRect(-p.w / 2, -p.h / 2, p.w, p.h);
ctx.restore();
}
raf = pieces.length ? requestAnimationFrame(tick) : 0;
if (!pieces.length) ctx.clearRect(0, 0, W, H);
}
document.getElementById("go").addEventListener("click", (e) => {
status.textContent = "تم الإرسال بنجاح";
if (reduce.matches) return; // no motion: text announcement only
const r = e.currentTarget.getBoundingClientRect();
burst(r.left + r.width / 2, r.top + r.height / 2);
});
</script>
</html>