-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
367 lines (310 loc) · 13 KB
/
Copy pathscripts.js
File metadata and controls
367 lines (310 loc) · 13 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* ============================================================
SCRIPTS.JS — Portafolio de Alejo De Miguel
Manejo centralizado de navegación, menú hamburguesa,
scroll suave, animaciones de entrada y pantalla de carga.
============================================================ */
document.addEventListener("DOMContentLoaded", function () {
/* ----------------------------------------------------------
0. SELECCIÓN DE TEMA (CLARO / OSCURO)
Manejo de la alternancia de tema, iconos y persistencia.
---------------------------------------------------------- */
const themeToggleBtn = document.getElementById("theme-toggle");
const themeMeta = document.getElementById("theme-meta");
function updateThemeUI(theme) {
if (!themeToggleBtn) return;
const icon = themeToggleBtn.querySelector("i");
if (icon) {
if (theme === "light") {
icon.className = "fa-solid fa-sun";
} else {
icon.className = "fa-solid fa-moon";
}
}
}
// Inicializar el estado del botón según el tema actual (ya aplicado en head)
const currentTheme = document.documentElement.getAttribute("data-theme") || "dark";
updateThemeUI(currentTheme);
if (themeToggleBtn) {
themeToggleBtn.addEventListener("click", function () {
const theme = document.documentElement.getAttribute("data-theme") === "light" ? "dark" : "light";
document.documentElement.setAttribute("data-theme", theme);
localStorage.setItem("theme", theme);
updateThemeUI(theme);
if (themeMeta) {
themeMeta.setAttribute("content", theme === "dark" ? "#0a0a0f" : "#f5f7fb");
}
// Notificar al Canvas que el tema cambió
const event = new CustomEvent("themeChanged", { detail: { theme: theme } });
document.dispatchEvent(event);
});
}
/* ----------------------------------------------------------
1. PANTALLA DE CARGA
Oculta la pantalla de carga una vez que todo el contenido
(incluyendo imágenes) haya terminado de cargar.
---------------------------------------------------------- */
const loadingScreen = document.getElementById("loading-screen");
window.addEventListener("load", function () {
setTimeout(function () {
loadingScreen.classList.add("loaded");
}, 300);
});
/* ----------------------------------------------------------
2. MENÚ HAMBURGUESA (MÓVIL)
Toggle del menú en pantallas pequeñas con animación.
Cierra el menú al hacer click en un enlace.
---------------------------------------------------------- */
const hamburgerBtn = document.getElementById("hamburger-btn");
const navMenu = document.getElementById("nav-menu");
const navLinks = document.querySelectorAll(".nav-link");
hamburgerBtn.addEventListener("click", function () {
const isOpen = navMenu.classList.toggle("open");
hamburgerBtn.classList.toggle("active");
hamburgerBtn.setAttribute("aria-expanded", isOpen);
});
// Cerrar menú al hacer click en un enlace de navegación
navLinks.forEach(function (link) {
link.addEventListener("click", function () {
navMenu.classList.remove("open");
hamburgerBtn.classList.remove("active");
hamburgerBtn.setAttribute("aria-expanded", "false");
});
});
/* ----------------------------------------------------------
3. SCROLL SUAVE
Al hacer click en los enlaces del nav, realiza un scroll
suave hasta la sección correspondiente, compensando
la altura del header sticky.
---------------------------------------------------------- */
navLinks.forEach(function (link) {
link.addEventListener("click", function (e) {
e.preventDefault();
const targetId = this.getAttribute("href").substring(1);
const targetSection = document.getElementById(targetId);
if (targetSection) {
const headerHeight = document.getElementById("site-header").offsetHeight;
const targetPosition = targetSection.offsetTop - headerHeight;
window.scrollTo({
top: targetPosition,
behavior: "smooth"
});
}
});
});
/* ----------------------------------------------------------
4. NAVEGACIÓN ACTIVA CON INTERSECTION OBSERVER
Resalta automáticamente el enlace del nav correspondiente
a la sección visible en pantalla durante el scroll.
---------------------------------------------------------- */
const sections = document.querySelectorAll(".section");
const navObserver = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
// Quitar clase activa de todos los links
navLinks.forEach(function (link) {
link.classList.remove("active");
});
// Agregar clase activa al link correspondiente
const activeLink = document.querySelector(
'.nav-link[data-section="' + entry.target.id + '"]'
);
if (activeLink) {
activeLink.classList.add("active");
}
}
});
}, {
threshold: 0.3,
rootMargin: "-70px 0px -30% 0px"
});
sections.forEach(function (section) {
navObserver.observe(section);
});
/* ----------------------------------------------------------
5. ANIMACIONES DE ENTRADA AL SCROLL
Utiliza IntersectionObserver para activar animaciones
de fade-in y slide-up cuando los elementos entran
al viewport del usuario.
---------------------------------------------------------- */
const animatedElements = document.querySelectorAll(".animate-on-scroll");
const scrollAnimationObserver = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
entry.target.classList.add("visible");
// Dejar de observar una vez animado para mejorar performance
scrollAnimationObserver.unobserve(entry.target);
}
});
}, {
threshold: 0.1,
rootMargin: "0px 0px -50px 0px"
});
animatedElements.forEach(function (el) {
scrollAnimationObserver.observe(el);
});
/* ----------------------------------------------------------
6. HEADER COMPACTO AL HACER SCROLL
Reduce el padding del header cuando el usuario
hace scroll hacia abajo para dar más espacio al contenido.
---------------------------------------------------------- */
const header = document.getElementById("site-header");
let lastScroll = 0;
window.addEventListener("scroll", function () {
const currentScroll = window.pageYOffset;
if (currentScroll > 50) {
header.classList.add("scrolled");
} else {
header.classList.remove("scrolled");
}
lastScroll = currentScroll;
});
});
/* ============================================================
7. FONDO TECNOLÓGICO ANIMADO (Canvas)
Dibuja una grilla sutil con nodos en las intersecciones
y partículas que viajan por las líneas, estilo circuito.
============================================================ */
(function () {
var canvas = document.getElementById("tech-bg");
if (!canvas) return;
var ctx = canvas.getContext("2d");
var gridSize = 60;
var nodes = [];
var particles = [];
var maxParticles = 28;
// Colores del tema (se inicializan leyendo de las variables CSS)
var accentColor = "108, 99, 255";
var secondaryColor = "0, 212, 170";
var gridLineOpacity = 0.12;
function updateColors() {
var style = getComputedStyle(document.documentElement);
var accent = style.getPropertyValue('--color-accent-rgb');
var secondary = style.getPropertyValue('--color-secondary-rgb');
var opacity = style.getPropertyValue('--grid-line-opacity');
if (accent) accentColor = accent.trim();
if (secondary) secondaryColor = secondary.trim();
if (opacity) gridLineOpacity = parseFloat(opacity.trim());
}
// Escuchar el evento de cambio de tema
document.addEventListener("themeChanged", function () {
updateColors();
// Actualizar partículas existentes con los nuevos colores de tema
particles.forEach(function (p) {
var useAccent = Math.random() > 0.5;
p.color = useAccent ? accentColor : secondaryColor;
});
});
// Inicializar colores al cargar
updateColors();
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
initNodes();
}
// Crear nodos en las intersecciones de la grilla
function initNodes() {
nodes = [];
var cols = Math.ceil(canvas.width / gridSize) + 1;
var rows = Math.ceil(canvas.height / gridSize) + 1;
for (var r = 0; r < rows; r++) {
for (var c = 0; c < cols; c++) {
nodes.push({
x: c * gridSize,
y: r * gridSize,
pulse: Math.random() * Math.PI * 2,
speed: 0.005 + Math.random() * 0.01
});
}
}
}
// Crear una partícula que viaja por las líneas de la grilla
function spawnParticle() {
if (particles.length >= maxParticles) return;
var startNode = nodes[Math.floor(Math.random() * nodes.length)];
// Elegir dirección: 0=derecha, 1=abajo, 2=izquierda, 3=arriba
var dir = Math.floor(Math.random() * 4);
var dx = [1, 0, -1, 0][dir];
var dy = [0, 1, 0, -1][dir];
var useAccent = Math.random() > 0.5;
particles.push({
x: startNode.x,
y: startNode.y,
dx: dx,
dy: dy,
progress: 0,
color: useAccent ? accentColor : secondaryColor,
life: 1
});
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Dibujar líneas de la grilla
ctx.strokeStyle = "rgba(" + accentColor + ", " + gridLineOpacity + ")";
ctx.lineWidth = 0.5;
// Líneas verticales
for (var x = 0; x <= canvas.width; x += gridSize) {
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, canvas.height);
ctx.stroke();
}
// Líneas horizontales
for (var y = 0; y <= canvas.height; y += gridSize) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(canvas.width, y);
ctx.stroke();
}
// Dibujar nodos pulsantes en las intersecciones
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
node.pulse += node.speed;
var glow = (gridLineOpacity * 0.66) + Math.sin(node.pulse) * (gridLineOpacity * 0.58);
ctx.beginPath();
ctx.arc(node.x, node.y, 1.5, 0, Math.PI * 2);
ctx.fillStyle = "rgba(" + accentColor + ", " + glow + ")";
ctx.fill();
}
// Dibujar y actualizar partículas viajeras
for (var j = particles.length - 1; j >= 0; j--) {
var p = particles[j];
p.progress += 0.8;
var px = p.x + p.dx * p.progress;
var py = p.y + p.dy * p.progress;
// Trail de la partícula
var trailLen = 20;
var gradient = ctx.createLinearGradient(
px - p.dx * trailLen, py - p.dy * trailLen,
px, py
);
gradient.addColorStop(0, "rgba(" + p.color + ", 0)");
gradient.addColorStop(1, "rgba(" + p.color + ", 0.35)");
ctx.beginPath();
ctx.moveTo(px - p.dx * trailLen, py - p.dy * trailLen);
ctx.lineTo(px, py);
ctx.strokeStyle = gradient;
ctx.lineWidth = 1.5;
ctx.stroke();
// Punto brillante en la cabeza
ctx.beginPath();
ctx.arc(px, py, 2, 0, Math.PI * 2);
ctx.fillStyle = "rgba(" + p.color + ", 0.6)";
ctx.fill();
// Eliminar cuando sale de la celda
if (p.progress >= gridSize) {
p.life -= 0.3;
if (p.life <= 0 || px < -20 || px > canvas.width + 20 || py < -20 || py > canvas.height + 20) {
particles.splice(j, 1);
}
}
}
// Generar nuevas partículas ocasionalmente
if (Math.random() < 0.05) {
spawnParticle();
}
requestAnimationFrame(draw);
}
window.addEventListener("resize", resize);
resize();
draw();
})();