Skip to content

Instantly share code, notes, and snippets.

@killroy42
Last active August 29, 2015 09:47
Show Gist options
  • Save killroy42/7d9931e40991da192372 to your computer and use it in GitHub Desktop.
Save killroy42/7d9931e40991da192372 to your computer and use it in GitHub Desktop.
Basic WebRtc Video
<script src="script.js"></script>
<div class="camera">
<video id="video">Video stream not available.</video>
<button id="startbutton">Take photo</button>
</div>
<canvas id="canvas"></canvas>
<div class="output">
<img id="photo" alt="The screen capture will appear in this box.">
</div>
document.addEventListener("DOMContentLoaded", function(event) {
navigator.getMedia = (
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
console.log(navigator.getMedia);
navigator.getMedia({video: true, audio: false}, function(stream) {
console.log(stream);
video.src = window.URL.createObjectURL(stream);
video.play();
}, function(err) {
console.log("An error occured! " + err);
});
document.getElementById('startbutton').addEventListener('click', function(e) {
var video = document.getElementById('video');
var w = video.videoWidth;
var h = video.videoHeight;
var canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
var ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0, w, h);
var dataUrl = canvas.toDataURL('image/png')
document.getElementById('photo').setAttribute('src', dataUrl);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment