@patrick fix the smoothness of the animations please
This commit is contained in:
parent
fd57d47956
commit
6e98a0c133
3 changed files with 21 additions and 41 deletions
|
|
@ -7,34 +7,34 @@ class Meteor {
|
||||||
this.life = 1.0;
|
this.life = 1.0;
|
||||||
this.particles = [];
|
this.particles = [];
|
||||||
|
|
||||||
// Enhanced color palette
|
|
||||||
this.colorType = Math.floor(Math.random() * 4);
|
this.colorType = Math.floor(Math.random() * 4);
|
||||||
this.colors = this.getMeteorColors();
|
this.colors = this.getMeteorColors();
|
||||||
|
|
||||||
// Get container dimensions for pixel-based positioning
|
|
||||||
this.containerRect = container.getBoundingClientRect();
|
this.containerRect = container.getBoundingClientRect();
|
||||||
|
|
||||||
// Start from random edge with PIXEL-based positioning
|
// Start from random edge with PIXEL-based positioning
|
||||||
const edge = Math.floor(Math.random() * 4);
|
const edge = Math.floor(Math.random() * 4);
|
||||||
if (edge === 0) {
|
switch (edge) {
|
||||||
// Top
|
case 0:
|
||||||
this.x = Math.random() * this.containerRect.width;
|
// Top
|
||||||
this.y = -20;
|
this.x = Math.random() * this.containerRect.width;
|
||||||
} else if (edge === 1) {
|
this.y = -20;
|
||||||
// Right
|
case 1:
|
||||||
this.x = this.containerRect.width + 20;
|
// Right
|
||||||
this.y = Math.random() * this.containerRect.height;
|
this.x = this.containerRect.width + 20;
|
||||||
} else if (edge === 2) {
|
this.y = Math.random() * this.containerRect.height;
|
||||||
// Bottom
|
case 2:
|
||||||
this.x = Math.random() * this.containerRect.width;
|
// Bottom
|
||||||
this.y = this.containerRect.height + 20;
|
this.x = Math.random() * this.containerRect.width;
|
||||||
} else {
|
this.y = this.containerRect.height + 20;
|
||||||
// Left
|
case 3:
|
||||||
this.x = -20;
|
// Left
|
||||||
this.y = Math.random() * this.containerRect.height;
|
this.x = -20;
|
||||||
|
this.y = Math.random() * this.containerRect.height;
|
||||||
|
default:
|
||||||
|
console.log("Error creating Meteor direction");
|
||||||
}
|
}
|
||||||
|
|
||||||
// More dynamic movement patterns
|
|
||||||
const centerX = this.containerRect.width / 2 + (Math.random() - 0.5) * 200;
|
const centerX = this.containerRect.width / 2 + (Math.random() - 0.5) * 200;
|
||||||
const centerY = this.containerRect.height / 2 + (Math.random() - 0.5) * 200;
|
const centerY = this.containerRect.height / 2 + (Math.random() - 0.5) * 200;
|
||||||
this.angle =
|
this.angle =
|
||||||
|
|
@ -84,7 +84,6 @@ class Meteor {
|
||||||
const size = Math.random() * 4 + 10;
|
const size = Math.random() * 4 + 10;
|
||||||
const colors = this.colors;
|
const colors = this.colors;
|
||||||
|
|
||||||
// Create enhanced meteor head with multiple layers
|
|
||||||
const head = document.createElement("div");
|
const head = document.createElement("div");
|
||||||
head.style.width = `${size}px`;
|
head.style.width = `${size}px`;
|
||||||
head.style.height = `${size}px`;
|
head.style.height = `${size}px`;
|
||||||
|
|
@ -127,7 +126,6 @@ class Meteor {
|
||||||
|
|
||||||
this.container.appendChild(this.element);
|
this.container.appendChild(this.element);
|
||||||
|
|
||||||
// Add trail particles
|
|
||||||
this.createTrailParticles();
|
this.createTrailParticles();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -159,7 +157,6 @@ class Meteor {
|
||||||
}
|
}
|
||||||
|
|
||||||
createTrailParticles() {
|
createTrailParticles() {
|
||||||
// Create initial trail particles
|
|
||||||
for (let i = 0; i < 5; i++) {
|
for (let i = 0; i < 5; i++) {
|
||||||
this.addTrailParticle();
|
this.addTrailParticle();
|
||||||
}
|
}
|
||||||
|
|
@ -196,12 +193,10 @@ class Meteor {
|
||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
update() {
|
||||||
// Smooth pixel-based movement
|
|
||||||
this.x += this.dx;
|
this.x += this.dx;
|
||||||
this.y += this.dy;
|
this.y += this.dy;
|
||||||
this.life -= 0.005;
|
this.life -= 0.005;
|
||||||
|
|
||||||
// Use pixel positioning for smooth movement
|
|
||||||
this.element.style.left = `${this.x}px`;
|
this.element.style.left = `${this.x}px`;
|
||||||
this.element.style.top = `${this.y}px`;
|
this.element.style.top = `${this.y}px`;
|
||||||
this.element.style.opacity = this.life;
|
this.element.style.opacity = this.life;
|
||||||
|
|
@ -216,7 +211,6 @@ class Meteor {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add new trail particles periodically
|
|
||||||
if (Math.random() < 0.3) {
|
if (Math.random() < 0.3) {
|
||||||
this.addTrailParticle();
|
this.addTrailParticle();
|
||||||
}
|
}
|
||||||
|
|
@ -239,7 +233,6 @@ class Meteor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enhanced removal condition with fading
|
|
||||||
if (
|
if (
|
||||||
this.x < -50 ||
|
this.x < -50 ||
|
||||||
this.x > this.containerRect.width + 50 ||
|
this.x > this.containerRect.width + 50 ||
|
||||||
|
|
@ -260,7 +253,6 @@ class Meteor {
|
||||||
}
|
}
|
||||||
|
|
||||||
remove() {
|
remove() {
|
||||||
// Add explosion effect when meteor dies
|
|
||||||
if (this.life > 0.3) {
|
if (this.life > 0.3) {
|
||||||
this.createExplosion();
|
this.createExplosion();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,6 @@ class Star {
|
||||||
this.x = Math.random() * 100;
|
this.x = Math.random() * 100;
|
||||||
this.y = Math.random() * 100;
|
this.y = Math.random() * 100;
|
||||||
|
|
||||||
// Add color variation
|
|
||||||
this.color = this.getRandomStarColor();
|
this.color = this.getRandomStarColor();
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,12 @@
|
||||||
import Star from "./Star.js";
|
import Star from "./Star.js";
|
||||||
import Meteor from "./Meteor.js";
|
import Meteor from "./Meteor.js";
|
||||||
|
|
||||||
// State
|
|
||||||
const starInstances = [];
|
const starInstances = [];
|
||||||
const meteorInstances = [];
|
const meteorInstances = [];
|
||||||
|
|
||||||
// Enhanced star creation with more stars
|
|
||||||
function createStars() {
|
function createStars() {
|
||||||
const stars = document.getElementById("stars");
|
const stars = document.getElementById("stars");
|
||||||
const count = 400; // More stars for richer background
|
const count = 400;
|
||||||
stars.innerHTML = "";
|
stars.innerHTML = "";
|
||||||
starInstances.length = 0;
|
starInstances.length = 0;
|
||||||
|
|
||||||
|
|
@ -18,7 +16,6 @@ function createStars() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inject required CSS for star animations
|
|
||||||
function injectStarCSS() {
|
function injectStarCSS() {
|
||||||
const style = document.createElement("style");
|
const style = document.createElement("style");
|
||||||
style.textContent = `
|
style.textContent = `
|
||||||
|
|
@ -36,9 +33,8 @@ function injectStarCSS() {
|
||||||
document.head.appendChild(style);
|
document.head.appendChild(style);
|
||||||
}
|
}
|
||||||
|
|
||||||
// More frequent meteor spawning with variable rates
|
|
||||||
function trySpawnMeteor() {
|
function trySpawnMeteor() {
|
||||||
const spawnChance = 0.12; // Increased spawn rate
|
const spawnChance = 0.12;
|
||||||
if (Math.random() < spawnChance) {
|
if (Math.random() < spawnChance) {
|
||||||
const stars = document.getElementById("stars");
|
const stars = document.getElementById("stars");
|
||||||
const newMeteor = new Meteor(stars);
|
const newMeteor = new Meteor(stars);
|
||||||
|
|
@ -46,7 +42,6 @@ function trySpawnMeteor() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enhanced animation loop
|
|
||||||
function animateStars() {
|
function animateStars() {
|
||||||
starInstances.forEach((star) => star.update());
|
starInstances.forEach((star) => star.update());
|
||||||
|
|
||||||
|
|
@ -60,7 +55,6 @@ function animateStars() {
|
||||||
requestAnimationFrame(animateStars);
|
requestAnimationFrame(animateStars);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Meteor burst effect
|
|
||||||
function createMeteorBurst() {
|
function createMeteorBurst() {
|
||||||
for (let i = 0; i < 3; i++) {
|
for (let i = 0; i < 3; i++) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
|
@ -71,20 +65,15 @@ function createMeteorBurst() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize everything
|
|
||||||
function init() {
|
function init() {
|
||||||
injectStarCSS();
|
injectStarCSS();
|
||||||
createStars();
|
createStars();
|
||||||
animateStars();
|
animateStars();
|
||||||
|
|
||||||
// More frequent meteor spawning
|
|
||||||
setInterval(trySpawnMeteor, 2000);
|
setInterval(trySpawnMeteor, 2000);
|
||||||
|
|
||||||
// Add occasional meteor bursts
|
|
||||||
setInterval(createMeteorBurst, 15000);
|
setInterval(createMeteorBurst, 15000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start when DOM is ready
|
|
||||||
if (document.readyState === "loading") {
|
if (document.readyState === "loading") {
|
||||||
document.addEventListener("DOMContentLoaded", init);
|
document.addEventListener("DOMContentLoaded", init);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue