


HTML5 Canvas JS controls the camera on your computer or mobile phone instance_html5 tutorial skills
Client APIs on mobile devices and desktop computers are not initially synchronized. Initially, certain functions and corresponding APIs will always be available on mobile devices, but slowly, these APIs will appear on desktop computers. One such application interface technology is the getUserMedia API, which allows application developers to access the user's camera or built-in camera. Let me show you how to access your camera through a browser and extract screenshots.
HTML code
I wrote some comments in the code below, please read:
Before writing the above tags, you should determine whether the user's client has camera support, but in order to avoid trouble, these HTML tags are written directly here. It should be noted that the length and width we use here are 640×480.
JavaScript code
Because we wrote the HTML manually, the following js code is much simpler than you think.
// Put event listeners into place
window.addEventListener("DOMContentLoaded", function() {
// Grab elements, create settings, etc.
var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d"),
video = document.getElementById("video"),
videoObj = { "video": true },
errBack = function(error) {
console.log("Video capture error: ", error.code);
};
// Put video listeners into place
if(navigator.getUserMedia) { // Standard
navigator.getUserMedia(videoObj, function(stream) {
video.src = stream;
video.play();
}, errBack);
} else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
navigator.webkitGetUserMedia(videoObj, function(stream){
video.src = window.webkitURL.createObjectURL(stream);
video.play();
}, errBack);
}
else if(navigator.mozGetUserMedia) { // Firefox-prefixed
navigator.mozGetUserMedia(videoObj, function(stream){
video.src = window.URL.createObjectURL(stream);
video.play();
}, errBack);
}
}, false);
Once it is determined that the user's browser supports getUserMedia, the following is very simple. You only need to set the src of the video element to the user's camera live video connection. That’s all you need to do to access the camera using your browser!
The function of taking pictures can only be said to be a little complicated. We add a listener to the button and draw the video screen to the canvas.
//Trigger photo action
document.getElementById("snap")
.addEventListener("click", function() {
context.drawImage(video, 0, 0, 640, 480);
});
Of course, you can also add some filter effects to the picture…
In the past, we needed to use a third-party plug-in to access the user's camera from the browser, which was somewhat complicated. Now we only need HTML5 canvas technology and javaScript, we can operate the user's camera simply and quickly. Not only is it access to the camera, but also because of the powerful canvas technology of HTML5, we can add various charming filter effects to the pictures. Now, take a picture of yourself with your own camera in your browser!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
