Skip to content

Instantly share code, notes, and snippets.

@KFMichael
Created May 6, 2015 07:23
Show Gist options
  • Save KFMichael/1d87cb34ec6e6c479f35 to your computer and use it in GitHub Desktop.
Save KFMichael/1d87cb34ec6e6c479f35 to your computer and use it in GitHub Desktop.
MediaStream API (aka getUserMedia)
<doctype html>
<html>
<head>
<style>
#snapshotCanvas {
width: 307px;
height: 250px;
background: rgba(255,255,255,0.5);
border: 1px solid #ccc;
}
#output {
width: 307px;
height: 250px;
background: rgba(255,255,255,0.5);
border: 0px solid #ccc;
}
#screenshot-stream {
width: initial;
height: initial;
}
#screenshot {
vertical-align: top;
}
.blur {
-webkit-filter: blur(3px);
-moz-filter: blur(3px);
-o-filter: blur(3px);
-ms-filter: blur(3px);
filter: blur(3px);
}
.brightness {
-webkit-filter: brightness(5);
-moz-filter: brightness(5);
-o-filter: brightness(5);
-ms-filter: brightness(5);
filter: brightness(5);
}
.contrast {
-webkit-filter: contrast(8);
-moz-filter: contrast(8);
-o-filter: contrast(8);
-ms-filter: contrast(8);
filter: contrast(8);
}
.hue-rotate {
-webkit-filter: hue-rotate(90deg);
-moz-filter: hue-rotate(90deg);
-o-filter: hue-rotate(90deg);
-ms-filter: hue-rotate(90deg);
filter: hue-rotate(90deg);
}
.hue-rotate2 {
-webkit-filter: hue-rotate(180deg);
-moz-filter: hue-rotate(180deg);
-o-filter: hue-rotate(180deg);
-ms-filter: hue-rotate(180deg);
filter: hue-rotate(180deg);
}
.hue-rotate3 {
-webkit-filter: hue-rotate(270deg);
-moz-filter: hue-rotate(270deg);
-o-filter: hue-rotate(270deg);
-ms-filter: hue-rotate(270deg);
filter: hue-rotate(270deg);
}
.saturate {
-webkit-filter: saturate(10);
-moz-filter: saturate(10);
-o-filter: saturate(10);
-ms-filter: saturate(10);
filter: saturate(10);
}
.grayscale {
-webkit-filter: grayscale(1);
-moz-filter: grayscale(1);
-o-filter: grayscale(1);
-ms-filter: grayscale(1);
filter: grayscale(1);
}
.sepia {
-webkit-filter: sepia(1);
-moz-filter: sepia(1);
-o-filter: sepia(1);
-ms-filter: sepia(1);
filter: sepia(1);
}
.invert {
-webkit-filter: invert(1);
-moz-filter: invert(1)
-o-filter: invert(1)
-ms-filter: invert(1)
filter: invert(1)
}
</style>
</head>
<body>
<video id="output" width="200" height="200" autoplay>Put your fallback message here.</video>
<canvas id="snapshotCanvas" width="200" height="200"></canvas><p>
<button id="startVideoCapture">Start Capture</button>
<button id="stopVideoCapture">Stop Capture</button>
<button id="takeSnapshot">Take snapshot</button>
<script>
var n = navigator, is_webkit = false;
var output = document.getElementById('output');
var canvas = document.querySelector('#snapshotCanvas');
var context = canvas.getContext('2d');
document.querySelector('#stopVideoCapture').addEventListener('click', function(e) {
output.pause();
}, false);
document.querySelector('#startVideoCapture').addEventListener('click', function(e) {
startCapture();
}, false);
document.querySelector('#takeSnapshot').addEventListener('click', function(e) {
takeSnapshot();
}, false);
function onSuccess(stream) {
var source;
output.autoplay = true;
if (!is_webkit) {
source = stream;
}
else {
source = window.webkitURL.createObjectURL(stream);
}
output.src = source;
}
function onError() {
// getUserMedia API not supported, or another application is using the webcam !
}
function startCapture() {
if (n.getUserMedia) {
// opera users (hopefully everyone else at some point)
n.getUserMedia({video:true, audio:true}, onSuccess, onError);
}
else if (n.webkitGetUserMedia) {
// webkit users
is_webkit = true;
n.webkitGetUserMedia({video:true}, onSuccess, onError);
}
}
takeSnapshot = function() {
context.drawImage(output, 0, 0, canvas.width, canvas.height);
}
var idx = 0;
var filters = [
'grayscale',
'sepia',
'blur',
'brightness',
'contrast',
'hue-rotate', 'hue-rotate2', 'hue-rotate3',
'saturate',
'invert',
''
];
function changeFilter(e) {
var el = e.target;
el.className = '';
var effect = filters[idx++ % filters.length];
if (effect) {
el.classList.add(effect);
}
}
output.addEventListener('click', changeFilter, false);
</script>
<script id="jsbin-source-javascript" type="text/javascript">
</script></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment