77 lines
2.5 KiB
JavaScript
77 lines
2.5 KiB
JavaScript
/* public/home/js/noise.js – Section 2.3 */
|
||
document.addEventListener('DOMContentLoaded', function() {
|
||
if (!document.querySelector('.home-page')) return;
|
||
|
||
window.addEventListener('load', function() {
|
||
// 2.3 Noise animation
|
||
const noise = () => {
|
||
const canvas = document.getElementById('noise');
|
||
const canvas2 = document.getElementById('noise_menu');
|
||
|
||
// Solo abortar si NO existe ninguno de los dos
|
||
if (!canvas && !canvas2) {
|
||
console.warn('⛔ Ningún canvas #noise ni #noise_menu encontrado — saltando ruido.');
|
||
return;
|
||
}
|
||
|
||
// Obtener contextos (si el elemento existe)
|
||
const ctx = canvas ? canvas.getContext('2d') : null;
|
||
const ctx2 = canvas2 ? canvas2.getContext('2d') : null;
|
||
let wWidth, wHeight;
|
||
const noiseData = [];
|
||
let frame = 0, loopTimeout;
|
||
|
||
const createNoise = () => {
|
||
const idata = ctx.createImageData(wWidth, wHeight);
|
||
const buffer32 = new Uint32Array(idata.data.buffer);
|
||
for (let i = 0, len = buffer32.length; i < len; i++) {
|
||
if (Math.random() < 0.5) buffer32[i] = 0xff000000;
|
||
}
|
||
noiseData.push(idata);
|
||
};
|
||
|
||
const paintNoise = () => {
|
||
frame = (frame + 1) % noiseData.length;
|
||
if (ctx) ctx.putImageData(noiseData[frame], 0, 0);
|
||
if (ctx2) ctx2.putImageData(noiseData[frame], 0, 0);
|
||
};
|
||
|
||
const loop = () => {
|
||
paintNoise();
|
||
loopTimeout = window.setTimeout(() => {
|
||
window.requestAnimationFrame(loop);
|
||
}, 1000 / 25);
|
||
};
|
||
|
||
const setup = () => {
|
||
wWidth = window.innerWidth;
|
||
wHeight = window.innerHeight;
|
||
if (canvas) { canvas.width = wWidth; canvas.height = wHeight; }
|
||
if (canvas2) { canvas2.width = wWidth; canvas2.height = wHeight; }
|
||
noiseData.length = 0; // limpiar array
|
||
for (let i = 0; i < 10; i++) createNoise();
|
||
loop();
|
||
};
|
||
|
||
let resizeThrottle;
|
||
const reset = () => {
|
||
window.addEventListener('resize', () => {
|
||
clearTimeout(resizeThrottle);
|
||
resizeThrottle = setTimeout(() => {
|
||
clearTimeout(loopTimeout);
|
||
setup();
|
||
}, 200);
|
||
}, false);
|
||
};
|
||
|
||
setup();
|
||
reset();
|
||
};
|
||
|
||
// Solo si no definiste noiseMode = 0 deshabilitado
|
||
if (typeof noiseMode === 'undefined' || noiseMode !== 0) {
|
||
noise();
|
||
}
|
||
});
|
||
});
|
||
|