0

i am exploring the API of the get user media and tried running the api in my localhost with sample code attached below

It is working fine in jsbin here

but completely fails in localhost with below errors

Uncaught TypeError: Cannot read property 'addEventListener' of null capture.html:43 Uncaught TypeError: Cannot set property 'src' of null

Code :

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>

<script>
(function() {

var streaming = false,
  video        = document.querySelector('#video'),
  cover        = document.querySelector('#cover'),
  canvas       = document.querySelector('#canvas'),
  photo        = document.querySelector('#photo'),
  startbutton  = document.querySelector('#startbutton'),
  width = 200,
  height = 0;

  navigator.getMedia = ( navigator.getUserMedia || 
                     navigator.webkitGetUserMedia ||
                     navigator.mozGetUserMedia ||
                     navigator.msGetUserMedia);

  navigator.getMedia(
  { 
  video: true, 
  audio: false 
  },
  function(stream) {
  if (navigator.mozGetUserMedia) { 
    video.mozSrcObject = stream;
  } else {
    var vendorURL = window.URL || window.webkitURL;
    video.src = vendorURL ? vendorURL.createObjectURL(stream) : stream;
  }
  video.play();
  },
  function(err) {
  console.log("An error occured! " + err);
  }
  );

  video.addEventListener('canplay', function(ev){
  if (!streaming) {
  height = video.videoHeight / (video.videoWidth/width);
  video.setAttribute('width', width);
  video.setAttribute('height', height);
  canvas.setAttribute('width', width);
  canvas.setAttribute('height', height);
  streaming = true;
  }
  }, false);

function takepicture() {
 canvas.width = width;
canvas.height = height;
canvas.getContext('2d').drawImage(video, 0, 0, width, height);
var data = canvas.toDataURL('image/png');
photo.setAttribute('src', data);
}

startbutton.addEventListener('click', function(ev){
takepicture();
ev.preventDefault();
}, false);

})();


</script>
<style>
html {
background: #111111;
height: 100%;
background: linear-gradient( #333, #000);
}
canvas {
display: none;
}
video, img, #startbutton {
display: block;
float: left;
border: 10px solid #fff;
border-radius: 10px;
}
#startbutton {
background: green;
border: none;
color: #fff;
margin: 100px 20px 20px 20px;
padding: 10px 20px;
font-size: 20px;
}
#container {
overflow: hidden;
width: 880px;
margin: 20px auto;
}
</style>
</head>
<body>
<video id="video"></video>
<button id="startbutton">Take photo</button>
<canvas id="canvas"></canvas>
<img src="http://placekitten.com/g/200/150" id="photo" alt="photo">
</body>
</html>
user3592479
  • 695
  • 3
  • 13
  • 26

1 Answers1

1

It is because your script is trying to access html elements that have not been created yet. HTML is read from the top down. Move your script into the body of your page, or tell it not to execute until the whole page loads.

It worked fine for me when I moved your script to the body instead of the head of the page.

Benjamin Trent
  • 7,378
  • 3
  • 31
  • 41