Skip to content

Instantly share code, notes, and snippets.

@nogizhopaboroda
Created June 2, 2019 12:18
Show Gist options
  • Select an option

  • Save nogizhopaboroda/fe8565c98bf795d4c92e9b619a5ab478 to your computer and use it in GitHub Desktop.

Select an option

Save nogizhopaboroda/fe8565c98bf795d4c92e9b619a5ab478 to your computer and use it in GitHub Desktop.
<html>
<head>
<style>
.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>
<div id="screenshot" style="text-align:center;">
<video class="videostream" autoplay="" style="height: 100%;"></video>
<img id="screenshot-img">
<p><button class="capture-button">Capture video</button>
</p><p><button id="screenshot-button" disabled="">Take screenshot</button></p>
</p><p><button id="cssfilters-apply">Apply filter</button></p>
</div>
<video autoplay></video>
<img src="">
<canvas style="display:none;"></canvas>
<script>
const captureVideoButton =
document.querySelector('#screenshot .capture-button');
const screenshotButton = document.querySelector('#screenshot-button');
const cssFiltersButton =
document.querySelector('#cssfilters-apply');
const img = document.querySelector('#screenshot img');
const video = document.querySelector('#screenshot video');
const canvas = document.createElement('canvas');
const hdConstraints = {
video: {width: {min: 1280}, height: {min: 720}, deviceId: {
exact: '9b240573a4ee3f6bd11e30736e240eee2d6f719de9ab0a32e240082b77162fdb'
}}
};
const constraints = hdConstraints;
let filterIndex = 0;
const filters = [
'grayscale',
'sepia',
'brightness',
'contrast',
'hue-rotate',
'hue-rotate2',
'hue-rotate3',
'saturate',
'invert',
''
];
cssFiltersButton.onclick = video.onclick = function() {
video.className = filters[filterIndex++ % filters.length];
};
captureVideoButton.onclick = function() {
navigator.mediaDevices.enumerateDevices()
.then(devices => {
var videoDevices = [0,0];
var videoDeviceIndex = 0;
devices.forEach(function(device) {
console.log(device.kind + ": " + device.label +
" id = " + device.deviceId);
if (device.kind == "videoinput") {
videoDevices[videoDeviceIndex++] = device.deviceId;
}
});
var constraints = {
deviceId: { exact: videoDevices[0] }
};
return navigator.mediaDevices.getUserMedia({ video: constraints });
}).then(handleSuccess);
};
screenshotButton.onclick = video.onclick = function() {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
// Other browsers will fall back to image/png
img.src = canvas.toDataURL('image/webp');
};
function handleSuccess(stream) {
screenshotButton.disabled = false;
video.srcObject = stream;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment