1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
function interstellarTravel(option) { const { maxSpeed = 5, maxSize = 2, maxStars = 1000, canvas } = option; const ctx = canvas.getContext('2d'); let width = canvas.width = window.innerWidth; let height = canvas.height = window.innerHeight;
window.addEventListener('resize', () => { width = canvas.width = window.innerWidth; height = canvas.height = window.innerHeight; });
const colors = [ '#999999', '#333333', '#666666', '#fafafa', '#ffffff', '#ffffff', '#ffffff', '#ffffff', '#ffffff' ];
class Star { constructor() { this.x = width / 2; this.y = height / 2; this.angle = Math.random() * Math.PI * 2; this.speed = Math.max(Math.random() * maxSpeed, 0.01); this.size = Math.max(Math.random() * maxSize, 0.1); this.color = colors[Math.floor(Math.random() * colors.length)]; }
update() { this.x += Math.cos(this.angle) * this.speed; this.y += Math.sin(this.angle) * this.speed; }
draw() { if (this.size > 1 && Math.abs(this.x - width / 2) < 100 && Math.abs(this.y - height / 2) < 100) { return; } ctx.beginPath(); ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2); ctx.fillStyle = this.color; ctx.fill(); } }
const stars = []; const faseCount = 10000;
for (let i = 0; i < maxStars; i++) { stars.push(new Star()); }
function animate() { ctx.fillStyle = 'black'; ctx.fillRect(0, 0, width, height);
if (stars.length < maxStars) { stars.push(new Star()); }
stars.forEach((star, index) => { star.update(); star.draw(); if (star.x < 0 || star.x > width || star.y < 0 || star.y > height) { stars[index] = new Star(); } }); requestAnimationFrame(animate); }
for (let i = 0; i < faseCount; i++) { stars.forEach((star, index) => { star.update(); if (star.x < 0 || star.x > width || star.y < 0 || star.y > height) { stars[index] = new Star(); } }); } animate(); }
interstellarTravel({ canvas: document.getElementById('canvas') });
|