Skip to content

Instantly share code, notes, and snippets.

@jawdatls
Created May 11, 2015 08:30
Show Gist options
  • Save jawdatls/681a6c615551e0e67810 to your computer and use it in GitHub Desktop.
Save jawdatls/681a6c615551e0e67810 to your computer and use it in GitHub Desktop.
Html5 Camera
<video id="video" width="640" height="480" autoplay></video>
<button id="snap" class="sexyButton">Snap Photo</button>
<canvas id="canvas" width="640" height="480"></canvas>
<script>
/* 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) { /* WebKit-prefixed */
navigator.mozGetUserMedia(videoObj, function(stream){
video.src = window.URL.createObjectURL(stream);
video.play();
}, errBack);
}
/* Trigger photo take */
document.getElementById("snap").addEventListener("click", function() {
context.drawImage(video, 0, 0, 640, 480);
});
}, false);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment