how to add background moving particales that are sensetive to mouse
- User
how to add background moving particales that are sensetive to mouse
- Assistant
- User
- Assistant
how to add background moving particales that are sensetive to mouse
To add background moving particles that react to mouse movement, you can use JavaScript along with the HTML5 canvas element. A popular way to achieve this effect is by using the particles.js library or by writing custom code with the Canvas API.
Below are two approaches:
Add this in your HTML <head> or before the closing </body> tag:
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<div id="particles-js" style="position:fixed; width:100%; height:100%; top:0; left:0; z-index:-1;"></div>
Add this script to initialize:
<script>
particlesJS('particles-js',
{
"particles": {
"number": {
"value": 80,
"density": {
"enable": true,
"value_area": 800
}
},
"color": {
"value": "#ffffff"
},
"shape": {
"type": "circle"
},
"opacity": {
"value": 0.5,
"random": false
},
"size": {
"value": 3,
"random": true
},
"move": {
"enable": true,
"speed": 2,
"direction": "none",
"random": false,
"straight": false,
"out_mode": "out"
},
"line_linked": {
"enable": true,
"distance": 150,
"color": "#ffffff",
"opacity": 0.4,
"width": 1
}
},
"interactivity": {
"detect_on": "canvas",
"events": {
"onhover": {
"enable": true,
"mode": "grab"
},
"onclick": {
"enable": true,
"mode": "push"
},
"resize": true
},
"modes": {
"grab": {
"distance": 200,
"line_linked": {
"opacity": 1
}
},
"push": {
"particles_nb": 4
}
}
},
"retina_detect": true
}
);
</script>
This creates a particle background where particles connect with lines, move around, and interact with your mouse pointer by "grabbing" lines to your cursor.
If you want a simple custom version:
<canvas id="particleCanvas" style="position:fixed; top:0; left:0; width:100vw; height:100vh; z-index:-1;"></canvas>
<script>
const canvas = document.getElementById('particleCanvas');
const ctx = canvas.getContext('2d');
let width, height;
let particles = [];
const particleCount = 100;
let mouse = { x: null, y: null, radius: 100 };
function init() {
resize();
particles = [];
for(let i = 0; i < particleCount; i++) {
particles.push({
x: Math.random() * width,
y: Math.random() * height,
size: Math.random() * 3 + 1,
baseX: 0,
baseY: 0,
density: Math.random() * 30 + 1
});
}
}
function resize() {
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width;
canvas.height = height;
}
window.addEventListener('resize', () => {
resize();
init();
});
window.addEventListener('mousemove', function(event) {
mouse.x = event.x;
mouse.y = event.y;
});
window.addEventListener('mouseout', function() {
mouse.x = null;
mouse.y = null;
});
function draw() {
ctx.clearRect(0, 0, width, height);
ctx.fillStyle = 'white';
for(let i = 0; i < particles.length; i++) {
let p = particles[i];
// Move particles slightly
p.x += (Math.random() - 0.5) * 1;
p.y += (Math.random() - 0.5) * 1;
// Keep particles within bounds
if(p.x < 0) p.x = width;
if(p.x > width) p.x = 0;
if(p.y < 0) p.y = height;
if(p.y > height) p.y = 0;
// Interaction with mouse
if (mouse.x && mouse.y) {
let dx = mouse.x - p.x;
let dy = mouse.y - p.y;
let dist = Math.sqrt(dx*dx + dy*dy);
if(dist < mouse.radius) {
let angle = Math.atan2(dy, dx);
let force = (mouse.radius - dist) / mouse.radius;
let moveX = force * Math.cos(angle) * 5;
let moveY = force * Math.sin(angle) * 5;
p.x -= moveX;
p.y -= moveY;
}
}
ctx.beginPath();
ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2);
ctx.fill();
}
requestAnimationFrame(draw);
}
init();
draw();
</script>
Explanation:
requestAnimationFrame.Let me know if you want me to customize the particles (color, shape, speed) or add more interaction types!
how do i do that on this code? : <?php session_start();
if (!isset($_SESSION['user_id'])) { header("Location: register.php"); exit; } ?>
<div class="welcome-message" aria-live="polite" aria-atomic="true">
خوش آمدید،
<span class="username"><?php echo htmlspecialchars($_SESSION['full_name']); ?></span>!
</div>
<div>
<a href="logout.php" class="logout-link" aria-label="خارج شدن از حساب کاربری">
<button type="button" class="logout-btn">
<svg viewBox="0 0 512 512" aria-hidden="true" focusable="false">
<path d="M377.9 105.9L500.7 228.7c7.2 7.2 11.3 17.1 11.3 27.3s-4.1 20.1-11.3 27.3L377.9 406.1c-6.4 6.4-15 9.9-24 9.9c-18.7 0-33.9-15.2-33.9-33.9l0-62.1-128 0c-17.7 0-32-14.3-32-32l0-64c0-17.7 14.3-32 32-32l128 0 0-62.1c0-18.7 15.2-33.9 33.9-33.9c9 0 17.6 3.6 24 9.9zM160 96L96 96c-17.7 0-32 14.3-32 32l0 256c0 17.7 14.3 32 32 32l64 0c17.7 0 32 14.3 32 32s-14.3 32-32 32l-64 0c-53 0-96-43-96-96L0 128C0 75 43 32 96 32l64 0c17.7 0 32 14.3 32 32s-14.3 32-32 32z"></path>
</svg>
خروج
</button>
</a>
</div>
<!-- Upload Section -->
<section class="card no-print" aria-label="آپلود فایل اکسل">
<label for="excelFile" class="file-upload-label" aria-describedby="upload-instructions" tabindex="0">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/>
<polyline points="17 8 12 3 7 8"/>
<line x1="12" y1="3" x2="12" y2="15"/>
</svg>
<span id="upload-instructions">فایل اکسل دانش آموزان را انتخاب کنید</span>
<input type="file" id="excelFile" accept=".xlsx, .xls" aria-label="انتخاب فایل اکسل" />
</label>
<div class="action-buttons" role="group" aria-label="دکمههای عملیات فایل">
<button onclick="handleFile()" class="primary-button" id="processBtn" aria-live="polite" aria-busy="false">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" aria-hidden="true">
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/>
<polyline points="7 10 12 15 17 10"/>
<line x1="12" y1="15" x2="12" y2="3"/>
</svg>
پردازش اطلاعات
</button>
<button onclick="window.print()" id="printBtn" class="secondary-button" style="display:none;">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" aria-hidden="true">
<polyline points="6 9 6 2 18 2 18 9"/>
<path d="M6 18H4a2 2 0 0 1-2-2v-5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-2"/>
<rect x="6" y="14" width="12" height="8"/>
</svg>
چاپ گزارشات
</button>
</div>
</section>
<!-- Loading -->
<div id="loadingIndicator" class="fixed z-50 flex hidden inset-0 flex-col items-center justify-center bg-white bg-opacity-95" role="alert" aria-live="assertive" aria-busy="true" aria-label="در حال پردازش اطلاعات">
<div class="loading-spinner"></div>
<p>در حال پردازش اطلاعات...</p>
</div>
<!-- Search Section -->
<!-- Print Header -->
<section class="print-only" style="display:none;">
<div class="print-header text-center py-5">
<div class="print-title">فرم هدایت تحصیلی دانش آموزان</div>
<div class="print-subtitle mt-2">امیرارسلان ملکی</div>
<div class="divider-indigo" style="margin: 12px auto 0;"></div>
</div>
</section>
You want to add a background with moving particles that react to the mouse on your existing PHP/HTML page. The simplest and cleanest way is to use particles.js because you already include it at the bottom:
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<body> tag, before any other content. Make sure it is behind everything else (negative z-index):<div id="particles-js" style="position: fixed; width: 100%; height: 100%; top: 0; left: 0; z-index: -1;"></div>
</body>:<script>
particlesJS('particles-js',
{
"particles": {
"number": {
"value": 60,
"density": {
"enable": true,
"value_area": 800
}
},
"color": {
"value": "#ffffff"
},
"shape": {
"type": "circle"
},
"opacity": {
"value": 0.5,
"random": false
},
"size": {
"value": 3,
"random": true
},
"line_linked": {
"enable": true,
"distance": 150,
"color": "#ffffff",
"opacity": 0.4,
"width": 1
},
"move": {
"enable": true,
"speed": 2,
"direction": "none",
"random": false,
"straight": false,
"out_mode": "out"
}
},
"interactivity": {
"detect_on": "canvas",
"events": {
"onhover": {
"enable": true,
"mode": "grab" /* 'grab' mode creates lines connecting to cursor */
},
"onclick": {
"enable": true,
"mode": "push"
},
"resize": true
},
"modes": {
"grab": {
"distance": 200,
"line_linked": {
"opacity": 1
}
},
"push": {
"particles_nb": 4
}
}
},
"retina_detect": true
});
</script>
Your <body> currently has a background gradient, which will cover the particles container unless it's behind it with correct z-index (you already set it z-index: -1 for particles-js).
If for some reason particles are not visible (covered by background), you can move the gradient background to the particles container or adjust the CSS:
background: linear-gradient(135deg, #e0e7ff, #a5b4fc); from body CSS#particles-js via CSS or inline styleIn your code, it would look like:
<body>
<!-- Add particles container here -->
<div id="particles-js" style="position: fixed; width: 100%; height: 100%; top: 0; left: 0; z-index: -1;"></div>
<!-- دکمه فیدبک شناور -->
<a href="efeedback.html" ...>💬 بازخورد دهید</a>
<!-- Your header, main content, etc. -->
Then at the bottom of your file, just before </body>, add:
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<script>
particlesJS('particles-js',
{
"particles": {
"number": {
"value": 60,
"density": {"enable": true,"value_area": 800}
},
"color": {"value": "#ffffff"},
"shape": {"type": "circle"},
"opacity": {"value": 0.5, "random": false},
"size": {"value": 3, "random": true},
"line_linked": {
"enable": true,
"distance": 150,
"color": "#ffffff",
"opacity": 0.4,
"width": 1
},
"move": {
"enable": true,
"speed": 2,
"direction": "none",
"random": false,
"straight": false,
"out_mode": "out"
}
},
"interactivity": {
"detect_on": "canvas",
"events": {
"onhover": {
"enable": true,
"mode": "grab"
},
"onclick": {
"enable": true,
"mode": "push"
},
"resize": true
},
"modes": {
"grab": {
"distance": 200,
"line_linked": {"opacity": 1}
},
"push": {"particles_nb": 4}
}
},
"retina_detect": true
});
</script>
"color": {"value": "#ffffff"} to another hex or array of colors."number": {"value": 60} to more or fewer particles."move": {"speed": 2} to change particle movement speed.If you want me to prepare a full snippet wrapped into your existing PHP page, just ask!
Not yet included in a published release.