简体   繁体   中英

Uncaught TypeError: Cannot read property 'getContext' of null Node.js

I'm trying to create a streaming video for a project, I am creating in node.js including html, and I can not finish by this error Uncaught TypeError: Can not read property 'getContext' of null

then I need to fix that problem, but I can not because I sought solutions and this type of error, but none works for me up to work only issue I had to comment on these lines:

  var context = canvas.getContext("2d");

        canvas.width = 800;
        canvas.height = 600;

        context.width = canvas.width;
        context.height = canvas.height

Here the codes of my proyect: Emitir Video

    <canvas style = "display:none;" id "preview" width='1280' height='720'></canvas>

    <div id = "logger"></div>

    <script type="text/javascript">
        var canvas = document.getElementById("preview");
        var context = canvas.getContext("2d"); // This is the line that generates the error

        canvas.width = 800;
        canvas.height = 600;

        context.width = canvas.width;
        context.height = canvas.height;

        var video = document.getElementById("video");
        var socket = io();

        function logger(msg)
        {
            $("#logger").text(msg);
        }

        function loadCam(stream)
        {
            video.src = window.URL.createObjectURL(stream);
            logger('Camara cargada correctamente [OK]');
        }

        function loadFail()
        {
            logger('Camara no conectada, Favor Conectela');
        }

        function  viewVideo(video,context)
        {
            context.drawImage(video,0,0,context.width,context.height);
            socket.emit('stream',canvas.toDataURL('image/webp'));
        }

        $(function(){
            navigator.getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msgGetUserMedia);

            if (navigator.getUserMedia)
            {
                navigator.getUserMedia({video: true}, loadCam, loadFail);
            }
            //1000 = 1 Seg.

            setInterval(function(){
                viewVideo(video,context);
            },70);

        });
    </script>

</body>
</html>

Thanks i will apreciate helps.

您需要将id "preview"更改为id="preview" ,然后可以继续进行下一个错误:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM