📚معرفی و نصب

Particles.js یک کتابخانه سبک جاوااسکریپت است که با canvas ذرات متحرک زیبایی ایجاد می‌کند. بسیار مناسب برای hero section، صفحه loading و پس‌زمینه‌های تعاملی.

نصب

<script src="particles.min.js"></script>

ساختار HTML

<!-- div با ارتفاع مشخص و position:relative -->
<div id="particles-js" style="position:relative; width:100%; height:400px; background:#0d0d2b;"></div>

راه‌اندازی پایه

particlesJS('particles-js', {
  particles: {
    number:  { value: 80 },
    color:   { value: '#88ce02' },
    shape:   { type: 'circle' },
    opacity: { value: 0.5 },
    size:    { value: 3 },
    move:    { enable: true, speed: 3 }
  },
  interactivity: {
    events: {
      onhover: { enable: true,  mode: 'repulse' },
      onclick: { enable: true,  mode: 'push' }
    }
  }
});
ⓘ نکته: div container باید position: relative داشته باشد. Canvas به‌صورت position: absolute درون آن قرار می‌گیرد و پر می‌شود.

پیکربندی پایه

گزینهنوعتوضیح
particles.number.valuenumberتعداد ذرات روی صفحه
particles.number.densityobjectتوزیع متناسب با سطح: {enable:true, value_area:800}
particles.color.valuestring / arrayرنگ ذرات — می‌تواند آرایه رنگ باشد
particles.shape.typestringcircle, edge, triangle, polygon, star, image
particles.opacity.value0–1شفافیت ذرات
particles.opacity.randombooleanشفافیت تصادفی
particles.size.valuenumberاندازه به پیکسل
particles.size.randombooleanاندازه تصادفی
retina_detectbooleanبهینه‌سازی برای صفحه‌های Retina
✨ دمو: ذرات دایره‌ای ساده
particlesJS('p-demo1', {
  particles: {
    number:  { value: 60, density: { enable: true, value_area: 800 } },
    color:   { value: '#88ce02' },
    shape:   { type: 'circle' },
    opacity: { value: 0.7, random: true },
    size:    { value: 4, random: true },
    line_linked: { enable: false },
    move:    { enable: true, speed: 2, out_mode: 'out' }
  },
  interactivity: { events: { onhover: { enable: false }, onclick: { enable: false } } },
  retina_detect: true
});

حرکت ذرات (Move)

گزینهمقادیرتوضیح
move.enabletrue / falseفعال/غیرفعال حرکت
move.speednumberسرعت — پیشنهاد: ۱ تا ۵
move.directionnone, top, bottom, left, right, top-right, ...جهت حرکت
move.randombooleanسرعت تصادفی برای هر ذره
move.straightbooleanحرکت مستقیم (بدون انحراف)
move.out_modeout / bounceرفتار در لبه صفحه
move.bouncebooleanبرخورد ذرات با یکدیگر
⬆ دمو: ذرات رنگارنگ در حال صعود (direction: top)
particlesJS('p-demo2', {
  particles: {
    number: { value: 50 },
    color:  { value: ['#ff6b6b', '#ffd93d', '#6bcfff', '#88ce02', '#ff9f43'] },
    shape:  { type: 'circle' },
    opacity: { value: 0.8, random: true },
    size:    { value: 5, random: true },
    line_linked: { enable: false },
    move: {
      enable: true, speed: 3,
      direction: 'top',     // حرکت به سمت بالا
      straight: false, random: true,
      out_mode: 'out'        // از پایین دوباره وارد می‌شود
    }
  }
});

🕷خطوط اتصال (line_linked)

particles: {
  line_linked: {
    enable:   true,
    distance: 150,      // حداکثر فاصله برای رسم خط (px)
    color:    '#ffffff',
    opacity:  0.4,
    width:    1
  }
}
🕷 دمو: Neural Network — ذرات با خطوط اتصال
particlesJS('p-demo3', {
  particles: {
    number: { value: 70 },
    color:  { value: '#aaaaff' },
    shape:  { type: 'circle' },
    opacity: { value: 0.6 },
    size:    { value: 3 },
    line_linked: {
      enable: true, distance: 130,
      color: '#6666ff', opacity: 0.4, width: 1
    },
    move: { enable: true, speed: 1.5, out_mode: 'bounce' }
  }
});

🖰تعامل با ماوس

Modeرویدادتوضیح
grabhoverخطوط اتصال از ماوس به ذرات نزدیک
bubblehoverذرات نزدیک بزرگ‌تر و شفاف‌تر می‌شوند
repulsehoverذرات از ماوس فرار می‌کنند
pushclickبا کلیک ذرات جدید اضافه می‌شوند
removeclickبا کلیک ذرات حذف می‌شوند
🖰 دمو: ماوس را روی ذرات بکشید — Repulse | کلیک = Push
particlesJS('p-demo4', {
  particles: {
    number: { value: 100 },
    color:  { value: '#ff6b6b' },
    shape:  { type: 'circle' },
    opacity: { value: 0.7 },
    size:    { value: 4, random: true },
    line_linked: { enable: true, distance: 100, color: '#ff4444', opacity: 0.3, width: 1 },
    move: { enable: true, speed: 2, out_mode: 'bounce' }
  },
  interactivity: {
    detect_on: 'canvas',
    events: {
      onhover: { enable: true,  mode: 'repulse' },
      onclick: { enable: true,  mode: 'push' },
      resize: true
    },
    modes: {
      repulse: { distance: 100, duration: 0.4 },
      push:    { particles_nb: 4 }
    }
  },
  retina_detect: true
});

🎪شکل‌های مختلف (Shapes)

Particles.js از شکل‌های مختلفی پشتیبانی می‌کند: circle, edge (مربع), triangle, polygon, star, image.

shape: {
  type: 'star',          // یا 'polygon', 'triangle', 'edge'
  stroke: { width: 1, color: '#fff' },
  polygon: { nb_sides: 6 }, // تعداد اضلاع برای polygon
  image: {
    src: 'img/particle.png',
    width: 100, height: 100
  }
}
⭐ دمو: ستاره‌ها و شکل‌های هندسی — Grab mode
particlesJS('p-demo5', {
  particles: {
    number: { value: 50 },
    color:  { value: ['#ffd700', '#ff6b6b', '#88ce02', '#6bcfff'] },
    shape:  { type: 'star', stroke: { width: 0 } },
    opacity: { value: 0.8, random: true },
    size:    { value: 8, random: true },
    line_linked: { enable: false },
    move: { enable: true, speed: 2, random: true }
  },
  interactivity: {
    events: { onhover: { enable: true, mode: 'grab' }, onclick: { enable: true, mode: 'push' } },
    modes:  { grab: { distance: 180, line_linked: { opacity: 0.8 } } }
  }
});

🌈پریست‌های آماده

❄ پریست برف

❄ افکت برف

🌞 پریست کهکشان

🌞 افکت کهکشان — ستاره‌های درخشان
// پریست برف
particlesJS('snow', {
  particles: {
    number: { value: 80 },
    color:  { value: '#ffffff' },
    shape:  { type: 'circle' },
    opacity: { value: 0.7, random: true },
    size:    { value: 4, random: true },
    line_linked: { enable: false },
    move: { enable: true, speed: 2, direction: 'bottom', random: true, out_mode: 'out' }
  }
});

// پریست کهکشان
particlesJS('galaxy', {
  particles: {
    number: { value: 120 },
    color:  { value: ['#ffffff', '#ffffaa', '#aaaaff', '#88ce02'] },
    shape:  { type: 'circle' },
    opacity: { value: 0.9, random: true, anim: { enable: true, speed: 1, opacity_min: 0.1, sync: false } },
    size:    { value: 2, random: true },
    line_linked: { enable: false },
    move: { enable: true, speed: 0.5, random: true }
  }
});
💡 نکات Performance:
  • بیش از ۱۵۰ ذره ممکن است روی دستگاه‌های ضعیف کند شود
  • line_linked CPU بیشتری نسبت به ذرات ساده مصرف می‌کند
  • در موبایل تعداد ذرات را به ۳۰–۵۰ کاهش دهید
  • retina_detect: true را برای صفحات HiDPI فعال کنید

🏆Hero Section کامل

ترکیب Particles.js با محتوای روی صفحه برای ساخت یک Hero Section حرفه‌ای:

✨ دمو: Hero Section زنده — Grab mode

🚀 HubGit.ir

آموزش کتابخانه‌های جاوااسکریپت به فارسی

<!-- HTML ساختار -->
<div class="hero" style="position:relative; height:100vh; background:#0d0d2b;">
  <div id="particles-js" style="position:absolute; width:100%; height:100%;"></div>
  <div class="content" style="position:relative; z-index:10; text-align:center; color:white;">
    <h1>عنوان</h1>
  </div>
</div>

// JavaScript
particlesJS('particles-js', {
  particles: {
    number: { value: 100 },
    color:  { value: '#88ce02' },
    shape:  { type: 'circle' },
    opacity: { value: 0.5, random: true },
    size:    { value: 3, random: true },
    line_linked: { enable: true, distance: 150, color: '#88ce02', opacity: 0.2, width: 1 },
    move: { enable: true, speed: 2, out_mode: 'bounce' }
  },
  interactivity: {
    events: {
      onhover: { enable: true, mode: 'grab' },
      onclick: { enable: true, mode: 'push' }
    },
    modes: { grab: { distance: 200, line_linked: { opacity: 0.6 } } }
  },
  retina_detect: true
});
💡 خلاصه Particles.js:
  • particles.number.value — تعداد ذرات
  • particles.color.value — رنگ (رشته یا آرایه)
  • particles.shape.type — circle, star, triangle, polygon
  • particles.move.direction — none, top, bottom, left, right
  • particles.line_linked.enable — خطوط اتصال
  • interactivity.events.onhover.mode — grab, bubble, repulse
  • interactivity.events.onclick.mode — push, remove

🚀زمین بازی کد (Playground)

پیکربندی Particles.js را تغییر بده و افکت ذرات را زنده ببین. تعداد، رنگ، حرکت و تعامل ماوس را امتحان کن و ▶ اجرا را بزن. نسخهٔ ۲.۰.۰ آفلاین بارگذاری می‌شود.