كيف تحرك شعارك بصيغة SVG خطوة بخطوة؟
شعار يرسم نفسه ثم يمتلئ باللون يعطي انطباعا احترافيا في مقدمة موقعك أو فيديو. الشرط أن يكون شعارك SVG مكونا من مسارات (paths) أو أشكال. أما اسمك المكتوب بالعربية فنتركه نصا عاديا يظهر بتلاشي، ولا نرسمه حرفا حرفا.
الخطوات
- جهّز الشعار كملف SVG: كل جزء مهم مسار أو شكل مستقل (دائرة، خط، مضلع) وبلا نص مُحوَّل.
- اقرأ طول كل شكل بـ getTotalLength() مرة واحدة، وخزّنه في متغير CSS مثل --len.
- اجعل stroke-dasharray وstroke-dashoffset يساويان --len، وحرّك الإزاحة إلى 0 لرسم الخط. أعط الشكل الثاني تأخيرا ليبدأ بعد الأول.
- بعد انتهاء الرسم حرّك fill من الشفاف إلى لون الشعار، ثم أظهر الاسم بتلاشي.
- عند prefers-reduced-motion أظهر الشعار كاملا بلا حركة.
المصادر
- MDN: getTotalLength() تعيد الطول الكلي للمسار: https://developer.mozilla.org/en-US/docs/Web/API/SVGGeometryElement/getTotalLength
- MDN: stroke-dasharray تحدد نمط الشرطات: https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/stroke-dasharray
- MDN: stroke-dashoffset تزيح نمط الشرطات: https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/stroke-dashoffset
- Tahrikit: أيقونة SVG ترسم نفسها بـ stroke-dasharray وstroke-dashoffset: https://motion.velloxhq.com/en/learn/svg-icon-draw-animation.html
- MDN: prefers-reduced-motion يكشف إعداد الجهاز لتقليل الحركة غير الضرورية، وقيمة reduce تعني أنه مفعّل: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion
الكود
HTML
<!doctype html>
<html lang="ar" dir="rtl">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
body { margin: 0; min-height: 100vh; display: grid; place-content: center; justify-items: center; gap: .6rem; background: #0b1220; color: #fff; font-family: system-ui, "Segoe UI", Tahoma, sans-serif; }
svg { width: 160px; height: 160px; }
.draw { fill: #22d3ee00; stroke: #22d3ee; stroke-width: 6; stroke-linecap: round; stroke-linejoin: round;
stroke-dasharray: var(--len); stroke-dashoffset: var(--len);
animation: draw 1.4s ease-out forwards, fillin .6s ease-out 1.4s forwards; }
.draw.two { stroke: #fff; animation: draw 1s ease-out 1.4s forwards; } /* the check mark stays white, no fill */
.name { font-size: 2rem; font-weight: 800; opacity: 0; animation: show .6s ease-out 2s forwards; }
@keyframes draw { to { stroke-dashoffset: 0; } }
@keyframes fillin { to { fill: #22d3ee; } }
@keyframes show { to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
.draw { animation: none; stroke-dashoffset: 0; fill: #22d3ee; }
.draw.two { fill: none; }
.name { animation: none; opacity: 1; }
}
</style>
<svg viewBox="0 0 100 100" role="img" aria-label="شعار">
<circle class="draw" cx="50" cy="50" r="40"/>
<path class="draw two" d="M30 55 L45 70 L72 35"/>
</svg>
<div class="name">شعارك</div>
<script>
// measure every shape once, and store its length in a CSS variable
document.querySelectorAll('.draw').forEach(p => p.style.setProperty('--len', Math.ceil(p.getTotalLength())));
</script>
</html>لم تجد الحركة التي تريدها؟ اطلبها من هنا
مقالات ذات صلة: