Skip to content

Instantly share code, notes, and snippets.

@michaeljymsgutierrez
Last active August 4, 2017 09:09
Show Gist options
  • Select an option

  • Save michaeljymsgutierrez/439e066e31425993725c069db4272337 to your computer and use it in GitHub Desktop.

Select an option

Save michaeljymsgutierrez/439e066e31425993725c069db4272337 to your computer and use it in GitHub Desktop.
<video id="video" width="640" height="480" autoplay></video>
<button id="snap">Snap Photo</button>
<canvas id="canvas" width="640" height="480"></canvas>
<script>
/* { audio: true } */
/* Get camera access */
if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) {
video.src = window.URL.createObjectURL(stream);
video.play();
});
}
else if(navigator.getUserMedia) { // Standard
navigator.getUserMedia({ video: true }, function(stream) {
video.src = stream;
video.play();
}, errBack);
} else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
navigator.webkitGetUserMedia({ video: true }, function(stream){
video.src = window.webkitURL.createObjectURL(stream);
video.play();
}, errBack);
} else if(navigator.mozGetUserMedia) { // Mozilla-prefixed
navigator.mozGetUserMedia({ video: true }, function(stream){
video.src = window.URL.createObjectURL(stream);
video.play();
}, errBack);
}
</script>
<script>
/*Take Photo */
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var video = document.getElementById('video');
document.getElementById("snap").addEventListener("click", function() {
context.drawImage(video, 0, 0, 640, 480);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment