
How to Integrate Real-time Webcam on a Web Page Using Javascript
12 October, 2021
16
16
2
Contributors
What is a WebCam
What is Javascript
To begin, we should first set up our project.
Note: If you don't have Visual Studio Code installed, you won't be able to use code .
Step 1 - Create a directory
npm
by typing the following command:Step 2 - Create files
script.js
and index.html
files.Step 3 - Setting up Index.html
Step 4 - Referencing Script.js, Jquery, Bootstrap JS, and CSS CDN
Step 5 - Integrating Webcam on the Web Page With Javascript
navigator.mediaDevices.getUserMedia
. We will specify video: true to indicate that we only want to use the webcam and not the audio.getUserMedia()
delivers a promise that resolves when the user grants permission to view the webcam and rejects it when the user denies it. As a result, we used .then
and .catch
to deal with the promise.startCam
as shown below:startCam
function. To do that, let us refer to the startCam
function on page load using jquery as shown below:Step 6 - Adding OnClick Functionality
index.js
to update our button with the text Click Here to Start Cam
with onClick attribute using the snippet below:ON
our webcam on your browser. We can see how Powerful Javascript is to turn to integrate webcam in our web page.Step 7 - Implementing Stop Cam
OFF
? Of course, we should do that with ease, just like we did for turning the webcam ON.
Let's stop the video tracks and turn off the webcam when the button stop is clicked.Step 8 - Adding OnClick Functionality
index.js
to update our button with the text Click Here to Stop Cam
with onClick attribute using the snippet below:
Conclusion
Resources
javascript
web
web development