A Pen by Sven Neumann on CodePen.
Last active
August 29, 2015 09:47
-
-
Save killroy42/7d9931e40991da192372 to your computer and use it in GitHub Desktop.
Basic WebRtc Video
This file contains hidden or 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
| <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> |
This file contains hidden or 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
| 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