Sure! Hosting particles.js on Cloudflare Pages is a great way to add animated backgrounds or effects to your static site. Here’s a step-by-step guide to help you do that:
✅ What You’ll Need
A working Cloudflare Pages project.
The particles.js library (either via CDN or by including the file manually).
A basic HTML page where you want to use particles.js.
🚀 Option 1: Use CDN (Quickest Way)
1. Create Your Project Structure
Your project directory might look like this:
code Copy code
my-particles-site/
├── index.html
├── style.css (optional)
└── script.js (optional, for initialization)
2. Add particles.js from CDN in index.html
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Particles.js on Cloudflare Pages</title>
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
}
#particles-js {
width: 100%;
height: 100%;
background-color: #000;
}
</style>
</head>
<body>
<div id="particles-js"></div>
<!-- particles.js library -->
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<!-- Initialization script -->
<script>
particlesJS.load('particles-js', 'particles.json', function() {
console.log('callback - particles.js config loaded');
});
</script>
</body>
</html>
Wait — we’re referencing particles.json, but that file isn’t included yet.
3. Add Configuration File particles.json
Create a particles.json file in your root folder with a sample config:
json Copy code
{
"particles": {
"number": {
"value": 80,
"density": {
"enable": true,
"value_area": 800
}
},
"color": {
"value": "#ffffff"
},
"shape": {
"type": "circle"
},
"opacity": {
"value": 0.5,
"random": false,
"anim": {
"enable": false
}
},
"size": {
"value": 3,
"random": true,
"anim": {
"enable": false
}
},
"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",
"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": 0.4
},
"push": {
"particles_nb": 4
},
"remove": {
"particles_nb": 2
}
}
},
"retina_detect": true
}
✅ This config will be loaded from your root directory along with the HTML file when deployed.
4. Deploy to Cloudflare Pages
Go to Cloudflare Dashboard .
Select your account → Workers & Pages .
Click Create a project (if not done), and link your GitHub/GitLab repo.
Set the build settings:
Build command : (leave empty – it’s a static site)
Build output directory : . (or leave empty if index.html is in root)
Click Save and Deploy .
🚀 Option 2: Host particles.js Locally
If you prefer not to use CDN:
Download particles.min.js from https://github.com/VincentGarreau/particles.js/
Place it in your project folder, e.g. js/particles.min.js
Update <script> tag in index.html:
html Copy code
<script src="/js/particles.min.js"></script>
The rest stays the same.
✅ Test Your Site
Once deployed, visit your Cloudflare Pages URL (e.g., https://yourusername.pages.dev) and you should see the animated particles background!
🧠 Tips
If you're using a framework like React or Vue, you can still include the script dynamically or via CDN in public/index.html.
Make sure particles.json is uploaded to Cloudflare Pages—it should be in the public root alongside index.html.
Need help modifying the particle effect or integrating it into a framework? Just ask!