If you want to implement particles.js directly in an HTML file without linking to an external library (CDN or local), and you have particles.js included in your project as a JavaScript object or function (presumably through a bundler or in a specific context where you can access it), the following simple HTML example would demonstrate how to set an interactive particles background.
HTML Structure (Standalone Example)
Here's an example of what your HTML structure would look like:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Particles.js Background</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #282c34; /* Background color */
}
#particles-js {
position: absolute;
width: 100%;
height: 100%;
z-index: -1; /* Ensure it's behind other content */
}
.content {
position: relative;
z-index: 1; /* Ensure content is on top */
color: white;
text-align: center;
padding: 20px;
}
</style>
</head>
<body>
<div id="particles-js"></div>
<div class="content">
<h1>Welcome to My Website</h1>
<p>This is a simple example with a particles.js background.</p>
</div>
<script>
// Check if particlesJS is a global function if it is included in your environment
if (typeof particlesJS !== 'undefined') {
particlesJS("particles-js", {
particles: {
number: {
value: 80,
density: {
enable: true,
value_area: 800
}
},
color: {
value: "#ffffff"
},
shape: {
type: "circle",
stroke: {
width: 0,
color: "#000000"
},
polygon: {
nb_sides: 5
}
},
opacity: {
value: 0.5,
random: false,
anim: {
enable: false,
speed: 1,
opacity_min: 0.1,
sync: false
}
},
size: {
value: 5,
random: true,
anim: {
enable: false,
speed: 40,
size_min: 0.1,
sync: false
}
},
line_linked: {
enable: true,
distance: 150,
color: "#ffffff",
opacity: 0.4,
width: 1
},
move: {
enable: true,
speed: 6,
direction: "none",
random: false,
straight: false,
out_mode: "out",
bounce: false,
}
},
interactivity: {
detect_on: "canvas",
events: {
onhover: {
enable: true,
mode: "repulse"
},
onclick: {
enable: true,
mode: "push"
},
resize: true
},
modes: {
grab: {
distance: 400,
line_linked: {
opacity: 1
}
},
bubble: {
distance: 400,
size: 40,
duration: 2,
opacity: 8,
speed: 3
},
repulse: {
distance: 200,
duration: 2
},
push: {
particles_nb: 4
},
remove: {
particles_nb: 2
}
}
},
retina_detect: true
});
} else {
console.error('particles.js is not defined. Please ensure it is included in your project.');
}
</script>
</body>
</html>
Explanation:
-
DOM Structure: The HTML skeleton includes a <div> for the particles and another <div> for text content.
-
CSS Styles: The CSS ensures the particle canvas occupies the full screen behind any content.
-
Script Section: The JavaScript checks if particlesJS is defined in your environment. If it's included directly (e.g., through a bundler that exposes it globally), it initializes it with the configuration provided.
Important Note:
-
Ensure particles.js is Loaded: If particles.js is already part of your JavaScript environment or available globally (such as through an earlier script tag added by your framework or bundler), this code will work directly.
-
Error Handling: The code includes a console error message to notify you if particles.js is not available.
-
Customization: Feel free to adjust the properties and configurations for particles (like size, color, movement behavior, etc.) according to your preferences.
Running the Example:
- Save this code to an
index.html file.
- Open it directly in your web browser where
particles.js is already available in the JavaScript context.
- You should see a particles background behind your content.
If you have further specifics about how particles.js is integrated or what environment you are working in, please provide those details for more tailored assistance!