Skip to content

Instantly share code, notes, and snippets.

@rlemon
Created July 4, 2016 18:53
Show Gist options
  • Save rlemon/4c1d8988a320cc664930f7d9585dc339 to your computer and use it in GitHub Desktop.
Save rlemon/4c1d8988a320cc664930f7d9585dc339 to your computer and use it in GitHub Desktop.
html webcam capture example
<video id="video"></video>
// mediaDevices.getUserMedia is not implemented everywhere. this is a shit filler
const fillGetUserMedia = _ => {
return function getUserMedia(constraints) {
const gum = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
if( !gum ) return Promise.reject('your browser is a pos');
return new Promise( (resolve, reject) => {
gum.call(navigator, constraints, resolve, reject);
});
}
};
if( !( 'mediaDevices' in navigator ) ) navigator.mediaDevices = {};
if( !( 'getUserMedia' in navigator.mediaDevices ) ) navigator.mediaDevices.getUserMedia = fillGetUserMedia();
const constraints = {
audio: true,
video: true,
height: 1280,
width: 720
};
navigator.mediaDevices.getUserMedia(constraints).then(stream => {
video.src = URL.createObjectURL(stream);
video.onloadedmetadata = _ => {
video.play();
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment