Last active
October 6, 2017 21:41
-
-
Save itsgreggreg/3062fba7802b66114f92fc782a5c32cf to your computer and use it in GitHub Desktop.
Quick and dirty webcam in browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<video id="video" width="640" height="480" autoplay></video> | |
<button id="snap">Snap Photo</button> | |
<canvas id="canvas" width="640" height="480"></canvas> | |
<script> | |
// Grab elements, create settings, etc. | |
var video = document.getElementById('video'); | |
// Get access to the camera! | |
if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { | |
// Not adding `{ audio: true }` since we only want video now | |
navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) { | |
video.src = window.URL.createObjectURL(stream); | |
video.play(); | |
}); | |
} | |
// Elements for taking the snapshot | |
var canvas = document.getElementById('canvas'); | |
var context = canvas.getContext('2d'); | |
var video = document.getElementById('video'); | |
// Trigger photo take | |
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