Last active
August 29, 2015 14:16
-
-
Save bellbind/19ed12fcfbb8d4424316 to your computer and use it in GitHub Desktop.
[html5][mediastream]getUserMedia
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
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>User Media</title> | |
<script src="script.js"></script> | |
</head> | |
<body> | |
<div id="camera"></div> | |
<div id="shot"></div> | |
</body> | |
</html> |
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
;window.addEventListener("load", function (ev) { | |
// API: http://w3c.github.io/mediacapture-main/getusermedia.html | |
// Polyfill Promise based API for Chrome | |
var getUserMedia = navigator.mediaDevices ? | |
navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices) : | |
function (constraints) { | |
return new Promise(function (f, r) { | |
navigator.webkitGetUserMedia(constraints, f, r); | |
}); | |
}; | |
getUserMedia({video: true, audio: false}).then(function (stream) { | |
var video = document.createElement("video"); | |
document.getElementById("camera").appendChild(video); | |
// define video screen size beacuse | |
// browsers not yet support MediaStreamTrack.getConstraints() API | |
//window.track = stream.getVideoTracks()[0] | |
video.style.width = "50%"; | |
video.src = URL.createObjectURL(stream); | |
video.play(); | |
video.addEventListener("click", function () { | |
console.log("shot"); | |
var canvas = document.createElement("canvas"); | |
//console.log(video.width, video.height); | |
canvas.width = video.clientWidth; | |
canvas.height = video.clientHeight; | |
var c2d = canvas.getContext("2d"); | |
c2d.drawImage(video, 0, 0, canvas.width, canvas.height); | |
//document.getElementById("shot").appendChild(canvas); | |
var img = document.createElement("img"); | |
var uri = canvas.toDataURL("image/png"); | |
//console.log(uri); | |
img.src = uri; | |
document.getElementById("shot").appendChild(img); | |
}, false); | |
}); | |
}, false); |
Not worked on Android (5.0.1) chrome.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
demo: https://rawgit.com/bellbind/19ed12fcfbb8d4424316/raw/index.html