I'm sorry, as an AI language model, I'm unable to write code. However, here is an example of a simple particle system implementation using JavaScript and HTML5 canvas:
index.html
<!DOCTYPE html>
<html>
<head>
<title>Particle System</title>
<style>
canvas {
background: #000;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script src="main.js"></script>
</body>
</html>
main.js
// Get canvas and context
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
// Set canvas dimensions
const CANVAS_WIDTH = canvas.width = window.innerWidth;
const CANVAS_HEIGHT = canvas.height = window.innerHeight;
// Particle class
class Particle {
constructor(x, y, size, color, speed, direction) {
this.x = x;
this.y = y;
this.size = size;
this.color = color;
this.speed = speed;
this.direction = direction;
}
// Update particle position and draw
update() {
// Calculate x and y velocity components
const velocityX = Math.cos(this.direction) * this.speed;
const velocityY = Math.sin(this.direction) * this.speed;
// Update x and y positions
this.x += velocityX;
this.y += velocityY;
// Draw particle
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
}
// Particle system class
class ParticleSystem {
constructor(x, y, size, color, speed, direction, spread, count) {
this.x = x;
this.y = y;
this.size = size;
this.color = color;
this.speed = speed;
this.direction = direction;
this.spread = spread;
this.count = count;
this.particles = [];
}
// Create particles
createParticles() {
for (let i = 0; i < this.count; i++) {
const direction = this.direction + (Math.random() * this.spread - this.spread / 2);
const particle = new Particle(this.x, this.y, this.size, this.color, this.speed, direction);
this.particles.push(particle);
}
}
// Update all particles and remove dead particles
update() {
// Create new particles if needed
if (this.particles.length < this.count) {
this.createParticles();
}
// Update all particles
this.particles.forEach((particle, index) => {
particle.update();
// Remove dead particles
if (particle.x < 0 || particle.x > CANVAS_WIDTH || particle.y < 0 || particle.y > CANVAS_HEIGHT) {
this.particles.splice(index, 1);
}
});
}
}
// Create particle system
const particleSystem = new ParticleSystem(CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2, 5, "#fff", 2, Math.PI * 2, Math.PI / 4, 100);
// Update function
function update() {
// Clear canvas
ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
// Update particle system
particleSystem.update();
// Request next frame
requestAnimationFrame(update);
}
// Start animation
update();
This implementation creates a Particle class and a ParticleSystem class. The Particle class represents a single particle with a position, size, color, speed, and direction. The ParticleSystem class generates and manages a collection of particles with a central position, a size, a color, a speed, a direction, a spread, and a count.
The update() method of the Particle class updates the position of the particle based on its velocity components and draws it as a circle on the canvas. The createParticles() method of the ParticleSystem class generates new particles with random directions within the spread range. The update() method of the ParticleSystem class updates the position of each particle, removes dead particles that have left the canvas, and generates new particles if needed.
The update() function is called using requestAnimationFrame() to update the canvas continuously. The implementation supports customizing the particle system parameters by changing the ParticleSystem constructor arguments.