How to Create an Animated Particle Background with HTML & JavaScript (Video + Code)

Creating visually appealing backgrounds can significantly enhance the user experience on your website. One popular and impressive technique is using animated particles. This tutorial will guide you through the process of creating a stunning animated particle background using HTML5 and JavaScript. By the end of this article, you’ll have a captivating background that can be easily integrated into any web project.

YouTube video

Why Use an Animated Particle Background?

Enhanced Visual Appeal

An animated particle background adds a dynamic and engaging element to your website. It captures the viewer’s attention and can make your site stand out from the competition. The movement and interaction of particles provide a modern, tech-savvy aesthetic that can enhance your site’s overall look and feel.

Improved User Experience

Interactive backgrounds can make your website more enjoyable to navigate. By creating a visually stimulating environment, you keep visitors engaged longer, which can lead to higher retention rates and a better overall user experience.

Easy Integration

Despite its sophisticated appearance, an animated particle background is relatively simple to implement. With the right guidance and code, you can integrate this feature into your website without extensive knowledge of complex programming.

How to Implement the Animated Particle Background

Step-by-Step Video Tutorial

To help you create this effect, we have prepared a detailed video tutorial. This video will walk you through each step, from setting up your HTML and CSS to adding the JavaScript necessary for the animation. Whether you are a beginner or an experienced developer, our tutorial is designed to be easy to follow.

Copy the Code

Ready to get started? You can copy the code provided below and paste it into your HTML file. This code includes all the necessary HTML, CSS, and JavaScript to create the animated particle background. Customize it to fit your needs and enjoy the enhanced visual appeal it brings to your website.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Animated Particle Background</title>
    <style>
        body, html {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
            background: #252839;
        }
        canvas {
            display: block;
        }
    </style>
</head>
<body>
    <canvas id="particleCanvas"></canvas>
    <script>
        const canvas = document.getElementById('particleCanvas');
        const ctx = canvas.getContext('2d');
        let particlesArray = [];

        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;

        class Particle {
            constructor(x, y, directionX, directionY, size, color) {
                this.x = x;
                this.y = y;
                this.directionX = directionX;
                this.directionY = directionY;
                this.size = size;
                this.color = color;
            }
            draw() {
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2, false);
                ctx.fillStyle = this.color;
                ctx.fill();
            }
            update() {
                if (this.x > canvas.width || this.x < 0) {
                    this.directionX = -this.directionX;
                }
                if (this.y > canvas.height || this.y < 0) {
                    this.directionY = -this.directionY;
                }
                this.x += this.directionX;
                this.y += this.directionY;
                this.draw();
            }
        }

        function init() {
            particlesArray = [];
            let numberOfParticles = (canvas.height * canvas.width) / 9000;
            for (let i = 0; i < numberOfParticles * 2; i++) {
                let size = (Math.random() * 5) + 1;
                let x = (Math.random() * (innerWidth - size * 2));
                let y = (Math.random() * (innerHeight - size * 2));
                let directionX = (Math.random() * 5) - 2.5;
                let directionY = (Math.random() * 5) - 2.5;
                let color = '#FFFFFF';

                particlesArray.push(new Particle(x, y, directionX, directionY, size, color));
            }
        }

        function animate() {
            requestAnimationFrame(animate);
            ctx.clearRect(0, 0, innerWidth, innerHeight);

            for (let i = 0; i < particlesArray.length; i++) {
                particlesArray[i].update();
            }
            connect();
        }

        function connect() {
            let opacityValue = 1;
            for (let a = 0; a < particlesArray.length; a++) {
                for (let b = a; b < particlesArray.length; b++) {
                    let distance = ((particlesArray[a].x - particlesArray[b].x) * (particlesArray[a].x - particlesArray[b].x))
                        + ((particlesArray[a].y - particlesArray[b].y) * (particlesArray[a].y - particlesArray[b].y));
                    if (distance < (canvas.width / 7) * (canvas.height / 7)) {
                        opacityValue = 1 - (distance / 20000);
                        ctx.strokeStyle = 'rgba(255,255,255,' + opacityValue + ')';
                        ctx.lineWidth = 1;
                        ctx.beginPath();
                        ctx.moveTo(particlesArray[a].x, particlesArray[a].y);
                        ctx.lineTo(particlesArray[b].x, particlesArray[b].y);
                        ctx.stroke();
                    }
                }
            }
        }

        window.addEventListener('resize', function () {
            canvas.width = innerWidth;
            canvas.height = innerHeight;
            init();
        });

        window.addEventListener('mouseout', function () {
            mouse.x = undefined;
            mouse.y = undefined;
        });

        init();
        animate();
    </script>
</body>
</html>

Conclusion

Implementing an animated particle background using HTML5 and JavaScript is a powerful way to enhance the visual appeal and user experience of your website. It is a straightforward process that can have a significant impact on how users perceive and interact with your site. Follow our video tutorial for a step-by-step guide and use the code provided below to create your own stunning animated background.

Feel free to copy the code below to implement this beautiful animated particle background on your website. Enjoy the new, dynamic look and improved user engagement!

Leave a Comment

Item added to cart.
0 items - $0.00