Skip to content

HTML canvas not producing image from web cam

I want to capture image every time socket.on(‘takePic’) gets triggered. It works fine for the first time. but when the socket.on(‘takePic’) gets triggered second time,the canvas div is just blank and so the img tag. I have copied the takePicture() function from somewhere and added the setInterval() and localstream variable to stop camera. how can I fix this?

here is my js code.

const socket = io('http://localhost:3001');

const params = new Proxy(new URLSearchParams(, {
    get: (searchParams, prop) => searchParams.get(prop),
  let roomId = params.roomId; 


var takePicture = function () {
    // The width and height of the captured photo. We will set the
    // width to the value defined here, but the height will be
    // calculated based on the aspect ratio of the input stream.
    var width = 320;    // We will scale the photo width to this
    var height = 0;     // This will be computed based on the input stream
    // |streaming| indicates whether or not we're currently streaming
    // video from the camera. Obviously, we start at false.
    var streaming = false;
    var localstream;
    // The various HTML elements we need to configure or control. These
    // will be set by the startup() function.
    var video = null;
    var canvas = null;
    var photo = null;
    var startbutton = null;
    function showViewLiveResultButton() {
      if (window.self !== {
        // Ensure that if our document is in a frame, we get the user
        // to first open it in its own tab or window. Otherwise, it
        // won't be able to request permission for camera access.
        const button = document.createElement("button");
        button.textContent = "View live result of the example code above";
        button.addEventListener('click', () =>;
        return true;
      return false;
    function startup() {
      if (showViewLiveResultButton()) { return; }
      video = document.getElementById('video');
      canvas = document.getElementById('canvas');
      photo = document.getElementById('photo');
      startbutton = document.getElementById('startbutton');
    navigator.mediaDevices.getUserMedia({video: true, audio: false})
      .then(function(stream) {
        video.srcObject = stream;
        localstream = stream;;
      .catch(function(err) {
        console.log("An error occurred: " + err);
      video.addEventListener('canplay', function(ev){
        if (!streaming) {
          height = video.videoHeight / (video.videoWidth/width);
          // Firefox currently has a bug where the height can't be read from
          // the video, so we will make assumptions if this happens.
          if (isNaN(height)) {
            height = width / (4/3);
          video.setAttribute('width', width);
          video.setAttribute('height', height);
          canvas.setAttribute('width', width);
          canvas.setAttribute('height', height);
          streaming = true;
      }, false);
      startbutton.addEventListener('click', function(ev){

        video.src = "";
      }, false);
    // Fill the photo with an indication that none has been
    // captured.
    function clearphoto() {
      var context = canvas.getContext('2d');
      context.fillStyle = "#AAA";
      context.fillRect(0, 0, canvas.width, canvas.height);
      var data = canvas.toDataURL('image/png');
      photo.setAttribute('src', data);
    // Capture a photo by fetching the current contents of the video
    // and drawing it into a canvas, then converting that to a PNG
    // format data URL. By drawing it on an offscreen canvas and then
    // drawing that to the screen, we can change its size and/or apply
    // other changes before drawing it.
    function takepicture() {
      var context = canvas.getContext('2d');
      if (width && height) {
        canvas.width = width;
        canvas.height = height;
        context.drawImage(video, 0, 0, width, height);
        var data = canvas.toDataURL('image/png');
        photo.setAttribute('src', data);
      } else {
    // Set up our event listener to run the startup process
    // once loading is complete.
    var i = 10;
   let picInterval = setInterval(()=>{
      i -= 1;
      $('#heading').html(`taking picture in ${i}`);
    }, 1000)



and this is the html code

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/studentVideo.css">
    <div class="contentarea">
        <h1 id="heading">
         This example demonstrates how to set up a media stream using your built-in webcam, fetch an image from that stream, and create a PNG using that image.
        <div class="camera">
          <video id="video" width="320" height="240">Video stream not available.</video>
          <button id="startbutton">Take photo</button> 
        <canvas id="canvas" width="320" height="240">
        <div class="output">
          Visit our article <a href=""> Taking still photos with WebRTC</a> to learn more about the technologies used here.

<script src=""></script>
<script src=""></script>
<script src="js/video.js"></script>


In the click handler of #startbutton your code calls takepicture and then it goes on to remove the video’s src and stop the MediaStream.
So the next time this handler is called, there is no source affected to the video element and thus nothing to be drawn on the canvas anymore.
It’s quire unclear why you clear the video in this click handler, so you might want to remove this part of the code, or to move it to a “stop” button instead, but anyway, you would probably be better calling takepicture from your interval directly rather than relying on the event handler.