/* Una cinta sobre una curva fija que termina en la cuadrícula HTML original. El scroll siempre conserva el control. La galería funciona también sin JS. */ const clamp = (n, a = 0, b = 1) => Math.min(b, Math.max(a, n)); const mix = (a, b, t) => a + (b - a) * t; const span = (n, a, b) => clamp((n - a) / (b - a)); const smooth = t => t * t * (3 - 2 * t); const out = t => 1 - (1 - t) ** 3; const PERSPECTIVE = 1000; const RAIL_END = .68; const LANDING = .13; function cubic(a, b, c, d, t) { return .5 * ((2 * b) + (-a + c) * t + (2 * a - 5 * b + 4 * c - d) * t * t + (-a + 3 * b - 3 * c + d) * t * t * t); } function makeRail(width, height, compact = false) { // Posiciones en pantalla y profundidad: el paso cercano es un arco suave. const control = compact ? [ [1.15, .82, -1350], [.96, .8, -850], [.73, .75, 30], [.43, .66, 145], [.14, .57, 45], [-.13, .48, -340], [.025, .42, -1000], [.35, .39, -1400], [.72, .43, -1150], [1.16, .4, -900], ] : [ [1.15, .87, -1350], [.96, .87, -850], [.73, .79, 30], [.43, .73, 145], [.14, .64, 45], [-.13, .43, -340], [.025, .13, -1000], [.35, .085, -1400], [.72, .12, -1150], [1.16, .045, -900], ]; const points = []; let length = 0; const segments = control.length - 1; for (let i = 0; i <= segments * 72; i++) { const f = i / 72, segment = Math.min(segments - 1, Math.floor(f)), t = f - segment; const a = control[Math.max(0, segment - 1)], b = control[segment]; const c = control[segment + 1], d = control[Math.min(segments, segment + 2)]; const sx = cubic(a[0], b[0], c[0], d[0], t) * width; const sy = cubic(a[1], b[1], c[1], d[1], t) * height; const z = cubic(a[2], b[2], c[2], d[2], t); const depthScale = PERSPECTIVE / (PERSPECTIVE - z); const point = { x: width / 2 + (sx - width / 2) / depthScale, y: height / 2 + (sy - height / 2) / depthScale, z, distance: 0 }; const previous = points.at(-1); if (previous) length += Math.hypot(point.x - previous.x, point.y - previous.y, point.z - previous.z); point.distance = length; points.push(point); } return { points, length }; } function sample(rail, distance) { const points = rail.points; distance = clamp(distance, 0, rail.length); let low = 0, high = points.length - 1; while (high - low > 1) { const mid = (low + high) >> 1; if (points[mid].distance < distance) low = mid; else high = mid; } const a = points[low], b = points[high]; const t = (distance - a.distance) / Math.max(.0001, b.distance - a.distance); const dx = b.x - a.x, dy = b.y - a.y, dz = b.z - a.z; let angle = Math.atan2(dy, dx); if (angle > Math.PI / 2) angle -= Math.PI; if (angle < -Math.PI / 2) angle += Math.PI; return { x: mix(a.x, b.x, t), y: mix(a.y, b.y, t), z: mix(a.z, b.z, t), rz: clamp(angle * 180 / Math.PI * .7, -38, 38), ry: clamp(-Math.atan2(dz, Math.hypot(dx, dy)) * 180 / Math.PI * .8, -52, 52), rx: clamp(-dy / Math.max(1, Math.hypot(dx, dy)) * 10, -10, 10), }; } function initialiseSpacesRibbon() { const section = document.querySelector('.spaces-ribbon'); if (!section) return; const stage = section.querySelector('.spaces-stage'); const grid = section.querySelector('.spaces-grid'); const head = section.querySelector('.spaces-head'); const lines = [...section.querySelectorAll('.spaces-title > span')]; const skip = section.querySelector('.spaces-skip'); const items = [...section.querySelectorAll('.spaces-item')]; const usable = matchMedia('(min-width: 280px) and (min-height: 360px)'); const narrow = matchMedia('(max-width: 799px)'); const reduced = matchMedia('(prefers-reduced-motion: reduce)'); let enabled = false, active = false, keyboardSettled = false; let width = 1, height = 1, current = 0, rail, bases = [], cardWidth = 1, step = 1; let frameId = 0, measureId = 0, previousTime = 0; function progress() { if (keyboardSettled) return 1; const rect = section.getBoundingClientRect(); return clamp(-rect.top / Math.max(1, section.offsetHeight - height)); } function draw(p) { if (!enabled || !rail || !bases.length) return; const headingSettles = smooth(span(p, .28, .74)); head.style.transform = `translate3d(0,${((1 - headingSettles) * height * .16).toFixed(2)}px,0) scale(${(1 + (1 - headingSettles) * .12).toFixed(4)})`; const crossing = Math.sin(span(p, 0, .72) * Math.PI) * (1 - headingSettles) * width * .035; lines[0].style.transform = `translateX(${-crossing.toFixed(2)}px)`; lines[1].style.transform = `translateX(${crossing.toFixed(2)}px)`; const start = step * 3.8, finish = rail.length + (items.length - 1) * step; const headDistance = mix(start, finish, span(p, 0, RAIL_END)); items.forEach((item, index) => { const distance = headDistance - index * step; const base = bases[index]; if (distance < 0) { item.style.visibility = 'hidden'; item.style.pointerEvents = 'none'; return; } item.style.visibility = 'visible'; const arrival = RAIL_END * (rail.length + index * step - start) / (finish - start); const landing = out(span(p, arrival, arrival + LANDING)); const onRail = distance < rail.length; const pose = sample(rail, onRail ? distance : rail.length); const q = onRail ? 0 : landing; const x = mix(pose.x, base.x, q), y = mix(pose.y, base.y, q), z = mix(pose.z, 0, q); const sx = mix(cardWidth / base.w, 1, q), sy = mix((cardWidth / 1.55) / base.h, 1, q); const transform = `translate3d(${(x - base.x).toFixed(3)}px,${(y - base.y).toFixed(3)}px,${z.toFixed(3)}px) rotateZ(${(pose.rz * (1 - q)).toFixed(3)}deg) rotateY(${(pose.ry * (1 - q)).toFixed(3)}deg) rotateX(${(pose.rx * (1 - q)).toFixed(3)}deg) scale(${sx.toFixed(4)},${sy.toFixed(4)})`; item.style.transform = transform; item.style.opacity = span(distance, 0, cardWidth * .35).toFixed(3); item.style.setProperty('--details', smooth(span(q, .12, .8)).toFixed(3)); item.style.pointerEvents = q > .94 ? 'auto' : 'none'; }); } function measure() { if (!enabled) return; width = stage.clientWidth; height = stage.clientHeight; if (!width || !height) return; cardWidth = narrow.matches ? clamp(width * .65, 160, 250) : clamp(width * .255, 230, 370); step = cardWidth * 1.035; bases = items.map(item => ({ x: grid.offsetLeft + item.offsetLeft + item.offsetWidth / 2, y: grid.offsetTop + item.offsetTop + item.offsetHeight / 2, w: item.offsetWidth, h: item.offsetHeight })); rail = makeRail(width, height, narrow.matches || height < 620); current = progress(); draw(current); } function tick(time) { frameId = 0; if (!enabled || !active || document.hidden) return; const target = progress(); const dt = Math.min(48, previousTime ? time - previousTime : 16.67); previousTime = time; current += (target - current) * (1 - .91 ** (dt / 16.67)); if (Math.abs(target - current) < .00008) current = target; draw(current); if (current !== target) frameId = requestAnimationFrame(tick); } function wake() { if (!enabled || !active || frameId || document.hidden) return; previousTime = 0; frameId = requestAnimationFrame(tick); } function scheduleMeasure() { if (measureId) cancelAnimationFrame(measureId); measureId = requestAnimationFrame(() => { measureId = 0; measure(); wake(); }); } function clearTransforms() { head.style.removeProperty('transform'); lines.forEach(line => line.style.removeProperty('transform')); items.forEach(item => { ['transform', 'visibility', 'pointer-events', 'opacity', '--details'].forEach(prop => item.style.removeProperty(prop)); }); } function updateMode() { const next = usable.matches && !reduced.matches && navigator.connection?.saveData !== true; if (next === enabled) { if (enabled) scheduleMeasure(); return; } enabled = next; cancelAnimationFrame(frameId); frameId = 0; keyboardSettled = false; section.classList.toggle('is-animated', enabled); if (enabled) { measure(); wake(); } else clearTransforms(); } function settleForKeyboard(event) { if (!enabled || !event.target.matches(':focus-visible')) return; keyboardSettled = true; current = 1; draw(1); } skip.addEventListener('focus', settleForKeyboard); grid.addEventListener('focusin', settleForKeyboard); skip.addEventListener('click', () => { keyboardSettled = false; wake(); }); addEventListener('scroll', wake, { passive: true }); addEventListener('resize', updateMode, { passive: true }); [usable, narrow, reduced].forEach(media => media.addEventListener('change', updateMode)); navigator.connection?.addEventListener?.('change', updateMode); document.addEventListener('visibilitychange', () => { if (document.hidden) { cancelAnimationFrame(frameId); frameId = 0; } else wake(); }); new ResizeObserver(scheduleMeasure).observe(stage); new IntersectionObserver(([entry]) => { active = entry.isIntersecting; if (active) { section.querySelectorAll('img').forEach(image => { image.loading = 'eager'; }); wake(); } else { cancelAnimationFrame(frameId); frameId = 0; if (!section.contains(document.activeElement)) keyboardSettled = false; } }, { rootMargin: '180px' }).observe(section); updateMode(); } try { initialiseSpacesRibbon(); } catch (error) { // Conservar las fotos y los enlaces si la mejora no está disponible. const section = document.querySelector('.spaces-ribbon'); section?.classList.remove('is-animated'); section?.querySelectorAll('[style]').forEach(element => element.removeAttribute('style')); console.warn('La galería se muestra en su versión estática.', error); }