[英]Uncaught TypeError: Cannot read property 'getContext' of null
[英]Uncaught TypeError: Cannot read property 'getContext' of null Node.js
我正在嘗試為一個項目創建流式視頻,正在包括HTML的node.js中創建,但無法通過此錯誤完成Uncaught TypeError: Can not read property 'getContext' of null
然后我需要解決該問題,但是我不能,因為我尋求解決方案和這種類型的錯誤,但是沒有一個對我有效,只是我必須在以下幾行中進行評論的問題:
var context = canvas.getContext("2d");
canvas.width = 800;
canvas.height = 600;
context.width = canvas.width;
context.height = canvas.height
這是我的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>
謝謝,我將幫助您。
您需要將id "preview"
更改為id="preview"
,然后可以繼續進行下一個錯誤:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.