Languages
[Edit]
EN

JavaScript - play web camera on video element (webcam usage example)

8 points
Created by:
Evania
584

In this short article, you can see how to stream video from web camera to video element using JavaScript.

Hint: used solutions were introduced in the major web browsers around 2016.

Quick solution:

 

Note: it is important to be sure the web camera is connected properly, permissions for camera are granted and it is not used by any other application.

 

Example preview:

Example camera streaming to video element using JavaScript.
Example camera streaming to video element using JavaScript.

Simple example

Edit

In this example, we present how to create reusable logic which can be used to play camera on video HTML element.

Web camera can be run using playCamera() function that accepts the following agruments:

  • preferedwidth - web camera video stream width in px (it is just suggestion),
  • preferedheight - web camera video stream height in px (it is just suggestion).

Warning: the logic doesn't provide API to destroy video stream - check next one example.

 

Complex example

Edit

In this example, we present how to create reusable logic which can be used to play web camera on video HTML element.

In the below example, web camera can be:

  • run using playCamera() function that accepts the following agruments:
    • preferedwidth - web camera video stream width in px (it is just suggestion),
    • preferedheight - web camera video stream height in px (it is just suggestion),
    • onReady - function to be executed once the web camera is ready,
    • onError - function to be executed when some error occurs.
  • stopped and destroyed using stopCamera() function, e.g.:

Source code: 

 

Giving premissions

Edit

When a web camera is accessed from JavaScript you should see notification to permit using it. Just let use it by desired website and remove permissions when it is not needed.

Giving permissions to use web camera under Google Chrome web browser.
Giving permissions to use web camera under Google Chrome web browser.

See also

Edit
  1. JavaScript - play web camera video on canvas element

  2. JavaScript - list available audio and video input devices

  3. JavaScript - capture video from device camera using input HTML element (mobile devices only)

  4. React - play web camera on video element

References

Edit
  1. MediaDevices - MDN Docs
  2. MediaStream - MDN Docs

  3. MediaDevices.getUserMedia() - MDN Docs

  4. HTMLMediaElement.srcObject - MDN Docs

  5. <video>: The Video Embed element - MDN Docs

Alternative titles

  1. JavaScript - stream web camera to video element
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join