Skip to content

Instantly share code, notes, and snippets.

@bellbind
Last active August 29, 2015 14:16
Show Gist options
  • Save bellbind/19ed12fcfbb8d4424316 to your computer and use it in GitHub Desktop.
Save bellbind/19ed12fcfbb8d4424316 to your computer and use it in GitHub Desktop.
[html5][mediastream]getUserMedia
<!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>
;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);
@bellbind
Copy link
Author

@bellbind
Copy link
Author

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