Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Canvas Particle Game</title>
- <!-- Tailwind CSS for a modern, responsive design -->
- <script src="https://cdn.tailwindcss.com"></script>
- <style>
- /* Custom styles for the game and body */
- body {
- background-color: #0d1117;
- font-family: 'Inter', sans-serif;
- color: #c9d1d9;
- display: flex;
- justify-content: center;
- align-items: center;
- height: 100vh;
- margin: 0;
- overflow: hidden;
- }
- .game-container {
- display: flex;
- flex-direction: column;
- align-items: center;
- padding: 20px;
- max-width: 90%;
- width: 800px;
- }
- h1 {
- font-size: 2rem;
- font-weight: 600;
- margin-bottom: 1rem;
- text-align: center;
- }
- p {
- text-align: center;
- max-width: 600px;
- margin-bottom: 2rem;
- }
- canvas {
- background-color: #161b22;
- border: 2px solid #30363d;
- border-radius: 12px;
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2), 0 10px 20px rgba(0, 0, 0, 0.15);
- touch-action: none;
- width: 100%;
- height: auto;
- max-height: 600px;
- aspect-ratio: 4 / 3;
- }
- </style>
- </head>
- <body class="bg-gray-900 text-gray-200">
- <div class="game-container">
- <h1>Canvas Particle System</h1>
- <p>
- Watch the colorful particles bounce and interact. This version includes a fix for the gradient color error, ensuring a smooth and continuous animation.
- </p>
- <canvas id="gameCanvas"></canvas>
- </div>
- <script>
- // --- Canvas and Context Setup ---
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- const dpr = window.devicePixelRatio || 1; // Device pixel ratio for sharp rendering on high-DPI screens
- // Adjust canvas size to be responsive and high-resolution
- function resizeCanvas() {
- const container = document.querySelector('.game-container');
- const newWidth = container.offsetWidth;
- const newHeight = newWidth * 0.75; // Maintain a 4:3 aspect ratio
- canvas.style.width = `${newWidth}px`;
- canvas.style.height = `${newHeight}px`;
- canvas.width = newWidth * dpr;
- canvas.height = newHeight * dpr;
- ctx.scale(dpr, dpr);
- }
- window.addEventListener('resize', resizeCanvas);
- resizeCanvas();
- // --- Particle Class ---
- class Particle {
- constructor(x, y, radius) {
- this.x = x;
- this.y = y;
- this.radius = radius;
- this.vx = (Math.random() - 0.5) * 2; // Velocity on x-axis
- this.vy = (Math.random() - 0.5) * 2; // Velocity on y-axis
- this.hue = Math.random() * 360; // Initial hue for color
- this.color = `hsl(${this.hue}, 80%, 60%)`;
- }
- // Update the particle's position
- update() {
- this.x += this.vx;
- this.y += this.vy;
- // Bounce off the canvas edges
- if (this.x + this.radius > canvas.width / dpr || this.x - this.radius < 0) {
- this.vx = -this.vx;
- }
- if (this.y + this.radius > canvas.height / dpr || this.y - this.radius < 0) {
- this.vy = -this.vy;
- }
- }
- // Draw the particle with a radial gradient
- draw() {
- ctx.beginPath();
- // Create a radial gradient for a glowing effect
- const gradient = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.radius);
- // The fix: Ensure color stops have valid color strings.
- // The original error occurred if a variable used to create the color string
- // became NaN. By using fixed or carefully validated values, this is prevented.
- gradient.addColorStop(0, this.color);
- gradient.addColorStop(1, 'rgba(183, 10, 255, 0.8)');
- ctx.fillStyle = gradient;
- ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
- ctx.fill();
- ctx.closePath();
- }
- }
- // --- Game Loop and Initialization ---
- let particles = [];
- const numParticles = 50;
- function init() {
- // Clear any previous particles and create new ones
- particles = [];
- for (let i = 0; i < numParticles; i++) {
- const radius = Math.random() * 10 + 2;
- const x = Math.random() * (canvas.width / dpr - radius * 2) + radius;
- const y = Math.random() * (canvas.height / dpr - radius * 2) + radius;
- particles.push(new Particle(x, y, radius));
- }
- }
- function gameLoop() {
- // Clear the canvas for the next frame
- ctx.clearRect(0, 0, canvas.width / dpr, canvas.height / dpr);
- // Update and draw each particle
- particles.forEach(p => {
- p.update();
- p.draw();
- });
- // Request the next animation frame
- requestAnimationFrame(gameLoop);
- }
- // Start the game when the window has loaded
- window.onload = function() {
- init();
- gameLoop();
- };
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment