JSFiddle - React, Tailwind, and code Playground
by serio
HTML
<article>
<section id="app" hidden>
<div class="container"><span id="live">LIVE</span><video id="monitor" autoplay onclick="changeFilter(this)" title="Click me to see different filters"></video></div>
<p>Click the video to see different CSS filters</p>
</section>
<p><button onclick="init(this)">Capture</button></p>
<div id="splash">
<p id="errorMessage">↑<br>Click to begin</p>
</div>
<div id="gallery"></div>
</article>
<canvas id="photo" style="display:none"></canvas>
JavaScript
navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.getUserMedia;
window.URL = window.URL || window.webkitURL;
var app = document.getElementById('app');
var video = document.getElementById('monitor');
var canvas = document.getElementById('photo');
var effect = document.getElementById('effect');
var gallery = document.getElementById('gallery');
var ctx = canvas.getContext('2d');
var intervalId = null;
var idx = 0;
var filters = [
'grayscale',
'sepia',
'blur',
'brightness',
'contrast',
'hue-rotate', 'hue-rotate2', 'hue-rotate3',
'saturate',
'invert',
''
];
function changeFilter(el) {
el.className = '';
var effect = filters[idx++ % filters.length];
if (effect) {
el.classList.add(effect);
}
}
function gotStream(stream) {
if (window.URL) {
video.src = window.URL.createObjectURL(stream);
} else {
video.src = stream; // Opera.
}
video.onerror = function(e) {
stream.stop();
};
stream.onended = noStream;
video.onloadedmetadata = function(e) { // Not firing in Chrome. See crbug.com/110938.
document.getElementById('splash').hidden = true;
document.getElementById('app').hidden = false;
};
// Since video.onloadedmetadata isn't firing for getUserMedia video, we have
// to fake it.
setTimeout(function() {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
document.getElementById('splash').hidden = true;
document.getElementById('app').hidden = false;
}, 50);
}
function noStream(e) {
var msg = 'No camera available.';
if (e.code == 1) {
msg = 'User denied access to use camera.';
}
document.getElementById('errorMessage').textContent = msg;
}
function capture() {
if (intervalId) {
clearInterval(intervalId);
intervalId = null;
return;
}
intervalId = setInterval(function() {
ctx.drawImage(video, 0, 0);
var img = document.createElement('img');
img.src = canvas.toDataURL('image/webp');
var angle =...