Languages
[Edit]
EN

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

6 points
Created by:
cory
1756

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

Example preview:

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

Practical example

Edit

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

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

 

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.

Reusable logic

Edit

In this section, you can find reusable class that plays indicated stream on canvas element.

 

See also

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

  2. JavaScript - list available audio and video input devices

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

References

Edit
  1. MediaDevices.getUserMedia() - MDN Docs
  2. HTMLMediaElement.srcObject - MDN Docs

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

Alternative titles

  1. JavaScript - stream web camera video to canvas 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