Spaces:
Running
Running
File size: 8,364 Bytes
3e44316 | 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | // CyberVanguard - Main JavaScript
// Theme Toggle
const initTheme = () => {
const theme = localStorage.getItem('theme') || 'dark';
document.documentElement.setAttribute('data-theme', theme);
return theme;
};
const toggleTheme = () => {
const current = document.documentElement.getAttribute('data-theme');
const next = current === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', next);
localStorage.setItem('theme', next);
};
// Matrix Rain Effect
class MatrixRain {
constructor(canvas) {
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
this.chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ$#@%&*';
this.drops = [];
this.fontSize = 14;
this.init();
}
init() {
this.resize();
window.addEventListener('resize', () => this.resize());
const columns = Math.floor(this.canvas.width / this.fontSize);
this.drops = Array(columns).fill(1);
this.animate();
}
resize() {
this.canvas.width = this.canvas.scrollWidth;
this.canvas.height = this.canvas.scrollHeight;
}
animate() {
this.ctx.fillStyle = 'rgba(2, 6, 23, 0.05)';
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
this.ctx.fillStyle = '#22c55e';
this.ctx.font = `${this.fontSize}px JetBrains Mono`;
for (let i = 0; i < this.drops.length; i++) {
const char = this.chars[Math.floor(Math.random() * this.chars.length)];
const x = i * this.fontSize;
const y = this.drops[i] * this.fontSize;
// Randomize colors for visual interest
const hue = Math.random() > 0.95 ? 340 : 142; // Green or pink
this.ctx.fillStyle = `hsl(${hue}, 70%, ${Math.random() > 0.5 ? 60 : 40}%)`;
this.ctx.fillText(char, x, y);
if (y > this.canvas.height && Math.random() > 0.975) {
this.drops[i] = 0;
}
this.drops[i]++;
}
this.animationId = requestAnimationFrame(() => this.animate());
}
destroy() {
cancelAnimationFrame(this.animationId);
}
}
// Counter Animation
const animateCounters = () => {
const counters = document.querySelectorAll('[data-count]');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const counter = entry.target;
const target = parseInt(counter.dataset.count);
const duration = 2000;
const step = target / (duration / 16);
let current = 0;
const updateCounter = () => {
current += step;
if (current < target) {
counter.textContent = Math.floor(current).toLocaleString();
requestAnimationFrame(updateCounter);
} else {
counter.textContent = target.toLocaleString() + (target < 100 ? '%' : '+');
}
};
updateCounter();
observer.unobserve(counter);
}
});
}, { threshold: 0.5 });
counters.forEach(counter => observer.observe(counter));
};
// Scroll Animations
const initScrollAnimations = () => {
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-slide-up');
observer.unobserve(entry.target);
}
});
}, observerOptions);
document.querySelectorAll('.hover-lift').forEach(el => {
el.classList.add('opacity-0', 'translate-y-4');
observer.observe(el);
});
};
// Terminal Interaction
class TerminalController {
constructor(element) {
this.element = element;
this.history = [];
this.historyIndex = -1;
this.currentLine = '';
this.isTyping = false;
}
async typeLine(text, type = 'command') {
const line = document.createElement('div');
line.className = 'terminal-line';
if (type === 'command') {
line.innerHTML = `<span class="terminal-prompt">student@cybervanguard:~$</span> `;
}
this.element.appendChild(line);
for (let i = 0; i < text.length; i++) {
await this.delay(30);
if (type === 'command') {
line.innerHTML += `<span class="terminal-command">${text[i]}</span>`;
} else {
line.innerHTML += `<span class="terminal-output">${text[i]}</span>`;
}
}
this.element.scrollTop = this.element.scrollHeight;
}
delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async runDemo() {
const commands = [
{ cmd: 'whoami', out: 'student' },
{ cmd: 'nmap -sV 192.168.1.0/24', out: 'Starting Nmap 7.94...\nScanning 256 hosts...\nFound: 192.168.1.1 (router)\nFound: 192.168.1.100 (web-server)\nPorts: 22/ssh, 80/http, 443/https open' },
{ cmd: 'enum4linux -a 192.168.1.100', out: 'Starting enum4linux...\n[+] Got domain/workgroup name: CYBERLAB\n[+] OS: Windows Server 2019\n[+] Users: admin, backup_svc, guest' },
{ cmd: 'python3 exploit.py --target 192.168.1.100', out: '[*] Checking target vulnerability...\n[+] Target is vulnerable to CVE-2023-1234\n[*] Sending payload...\n[+] Got shell! spawn /bin/bash\nstudent@web-server:~$' }
];
for (const { cmd, out } of commands) {
await this.typeLine(cmd, 'command');
await this.delay(500);
await this.typeLine(out, 'output');
await this.delay(800);
}
// Add blinking cursor
const cursor = document.createElement('span');
cursor.className = 'terminal-cursor';
this.element.lastElementChild?.appendChild(cursor);
}
}
// Navigation
const initNavigation = () => {
let lastScroll = 0;
const navbar = document.querySelector('cyber-navbar');
window.addEventListener('scroll', () => {
const current = window.pageYOffset;
if (current > 100) {
navbar?.classList.add('scrolled');
} else {
navbar?.classList.remove('scrolled');
}
lastScroll = current;
});
};
// Mobile Menu
const initMobileMenu = () => {
const toggle = document.querySelector('[data-menu-toggle]');
const menu = document.querySelector('[data-mobile-menu]');
toggle?.addEventListener('click', () => {
menu?.classList.toggle('hidden');
toggle.setAttribute('aria-expanded',
menu?.classList.contains('hidden') ? 'false' : 'true'
);
});
};
// Initialize
document.addEventListener('DOMContentLoaded', () => {
// Initialize theme
initTheme();
// Initialize Matrix effect if canvas exists
const matrixCanvas = document.getElementById('matrix-canvas');
if (matrixCanvas) {
new MatrixRain(matrixCanvas);
}
// Initialize counters
animateCounters();
// Initialize scroll animations
initScrollAnimations();
// Initialize navigation
initNavigation();
// Initialize mobile menu
initMobileMenu();
// Initialize terminal if exists
const terminalBody = document.querySelector('[data-terminal-body]');
if (terminalBody) {
const terminal = new TerminalController(terminalBody);
setTimeout(() => terminal.runDemo(), 1000);
}
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
});
});
});
// Export for use in components
window.CyberVanguard = {
MatrixRain,
TerminalController,
toggleTheme,
initTheme
}; |