
let intervalTime = 0;

function loadScriptapp(urlScr) {
  return new Promise((resolve, reject) => {
    const script = document.createElement('script');
    script.src = urlScr;
    script.async = true;
    script.onload = () => resolve();
    script.onerror = () => reject(new Error(`Failed to load script: ${urlScr}`));
    document.head.appendChild(script);
  });
}

function returnToOpen() {
  let returToOpen = localStorage.getItem("returToOpen");
  if (returToOpen === "ias") window.open('../ias/open.html', '_self')
  else window.open('../rhb/open.html', '_self')
}

/*
////////////////////////////////////////////////
if (key == 'g' || key == 'G') {
    recording = !recording;
    if (recording) {
      let time = 1, x = setInterval(function () {
        time--;
        if (time === 0) {
          clearInterval(x);
          preloadEncoder();
        }
      }, 4000);
    }
    
if (key == 'r' || key == 'R') {
    if (millis() - lapse > 100) {
      saveCanvas("img" + year() + month() + day() + hour() + minute(), "jpg");
      lapse = millis();
    }
  }
//////////////////////////////////////////////
// Crear un H264 MP4 Encoder
async function preloadEncoder() {
  let wV = windowWidth;
  if (!numPar(wV)) wV = wV + 1
  let hV = windowHeight;
  if (!numPar(hV)) hV = hV + 1

  await HME.createH264MP4Encoder().then(enc => {
    encoder = enc,
      encoder.outputFilename = (random() + "").replace("0.", "mirror"),
      encoder.width = wV,
      encoder.height = hV,
      encoder.frameRate = 10,
      encoder.kbps = 50000,
      encoder.groupOfPictures = 10,
      encoder.initialize();
  })
}

async function grabarFotograma() {
  // keep adding new frame
  if (recording) {
    console.log('recording')

    const tempCanvas = document.createElement('canvas');
    const tempCtx = tempCanvas.getContext('2d', { willReadFrequently: true });
    tempCanvas.width = encoder.width;
    tempCanvas.height = encoder.height;
    // Dibuja la imagen original en el canvas temporal escalado
    tempCtx.drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, encoder.width, encoder.height);
    // Obtén la imagen escalada del canvas temporal
    const scaledImageData = tempCtx.getImageData(0, 0, encoder.width, encoder.height);
    // Añade el frame escalado al encoder
    encoder.addFrameRgba(scaledImageData.data);

    //encoder.addFrameRgba(drawingContext.getImageData(0, 0, encoder.width, encoder.height).data);
    await new Promise(resolve => window.requestAnimationFrame(resolve));
    recordedFrames++
  }
  // finalize encoding and export as mp4
  if (recordedFrames === numFrames) {
    recordedFrames = 0
    recording = false
    console.log('recording stopped')
    encoder.finalize()
    const uint8Array = encoder.FS.readFile(encoder.outputFilename);
    const anchor = document.createElement('a')
    anchor.href = URL.createObjectURL(new Blob([uint8Array], { type: 'video/mp4' }))
    anchor.download = encoder.outputFilename
    anchor.click()
    encoder.delete()
  }
}
//////////////////////////////////////
*/