/** * Lubot · Hero — "red de conversación" (Canvas 2D). * Nodos que derivan lentamente y se conectan entre sí cuando están cerca, * como mensajes encontrando su acción. Un solo momento visual en la página; * el resto del sitio es tipografía. * * Por qué Canvas 2D y no WebGL: la página ya lleva un canvas WebGL (el personaje * de marca, lubot-guia.mjs). Dos contextos WebGL a pantalla completa agotan la * memoria GPU en entornos con poca VRAM (móviles modestos, integración, * renderizado por software) y el navegador pierde el contexto — el personaje * desaparecía. La red de puntos es geometría simple (círculos + líneas), así * que se proyecta a mano con la misma cámara del antiguo WebGL (FOV 55°, * z=42): mismo aspecto, un solo contexto WebGL en toda la página y cero * riesgo de context loss. */ const canvas = document.getElementById( 'lbt-hero-canvas' ); // Paletas por tema: en oscuro, blending aditivo sobre fondo casi negro; // en claro, blending normal con tonos más profundos para no "lavarse". const THEME_PALETTES = { dark: { turq: '#6ff0eb', purp: '#8a5bc7', line: '#6ff0eb', lineOpacity: 0.14, pointOpacity: 0.92 }, light: { turq: '#0f8b86', purp: '#6d3fb8', line: '#0f8b86', lineOpacity: 0.25, pointOpacity: 0.9 } }; function currentTheme() { return document.documentElement.getAttribute( 'data-theme' ) === 'dark' ? 'dark' : 'light'; } // --- Utilidades de color (equivalente a THREE.Color.offsetHSL) ----------------- function hexToRgb( hex ) { const n = parseInt( hex.slice( 1 ), 16 ); return [ ( n >> 16 ) & 255, ( n >> 8 ) & 255, n & 255 ]; } function rgbToHsl( r, g, b ) { r /= 255; g /= 255; b /= 255; const max = Math.max( r, g, b ), min = Math.min( r, g, b ); let h = 0, s = 0; const l = ( max + min ) / 2; if ( max !== min ) { const d = max - min; s = l > 0.5 ? d / ( 2 - max - min ) : d / ( max + min ); switch ( max ) { case r: h = ( g - b ) / d + ( g < b ? 6 : 0 ); break; case g: h = ( b - r ) / d + 2; break; default: h = ( r - g ) / d + 4; } h /= 6; } return [ h, s, l ]; } function hslToRgb( h, s, l ) { if ( s === 0 ) { const v = Math.round( l * 255 ); return [ v, v, v ]; } const q = l < 0.5 ? l * ( 1 + s ) : l + s - l * s; const p = 2 * l - q; const f = ( t ) => { if ( t < 0 ) t += 1; if ( t > 1 ) t -= 1; if ( t < 1 / 6 ) return p + ( q - p ) * 6 * t; if ( t < 1 / 2 ) return q; if ( t < 2 / 3 ) return p + ( q - p ) * ( 2 / 3 - t ) * 6; return p; }; return [ Math.round( f( h + 1 / 3 ) * 255 ), Math.round( f( h ) * 255 ), Math.round( f( h - 1 / 3 ) * 255 ) ]; } if ( canvas ) { init(); } function init() { const reduced = window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches; let ctx; try { ctx = canvas.getContext( '2d' ); } catch ( e ) { return; // sin 2D context: el hero sigue funcionando solo con tipografía } if ( ! ctx ) return; const FOV = 55, CAM_Z = 42; const FOCAL = 1 / Math.tan( ( FOV / 2 ) * Math.PI / 180 ); // factor de proyección let W = 0, H = 0, DPR = 1; // --- Nodos ------------------------------------------------------------- const N = 110; const basePos = new Float32Array( N * 3 ); const drift = new Float32Array( N * 3 ); const positions = new Float32Array( N * 3 ); // posiciones actuales (se actualizan por frame) // Por nodo: si es púrpura y su jitter de luminosidad. const nodePurp = []; const jitter = []; for ( let i = 0; i < N; i++ ) { // elipsoide achatado: la red se lee como una banda horizontal const r = 24 * Math.cbrt( Math.random() ); const theta = Math.random() * Math.PI * 2; const phi = Math.acos( 2 * Math.random() - 1 ); const x = r * Math.sin( phi ) * Math.cos( theta ); const y = r * Math.sin( phi ) * Math.sin( theta ) * 0.58; const z = r * Math.cos( phi ) * 0.72; basePos[ i * 3 ] = x; basePos[ i * 3 + 1 ] = y; basePos[ i * 3 + 2 ] = z; drift[ i * 3 ] = ( Math.random() - 0.5 ) * 0.9; drift[ i * 3 + 1 ] = ( Math.random() - 0.5 ) * 0.7; drift[ i * 3 + 2 ] = ( Math.random() - 0.5 ) * 0.6; nodePurp.push( Math.random() >= 0.72 ); jitter.push( ( Math.random() - 0.5 ) * 0.12 ); } // --- Conexiones (pares cercanos, topología fija como en la versión WebGL) -- const pairs = []; for ( let i = 0; i < N; i++ ) { for ( let j = i + 1; j < N; j++ ) { const dx = basePos[ i * 3 ] - basePos[ j * 3 ]; const dy = basePos[ i * 3 + 1 ] - basePos[ j * 3 + 1 ]; const dz = basePos[ i * 3 + 2 ] - basePos[ j * 3 + 2 ]; if ( dx * dx + dy * dy + dz * dz < 49 ) { pairs.push( [ i, j ] ); } // dist < 7 } } // --- Tema ------------------------------------------------------------------ let pal = THEME_PALETTES[ currentTheme() ]; const nodeColors = new Array( N ); // 'rgba(r,g,b,a)' por nodo y tema function paintNodes() { for ( let i = 0; i < N; i++ ) { const [ r, g, b ] = hexToRgb( nodePurp[ i ] ? pal.purp : pal.turq ); let [ h, s, l ] = rgbToHsl( r, g, b ); l = Math.min( 1, Math.max( 0, l + jitter[ i ] ) ); // offsetHSL(0,0,jitter) const c = hslToRgb( h, s, l ); nodeColors[ i ] = `rgba(${c[0]},${c[1]},${c[2]},${pal.pointOpacity})`; } } paintNodes(); function applyTheme( theme ) { pal = THEME_PALETTES[ theme ] || THEME_PALETTES.light; paintNodes(); if ( reduced ) drawFrame(); // re-renderiza el fotograma estático } document.addEventListener( 'lbt-theme-change', ( e ) => applyTheme( e.detail.theme ) ); // --- Parallax de puntero ----------------------------------------------------- let px = 0, py = 0, tx = 0, ty = 0; window.addEventListener( 'pointermove', ( e ) => { tx = ( e.clientX / window.innerWidth - 0.5 ) * 2; ty = ( e.clientY / window.innerHeight - 0.5 ) * 2; }, { passive: true } ); // --- Tamaño -------------------------------------------------------------------- function resize() { W = canvas.clientWidth || window.innerWidth; H = canvas.clientHeight || window.innerHeight; DPR = Math.min( window.devicePixelRatio || 1, 2 ); canvas.width = Math.round( W * DPR ); canvas.height = Math.round( H * DPR ); ctx.setTransform( DPR, 0, 0, DPR, 0, 0 ); // dibujamos en px CSS if ( reduced ) drawFrame(); } window.addEventListener( 'resize', resize ); // --- Proyección y dibujo --------------------------------------------------------- let rotY = Math.random() * Math.PI * 2; // giro lento continuo de la banda function project( x, y, z, out ) { // Espacio cámara: la cámara está en (0,0,CAM_Z) mirando al origen. const vz = z - CAM_Z; // negativo delante de la cámara if ( vz >= -0.5 ) { out[ 3 ] = 0; return false; } const inv = FOCAL / -vz; // ndc.x*W/2: el aspect se cancela — (W/2)/aspect == H/2. out[ 0 ] = W / 2 + x * inv * ( H / 2 ); out[ 1 ] = H / 2 - y * inv * ( H / 2 ); out[ 2 ] = vz; out[ 3 ] = 1; return true; } const pvA = [ 0, 0, 0, 0 ], pvB = [ 0, 0, 0, 0 ]; function drawFrame() { ctx.clearRect( 0, 0, W, H ); if ( currentTheme() === 'dark' ) ctx.globalCompositeOperation = 'lighter'; // aditivo else ctx.globalCompositeOperation = 'source-over'; // Líneas primero, puntos encima (como el depthWrite:false de la versión WebGL). ctx.lineWidth = 1; ctx.strokeStyle = pal.line; ctx.globalAlpha = pal.lineOpacity; ctx.beginPath(); for ( let k = 0; k < pairs.length; k++ ) { const a = pairs[ k ][ 0 ] * 3, b = pairs[ k ][ 1 ] * 3; if ( ! project( positions[ a ], positions[ a + 1 ], positions[ a + 2 ], pvA ) ) continue; if ( ! project( positions[ b ], positions[ b + 1 ], positions[ b + 2 ], pvB ) ) continue; ctx.moveTo( pvA[ 0 ], pvA[ 1 ] ); ctx.lineTo( pvB[ 0 ], pvB[ 1 ] ); } ctx.stroke(); // Puntos con atenuación de tamaño (mismo factor que PointsMaterial). ctx.globalAlpha = 1; for ( let i = 0; i < N; i++ ) { const ix = i * 3; if ( ! project( positions[ ix ], positions[ ix + 1 ], positions[ ix + 2 ], pvA ) ) continue; const r = ( 0.55 * ( H / 2 ) / -pvA[ 2 ] ) / 2; // radio en px CSS ctx.fillStyle = nodeColors[ i ]; ctx.beginPath(); ctx.arc( pvA[ 0 ], pvA[ 1 ], r, 0, Math.PI * 2 ); ctx.fill(); } ctx.globalCompositeOperation = 'source-over'; } // --- Bucle (pausa fuera de pantalla o pestaña oculta) ------------------------------ let running = true; let visible = true; const io = new IntersectionObserver( ( entries ) => { visible = entries[ 0 ].isIntersecting; }, { threshold: 0.02 } ); io.observe( canvas ); let t = Math.random() * 100; let last = performance.now(); function frame( now ) { requestAnimationFrame( frame ); const dt = Math.min( ( now - last ) / 1000, 0.05 ); last = now; if ( ! visible || document.hidden ) return; t += dt; for ( let i = 0; i < N; i++ ) { const ix = i * 3; positions[ ix ] = basePos[ ix ] + Math.sin( t * 0.5 + i * 1.7 ) * drift[ ix ]; positions[ ix + 1 ] = basePos[ ix + 1 ] + Math.cos( t * 0.42 + i * 2.3 ) * drift[ ix + 1 ]; positions[ ix + 2 ] = basePos[ ix + 2 ] + Math.sin( t * 0.36 + i * 0.9 ) * drift[ ix + 2 ]; } rotY += dt * 0.045; // giro lento de la banda (igual que group.rotation.y) px += ( tx - px ) * 0.03; py += ( ty - py ) * 0.03; // Rotación Y y luego X (mismo orden que el Euler 'XYZ' de three), + parallax. const cy = Math.cos( rotY ), sy = Math.sin( rotY ); const cxr = Math.cos( py * 0.06 ), sxr = Math.sin( py * 0.06 ); for ( let i = 0; i < N; i++ ) { const ix = i * 3; let x = positions[ ix ] + px * 1.6, y = positions[ ix + 1 ], z = positions[ ix + 2 ]; const rx = cy * x - sy * z, rz = sy * x + cy * z; // rotY positions[ ix ] = rx; positions[ ix + 1 ] = cxr * y - sxr * rz; // rotX positions[ ix + 2 ] = sxr * y + cxr * rz; } drawFrame(); } resize(); if ( reduced ) { drawFrame(); // un solo fotograma estático } else { requestAnimationFrame( frame ); } }