Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

estoy usando javascript y quier definir variables con una función, pero no me resultan…

  1. User

    estoy usando javascript y quier definir variables con una función, pero no me resultan definidas cuando quiero usar estas variables en otra función function inittamanios(){ let width = 0; let height = 0;

    let qvga = { width: { exact: 320 }, height: { exact: 240 } };

    let vga = { width: { exact: 640 }, height: { exact: 480 } };

    let resolution = window.innerWidth < 640 ? qvga : vga; let stream = null; let vc = null; let streaming = false; var stats = null; }

    //Función para inicar la cámara web function startCamera() { if (streaming) return; navigator.mediaDevices.getUserMedia({ video: resolution, audio: false }) .then(function (s) { stream = s; video.srcObject = s; video.play(); }) .catch(function (err) { console.log("An error occured! " + err); });

    video.addEventListener("canplay", function (ev) { if (!streaming) { height = video.videoHeight; width = video.videoWidth; video.setAttribute("width", width); video.setAttribute("height", height); streaming = true; vc = new cv.VideoCapture(video); } //La funcion "startVideoProcessing" define las variables de imagenes startVideoProcessing(); //La funcion "processVideo" hace el procesamiento de video [aplica filtros, entre otros] processVideo() }, false); } //Iniciamos la función "startCamera" para poder ver el streaming de la cámara web inittamanios() startCamera()

  2. Assistant

    El problema está en que las variables width y height están siendo declaradas dentro de la función inittamanios() como variables locales. Por lo tanto, no están disponibles en la función startCamera() porque se encuentran fuera de su alcance.

    Para solucionar esto, puedes declarar las variables width y height como variables globales fuera de las funciones. De esta forma, podrás acceder a ellas desde cualquier lugar en tu código. Aquí tienes un ejemplo de cómo hacerlo:

    let width = 0;
    let height = 0;
    let resolution = null;
    let stream = null;
    let vc = null;
    let streaming = false;
    var stats = null;
    
    function inittamanios(){
      let qvga = { width: { exact: 320 }, height: { exact: 240 } };
      
      let vga = { width: { exact: 640 }, height: { exact: 480 } };
      
      resolution = window.innerWidth < 640 ? qvga : vga;
    }
    
    //Función para inicar la cámara web
    function startCamera() {
      if (streaming) return;
      navigator.mediaDevices.getUserMedia({ video: resolution, audio: false })
        .then(function (s) {
          stream = s;
          video.srcObject = s;
          video.play();
        })
        .catch(function (err) {
          console.log("An error occured! " + err);
        });
    
      video.addEventListener("canplay", function (ev) {
        if (!streaming) {
          height = video.videoHeight;
          width = video.videoWidth;
          video.setAttribute("width", width);
          video.setAttribute("height", height);
          streaming = true;
          vc = new cv.VideoCapture(video);
        }
        //La funcion "startVideoProcessing" define las variables de imagenes
        startVideoProcessing();
        //La funcion "processVideo" hace el procesamiento de video [aplica filtros, entre otros]
        processVideo()
      }, false);
    }
    
    //Iniciamos la función "startCamera" para poder ver el streaming de la cámara web
    inittamanios();
    startCamera();
    

    De esta manera, las variables width y height deberían ser accesibles en la función startCamera().

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.