document.addEventListener('DOMContentLoaded',
function
() {
var
video = document.querySelector('video');
var
audio, audioType;
var
canvas = document.querySelector('canvas');
var
context = canvas.getContext('2d');
var
iFilter = 0;
var
filters = [
'grayscale',
'sepia',
'blur',
'brightness',
'contrast',
'hue-rotate',
'hue-rotate2',
'hue-rotate3',
'saturate',
'invert',
'none'
];
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
if
(navigator.getUserMedia) {
navigator.getUserMedia({video: true, audio: true}, onSuccessCallback, onErrorCallback);
function
onSuccessCallback(stream) {
video.src = window.URL.createObjectURL(stream) || stream;
video.play();
audio =
new
Audio();
audioType = getAudioType(audio);
if
(audioType) {
audio.src = 'polaroid.' + audioType;
audio.play();
}
}
function
onErrorCallback(e) {
var
expl = 'An error occurred: [Reason: ' + e.code + ']';
console.error(expl);
alert(expl);
return
;
}
}
else
{
document.querySelector('.container').style.visibility = 'hidden';
document.querySelector('.warning').style.visibility = 'visible';
return
;
}
function
drawVideoAtCanvas(obj, context) {
window.setInterval(
function
() {
context.drawImage(obj, 0, 0);
}, 60);
}
function
getAudioType(element) {
if
(element.canPlayType) {
if
(element.canPlayType('audio/mp4; codecs=
"mp4a.40.5"
') !== '') {
return
('aac');
}
else
if
(element.canPlayType('audio/ogg; codecs=
"vorbis"
') !== '') {
return
(
"ogg"
);
}
}
return
false;
}
video.addEventListener('play',
function
() {
drawVideoAtCanvas(this, context);
}, false);
document.querySelector('button').addEventListener('click',
function
() {
video.className = '';
canvas.className = '';
var
effect = filters[iFilter++ % filters.length];
if
(effect) {
video.classList.add(effect);
canvas.classList.add(effect);
document.querySelector('.container h3').innerHTML = 'Current filter is: ' + effect;
}
}, false);
}, false);